Producer-Consumer Problem

Producer-Consumer Problem using Semaphore

Producer() {

put(item) {

P(empty); // for wait, Get an empty count, block if unavailable
P(mutex); // lock critical section: shared buffer
<<< critical section: Put item into shared buffer >>>
V(mutex); // unlock critical section
V(full); // for signal, increase number of full count

}

}

Consumer() {

take () {

P(full); // for wait, Get a full count, block if unavailable
P(mutex);
<<< critical section: Take item from shared buffer >>>
V(mutex);
V(empty); // for signal, increase number of empty count
return item;

}

}

Producer-Consumer Problem using Monitor

Producer() {

synchronized put(item) {

while(count >= buffer.length) { wait(); } // wait if buffer is full
count++
<<< critical section: Put iteminto shared buffer >>>
notifyAll(); // signal

}

}

Consumer() {

synchronized take () {

while(count <=0) { wait(); } // wait if buffer is empty
count- –
<<< critical section: Take itemfrom shared buffer >>>
notifyAll(); // signal
return item;

}

}

Concurrency in Swing

Concurrency in Swing
https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html

A Swing programmer deals with the following kinds of threads:

 

Java Thread Synchronization

Thread Synchronization

https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

  • Mutex Semaphore (aka Mutex) controls only one thread at a time executing on the shared resource by lock & unlock.
  • Counting Semaphore (aka Semaphore – Java7) controls the number of threads executing on the shared resource by acquire & release.
  • Monitor controls only one thread at a time, and can execute in the monitor (shared object) by wait & notify/notifyAll.