alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (links.scala)

This example Scala source code file (links.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

actor, actor, application, exit, int, int, last, last, stop, stop

The Scala links.scala source code

package examples.actors

import scala.actors.{Actor, Exit}
import scala.actors.Actor._

object links extends Application {

  case object Stop

  actor {
    val start = link(p(2))
    start ! Stop
  }

  def p(n: Int): Actor =
    if (n == 0) top1()
    else top(p(n-1), n)

  def top(a: Actor, n: Int): Actor = actor {
    println("starting actor " + n + " (" + self + ")")
    self.trapExit = true
    link(a)
    loop {
      receive {
        case ex @ Exit(from, reason) =>
          println("Actor " + n + " received " + ex)
          exit('finished)
        case any => {
          println("Actor " + n + " received " + any)
          a ! any
        }
      }
    }
  }

  def top1(): Actor = actor {
    println("starting last actor"  + " (" + self + ")")
    receive {
      case Stop =>
        println("Last actor now exiting")
        exit('abnormal)
      case any =>
        println("Last actor received " + any)
        top1()
    }
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala links.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.