Presentation is loading. Please wait.

Presentation is loading. Please wait.

Condition Variables and Producer/Consumer

Similar presentations


Presentation on theme: "Condition Variables and Producer/Consumer"— Presentation transcript:

1 Condition Variables and Producer/Consumer
CSE451 Andrew Whitaker

2 Background Often, a thread needs to wait on another thread until some condition becomes true Example: producer/consumer problems Some threads produce items Other threads consume items Consuming threads block until there is something to consume Condition variables provide a “rendezvous” mechanism Wait until some condition becomes true

3 A Simple Example Thread 1: Thread 2: Wait a second Increment a counter
Repeat Thread 2: Wait for the counter to reach 10 (in other words, wait ten seconds)

4 Busy waiting is bad! public class TimerCounter implements Runnable {
// this counter gets incremented every second static volatile int count = 0; static final int TARGET = 10; public static void main(String[] args) { // start the counter thread Thread t = new Thread (new TimerCounter()); t.start(); // wait TARGET seconds while (count < TARGET) { ; } // busy wait System.out.println(TARGET + " seconds has expired"); } public void run() { while (count < TARGET) { try { Thread.sleep(1000); // sleep for one second } catch (InterruptedException e) {} // ignored count++; Busy waiting is bad!

5 Revised Version Replace busy waiting with a condition variable
Allows the thread to safely give up the processor Essentially, we are replacing polling with interruption

6 wait releases the lock // class TimerCounter {
static final Object lockObject = new Object(); public static void main(String[] args) { // initialization as before synchronized (lockObject) { while (count < TARGET) { try { lockObject.wait(); } catch (InterruptedException excp) {} // ignored System.out.println(TARGET + " seconds has expired"); public void run() { try { Thread.sleep(1000); } // sleep for one second catch (InterruptedException e) {} // ignored count++; lockObject.notify(); wait releases the lock

7 Condition Variable Details
Condition variables support three operations: wait,notify,notifyAll Every condition variable is associated with a lock The lock must be held when doing any operation Invoking wait releases the lock Lock is held when wait returns Condition predicate should always be tested in a while loop

8 Under the Covers Each condition variable has an associated wait queue
Lock ensures thread-safe access to this queue notify: Move one thread from blocked to runnable notifyAll: Move all threads from blocked to runnable blocked threads

9 Example: Bounded Buffer
Bounded buffer is a finite queue of elements Commonly used to coordinate producers and consumers Put adds an element to the queue And, blocks if the queue is full Take removes an element from the queue And, blocks if the queue is empty put take


Download ppt "Condition Variables and Producer/Consumer"

Similar presentations


Ads by Google