diff options
author | Philipp Haller <hallerp@gmail.com> | 2006-09-29 16:50:44 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2006-09-29 16:50:44 +0000 |
commit | 60b3d90f81cf3f83440725a02afc7dc693fa9ea5 (patch) | |
tree | a441481b4812eb9815db8946dea88dc3d5e08245 /docs/examples/actors/BoundedBufferTest.scala | |
parent | 499d7f10e23549ef30a61d13fc7f4203145f14f1 (diff) | |
download | scala-60b3d90f81cf3f83440725a02afc7dc693fa9ea5.tar.gz scala-60b3d90f81cf3f83440725a02afc7dc693fa9ea5.tar.bz2 scala-60b3d90f81cf3f83440725a02afc7dc693fa9ea5.zip |
Checked in examples for new actors lib.
Diffstat (limited to 'docs/examples/actors/BoundedBufferTest.scala')
-rw-r--r-- | docs/examples/actors/BoundedBufferTest.scala | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/docs/examples/actors/BoundedBufferTest.scala b/docs/examples/actors/BoundedBufferTest.scala new file mode 100644 index 0000000000..5a04f7aafd --- /dev/null +++ b/docs/examples/actors/BoundedBufferTest.scala @@ -0,0 +1,33 @@ +package examples.actors + +import scala.actors.Actor._ + +class BoundedBuffer[T](N: int) { + private case class Get + private case class Put(x: T) + + private val buffer = actor { + val buf = new Array[T](N) + var in = 0; var out = 0; var n = 0 + while(true) { + receive { + case Put(x) if n < N => + buf(in) = x; in = (in + 1) % N; n = n + 1; reply() + case Get() if n > 0 => + val r = buf(out); out = (out + 1) % N; n = n - 1; reply(r) + } + } + } + + def put(x: T): Unit = buffer !? Put(x) + + def get: T = (buffer !? Get()).asInstanceOf[T] +} + +object BoundedBufferTest { + def main(args: Array[String]) = { + val buf = new BoundedBuffer[Int](1) + buf.put(42) + scala.Console.println("" + buf.get) + } +} |