







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








From “Alfonse, Wait Here For My Signal!”, Stephen J. Hartley, SIGCSE 3/99 New Orleans, LA, USA
class Monitor extends ... { private ... // data fields (state variables) public synchronized type method1(...) { ... notifyAll(); // if any wait conditions altered while (!condition) try { wait(); } catch (InterruptedException e) {} ... notifyAll(); // if any wait conditions altered } ... }
public class CountingSemaphore { private int value = 0; public CountingSemaphore(int initial) { if (initial > 0) value = initial; } public synchronized void P() throws InterruptedException { if (value == 0) wait (); value-- ; } public synchronized void V() { if (value == 0) notify(); value++ ; } }
public class CountingSemaphore { private int value = 0; public CountingSemaphore (int initial) { if (initial > 0) value = initial; } public synchronized void P() throws InterruptedException { value-- ; if (value < 0) wait(); } public synchronized void V() { value++ ; if (value <= 0) notify(); } }
public class CountingSemaphore { …. public synchronized void P() throws InterruptedException { if (value == 0 || waitcount > 0) { waitCount++; try { do {wait();} while (!notified); } catch (InterruptedException e) {notify(); throw e;} finally {waitCount--;} notified = false; } value--; } public synchronized void V() { value++ ; if (waitcount > 0) {notified = true; notify();} } }
public class CountingSemaphore ( public CountingSemaphore(int initial) { if (initial > 0) value = initial;} public synchronized void P() throws InterruptedException { if (value == 0 || waitcount > 0) { waitCount++ ; try {do {wait ();} while (notifycount == 0);} catch (InterruptedException e) { notify(); throw e;} finally {waitCount--;} notifyCount--;} value-- ;} public synchronized void V() { value++ ; if (waitcount > 0) { notifyCount++ ; notify0;} } }