Skip to content

Instantly share code, notes, and snippets.

@CremboC
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CremboC/982a304f28f225f17494 to your computer and use it in GitHub Desktop.
Save CremboC/982a304f28f225f17494 to your computer and use it in GitHub Desktop.
/**
* Put an item into the bounded buffer
*
* @param itm
* @throws InterruptedException
*/
public void put(int itm) throws InterruptedException {
enter();
boolean acquired = false;
while (numberInBuffer == size) {
acquired = doWait(acquired);
}
// Critical section
last = (last + 1) % size;
items[last] = itm;
numberInBuffer++;
System.out.println("Produced. Slot: " + last);
doNotifyAll();
exit();
}
/**
* Take items out of the bounded buffer
*
* @return the taken item
* @throws InterruptedException
*/
public int get() throws InterruptedException {
enter();
boolean acquired = false;
while (numberInBuffer == 0) {
acquired = doWait(acquired);
}
// Critical section
first = (first + 1) % size;
int item = items[first];
numberInBuffer--;
System.out.println("Consumed. Number: " + items[first] + " Slot: " + first);
doNotifyAll();
exit();
return item;
}
/**
* Implementation of monitors using the semaphore implementation created in Task 2.
* Detail explanation of this class in attached pdf
*/
public static class Monitor {
// makes sure mutex is acquired for critical sections
private Semaphore mutex = new Semaphore();
private Semaphore notifyCalled = new Semaphore(0);
// count of threads waiting
private int threadsWaiting = 0;
/**
* What the compilers insert at the beginning of every synchronized method
* Detail explanation in attached pdf
*
* @throws InterruptedException
*/
protected void enter() throws InterruptedException {
mutex.down();
}
/**
* Equivalent of Object.wait();
* Detail explanation in attached pdf
*
* @param acquired the lock
* @return the changed lock, if it was acquired
* @throws InterruptedException
*/
protected boolean doWait(boolean acquired) throws InterruptedException {
// Equivalent of wait()
if (acquired) {
threadsWaiting--;
notifyCalled.up();
}
threadsWaiting++;
mutex.up();
notifyCalled.down();
mutex.down();
acquired = true;
return acquired;
}
/**
* Equivalent to Object.notifyAll()
* Detail explanation in attached pdf
*
* @throws InterruptedException
*/
protected void doNotifyAll() throws InterruptedException{
while (threadsWaiting > 0) {
threadsWaiting--;
notifyCalled.up();
}
}
/**
* Equivalent to Object.notify()
* Detail explanation in attached pdf
*
* @throws InterruptedException
*/
protected void doNotify() throws InterruptedException {
if (threadsWaiting > 0) {
threadsWaiting--;
notifyCalled.up();
}
}
/**
* What the compiler inserts at the end of every synchronized method;
* Detail explanation in attached pdf
*
* @throws InterruptedException
*/
protected void exit() throws InterruptedException {
mutex.up();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment