diff options
author | michelou <michelou@epfl.ch> | 2003-12-08 16:17:42 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2003-12-08 16:17:42 +0000 |
commit | 5e87c33e2aaec7a0c9bcd6f257a9a8e4f9664529 (patch) | |
tree | 941571431fef1a7273ea82fa2475b99d35ba66ca /sources/examples/boundedbuffer.scala | |
parent | 103888d458fe8cc0ba7c8000c9e2b66923a1b430 (diff) | |
download | scala-5e87c33e2aaec7a0c9bcd6f257a9a8e4f9664529.tar.gz scala-5e87c33e2aaec7a0c9bcd6f257a9a8e4f9664529.tar.bz2 scala-5e87c33e2aaec7a0c9bcd6f257a9a8e4f9664529.zip |
- adapted to current Scala version
Diffstat (limited to 'sources/examples/boundedbuffer.scala')
-rw-r--r-- | sources/examples/boundedbuffer.scala | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/sources/examples/boundedbuffer.scala b/sources/examples/boundedbuffer.scala index 6f71f72cf5..fdc6feb9b4 100644 --- a/sources/examples/boundedbuffer.scala +++ b/sources/examples/boundedbuffer.scala @@ -1,30 +1,34 @@ package examples; -import concurrent.ops._; +object boundedbuffer { -class BoundedBuffer[a](N: Int) extends Monitor() { - var in = 0, out = 0, n = 0; - val elems = new Array[a](N); + import concurrent.ops._; - def put(x: a) = synchronized { - await (n < N); - elems(in) = x ; in = (in + 1) % N ; n = n + 1; - if (n == 1) notifyAll(); + class BoundedBuffer[a](N: Int) extends Monitor() { + var in = 0, out = 0, n = 0; + val elems = new Array[a](N); + + def put(x: a) = synchronized { + await (n < N); + elems(in) = x; in = (in + 1) % N; n = n + 1; + if (n == 1) notifyAll(); + } + + def get: a = synchronized { + await (n != 0); + val x = elems(out); out = (out + 1) % N ; n = n - 1; + if (n == N - 1) notifyAll(); + x + } } - def get: a = synchronized { - await (n != 0); - val x = elems(out) ; out = (out + 1) % N ; n = n - 1; - if (n == N - 1) notifyAll(); - x + def main(args: Array[String]) = { + val buf = new BoundedBuffer[String](10); + var cnt = 0; + def produceString = { cnt = cnt + 1; cnt.toString() } + def consumeString(ss: String) = System.out.println(ss); + spawn { while (true) { val ssss = produceString; buf.put(ssss) } } + spawn { while (true) { val s = buf.get; consumeString(s) } } } -} -module test { - val buf = new BoundedBuffer[String](10); - var cnt = 0; - def produceString = { cnt = cnt + 1; cnt.toString() } - def consumeString(ss: String) = System.out.println(ss); - spawn { while (True) { val ssss = produceString ; buf.put(ssss) } } - spawn { while (True) { val s = buf.get ; consumeString(s) } } } |