diff options
author | michelou <michelou@epfl.ch> | 2004-02-23 13:08:32 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2004-02-23 13:08:32 +0000 |
commit | a45c078ec7bcead01ebd13835fedec6e271deadd (patch) | |
tree | 5c83c323ae5662b88b4a1a4f71e6a396b06a2c6b /sources | |
parent | c2e3c0f366628a64e735686f3cd871843ea239c6 (diff) | |
download | scala-a45c078ec7bcead01ebd13835fedec6e271deadd.tar.gz scala-a45c078ec7bcead01ebd13835fedec6e271deadd.tar.bz2 scala-a45c078ec7bcead01ebd13835fedec6e271deadd.zip |
- example 'buffer1' renamed to 'oneplacebuffer'
Diffstat (limited to 'sources')
-rw-r--r-- | sources/examples/oneplacebuffer.scala | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/sources/examples/oneplacebuffer.scala b/sources/examples/oneplacebuffer.scala new file mode 100644 index 0000000000..9a7d1d714d --- /dev/null +++ b/sources/examples/oneplacebuffer.scala @@ -0,0 +1,46 @@ +package examples; + +object oneplacebuffer { + + import scala.concurrent._; + + class OnePlaceBuffer { + private val m = new MailBox(); // An internal mailbox + private case class Empty(), Full(x: Int); // Types of messages we deal with + + m send Empty(); // Initialization + + def write(x: Int): Unit = m receive { + case Empty() => + Console.println("put " + x); + m send Full(x) + } + + def read: Int = m receive { + case Full(x) => + Console.println("get " + x); + m send Empty() ; x + } + } + + def main(args: Array[String]) = { + val buf = new OnePlaceBuffer; + val random = new java.util.Random(); + + def producer(n: int): unit = { + Thread.sleep(random.nextInt(1000)); + buf.write(n); + producer(n + 1) + } + + def consumer: unit = { + Thread.sleep(random.nextInt(1000)); + val n = buf.read; + consumer + } + + ops.spawn(producer(0)); + ops.spawn(consumer) + } + +} |