// A simple producer-comsumer skeleton using Java threads.
//
// Operating Systems, Spring 2012
import java.util.concurrent.SynchronousQueue;
class P
implements Runnable
P(SynchronousQueue<Widget> q)
// blah blah blah
this.q = q
public void run()
while (true)
Widget w = null
// blah blah blah
try { q.put(w); }
catch (InterruptedException e)
// blah blah blah
private SynchronousQueue<Widget> q
class C
implements Runnable
C(SynchronousQueue<Widget> q)
// blah blah blah
this.q = q
public void run()
while (true)
Widget w = null
try { w = q.take(); }
catch (InterruptedException e)
// blah blah blah
// blah blah blah
private SynchronousQueue<Widget> q
class Widget
// blah blah blah
class Driver
public static void main(String args[])
SynchronousQueue<Widget> q = new SynchronousQueue<Widget>()
Thread p = new Thread(new P(q))
Thread c = new Thread(new C(q))
p.start()
c.start()
try
p.join()
c.join()
catch (InterruptedException e)
// blah blah blah
syntax highlighted by Code2HTML, v. 0.9.1