Condition Variables and Producer/Consumer

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Ch 7 B.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
1 Lecture 18 Further Threading Overview  Case Study: Cooperating Threads  Producer/ Consumer model threads  Waiting for Synchronized Data  Busy Waiting.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
CS 11 java track: lecture 7 This week: Web tutorial:
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
11-Jun-15 Producer-Consumer An example of using Threads.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
28-Jun-15 Producer-Consumer An example of using Threads.
Threads Just Java: C10–pages 251- C11–pages 275-
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
Multithreading.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Synchronization Methods in Message Passing Model.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
CSC321 §8 Implementing FSP Models in Java 1 Section 8 Implementing FSP Models in Java.
pThread synchronization
Distributed and Parallel Processing George Wells.
Race Conditions & Synchronization
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Multi-processor Scheduling
Multi Threading.
Lecture 9 Object Oriented Programming Using Java
Background on the need for Synchronization
Practice Session 8 Lockfree LinkedList Blocking queues
Java Concurrency.
Multithreading Chapter 9.
System Programming Practical Session 7
Threads and Concurrency in Java: Part 2
Threads Chate Patanothai.
Java Concurrency.
Threads II IS
Synchronization Lecture 23 – Fall 2017.
Condition Variables and Producer/Consumer
Race Conditions & Synchronization
Producer-Consumer Problem
Multithreading 2 Lec 24.
Chapter 30 Condition Variables
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Computer Science 2 06A-Java Multithreading
Software Engineering and Architecture
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Presentation transcript:

Condition Variables and Producer/Consumer CSE451 Andrew Whitaker

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

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)

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!

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

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

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

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

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