diff options
author | Philipp Haller <hallerp@gmail.com> | 2006-09-30 14:36:46 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2006-09-30 14:36:46 +0000 |
commit | 942bb16fc5543c5876c4870e3de4bcc49ea9610f (patch) | |
tree | 1639dfe3f6a52811f42f5b7cf28fcc1cbe31ff2f | |
parent | 53cb459ecf29b9018f7c100a419a2bbfd5d4dc64 (diff) | |
download | scala-942bb16fc5543c5876c4870e3de4bcc49ea9610f.tar.gz scala-942bb16fc5543c5876c4870e3de4bcc49ea9610f.tar.bz2 scala-942bb16fc5543c5876c4870e3de4bcc49ea9610f.zip |
Added example for actor linking.
-rw-r--r-- | docs/examples/actors/Links.scala | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/docs/examples/actors/Links.scala b/docs/examples/actors/Links.scala new file mode 100644 index 0000000000..77ba80cbee --- /dev/null +++ b/docs/examples/actors/Links.scala @@ -0,0 +1,44 @@ +package examples.actors + +import scala.actors.Actor._ +import scala.actors.Actor + +case object Stop + +object Links extends Application { + + 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 { + Console.println("starting actor " + n + " (" + Thread.currentThread() + ")") + //self.trapExit = true + link(a) + while (true) { + receive { + case any => { + Console.println("Actor " + n + " received " + any) + a ! any + } + } + } + } + + def top1(): Actor = actor { + Console.println("starting last actor" + " (" + Thread.currentThread() + ")") + receive { + case Stop => + Console.println("Last actor now exiting") + exit("abnormal") + case any => + Console.println("Last actor received " + any) + top1() + } + } +} |