/* "shared object" parallel Example program, to be used to compare it against a "JCSP" parallel program */ public class SimpleShObject { public static void main(String[] args) { Buffer b = new Buffer(); Thread t1 = new Thread(new Producer(b)); Thread t2 = new Thread(new Consumer(b)); t1.start(); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException E) { } ; System.out.println("Driver finished"); } } class Buffer { Integer data; boolean full = false; synchronized void put(Integer i) { while (true) { try { if (full) { wait(); } data = i; full = true; notifyAll(); break; } catch (InterruptedException E) { } } } synchronized Integer get() { Integer tmp; while (true) { try { if (!full) { wait(); } tmp = this.data; full = false; notifyAll(); break; } catch (InterruptedException E) { } } return tmp; } } class Producer implements Runnable { Buffer b; public Producer(Buffer our_b) { b = our_b; } public void run() { for (int i = 0; i < 10; i++) { b.put(new Integer(i)); } System.out.println("Producer finished"); } } class Consumer implements Runnable { Buffer b; public Consumer(Buffer our_b) { b = our_b; } public void run() { for (int i = 0; i < 10; i++) { Integer d = b.get(); System.out.println("Read: " + d.intValue()); } System.out.println("Consumer finished"); } }