1 Lecture 18 Further Threading Overview  Case Study: Cooperating Threads  Producer/ Consumer model threads  Waiting for Synchronized Data  Busy Waiting.

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.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli | Ralph Walde Trinity College Hartford, CT presentation slides for published by Prentice.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
The Critical-Section Problem
11-Jun-15 Producer-Consumer An example of using Threads.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
16-Jun-15 Producer-Consumer An example of using Threads.
26-Jun-15 Threads and Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread extends.
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.
Definitions Process – An executing program
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Internet Software Development More stuff on Threads Paul Krause.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
The Critical Section Problem
1 Java Threads and Synchronization Review Modified from slides taken from
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Threading Eriq Muhammad Adams J
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
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.
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.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
 2002 Prentice Hall, Inc. All rights reserved. Outline HoldIntegerUnsyn chronized.java Line 4 Lines 7-13 Lines // Fig. 15.6: HoldIntegerUnsynchronized.java.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
CSCD 330 Network Programming
Multithreading / Concurrency
Threads and Concurrency in Java: Part 2
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Threads II IS
Monitors Chapter 7.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Multithreading.
Monitors Chapter 7.
Monitors Chapter 7.
Component-Based Software Engineering
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
Problems with Locks Andrew Whitaker CSE451.
Presentation transcript:

1 Lecture 18 Further Threading Overview  Case Study: Cooperating Threads  Producer/ Consumer model threads  Waiting for Synchronized Data  Busy Waiting Problem  Using wait/notify to Coordinate Threads 

2 Lecture 18 Case Study: Cooperating Threads l Cooperating threads require explicit synchronization and coordination. l Problem: Simulate a bakery waiting line with a clerk and one or more customers. Use a take-a-number device to manage waiting. l Class Decomposition: »Bakery : main program, starts the threads. »TakeANumber : keeps track of who’s next. »Clerk : serves the next customer »Customer: waits on line.

3 Lecture 18 TakeANumber Class (Version 1) class TakeANumber { private int next = 0; // Next place in line private int serving = 0; // Next customer to serve public synchronized int nextNumber() { next = next + 1; return next; } // nextNumber() public int nextCustomer() { ++serving; return serving; } // nextCustomer() } // TakeANumber Used by customers. Shared Resource: A TakeANumber object is shared by several threads. Used by clerk. A synchronized method cannot be preempted.

4 Lecture 18 Java Monitors and Mutual Exclusion l Monitor: a mechanism than ensures only one thread at a time can execute a synchronized method. When a synchronized method -- nextNumber() -- is called, its object -- TakeANumber -- is locked. l Mutual exclusion: While an object is locked, none of its synchronized methods can be run. l While one customer is getting a number, it can’t be preempted by another customer. l Effective Design: Synchronization. To provide mutually exclusive access to an object’s methods, declare them synchronized.

5 Lecture 18 The Customer Class l Customers take the next number. public class Customer extends Thread { private static int number = 10000; // Initial ID number private int id; private TakeANumber takeANumber; public Customer( TakeANumber gadget ) { id = ++number; takeANumber = gadget; } public void run() { try { sleep( (int)(Math.random() * 1000 ) ); System.out.println("Customer " + id + " takes ticket " + takeANumber.nextNumber()); } catch (InterruptedException e) { System.out.println("Exception " + e.getMessage()); } } // run() } // Customer Unique ID. This customer may have to wait. Class variable.

6 Lecture 18 The Clerk Class l Clerks repeatedly serve the next customer. public class Clerk extends Thread { private TakeANumber takeANumber; public Clerk(TakeANumber gadget) { takeANumber = gadget; } public void run() { while (true) { try { sleep( (int)(Math.random() * 50)); System.out.println("Clerk serving ticket " + takeANumber.nextCustomer()); } catch (InterruptedException e) { System.out.println("Exception " + e.getMessage() ); } } //while } //run() } // Clerk Infinite loop. Serve next customer.

7 Lecture 18 The Bakery Class Bakery starts clerk and customer threads, passing them a reference to the TakeANumber. public class Bakery { public static void main(String args[]) { System.out.println( "Starting clerk and customer threads" ); TakeANumber numberGadget = new TakeANumber(); Clerk clerk = new Clerk(numberGadget); clerk.start(); for (int k = 0; k < 5; k++) { Customer customer = new Customer(numberGadget); customer.start(); } } // main() } // Bakery 5 customers 1 clerk

8 Lecture 18 Problem: Nonexistent Customers l Problem: Clerk doesn’t wait for customers. Starting clerk and customer threads Clerk serving ticket 1 Clerk serving ticket 2 Clerk serving ticket 3 Clerk serving ticket 4 Clerk serving ticket 5 Customer takes ticket 1 Customer takes ticket 2 Clerk serving ticket 6 Customer takes ticket 3 Clerk serving ticket 7 Clerk serving ticket 8 Clerk serving ticket 9 Clerk serving ticket 10 Customer takes ticket 4 Customer takes ticket 5 Clerk thread should wait until a customer takes a number.

9 Lecture 18 Sychronize: Clerk Waits for Customers … And TakeANumber: public void run() { while (true) { try { sleep((int)(Math.random() * 50)); if (takeANumber.customerWaiting()) System.out.println("Clerk serving ticket " + takeANumber.nextCustomer()); } catch (InterruptedException e) { System.out.println("Exception " + e.getMessage() ); } } // while } // run() Clerk checks for customer. Solution: Modify Clerk.run()... public boolean customerWaiting() { return next > serving; }

10 Lecture 18 Thread Cooperation Starting clerk and customer threads Customer takes ticket 1 Clerk serving ticket 1 Customer takes ticket 2 Clerk serving ticket 2 Customer takes ticket 3 Clerk serving ticket 3 Customer takes ticket 4 Clerk serving ticket 4 Customer takes ticket 5 Clerk serving ticket 5 Service follows customer’s arrival. l Effective Design: Thread cooperation must be designed into the algorithm.

11 Lecture 18 Problem: Critical Sections System.out.println("Customer " + id + " takes ticket " + takeANumber.nextNumber()); l A critical section is a section of a thread that should not be preempted in the middle. l If we simulate the preemption of: public void run() { // Customer.run() try { int myturn = takeANumber.nextNumber(); sleep( (int)(Math.random() * 1000 ) ); System.out.println("Customer " + id + " takes ticket " + myturn); } catch (InterruptedException e) { System.out.println("Exception " + e.getMessage()); } } // run() What if another thread runs here?

12 Lecture 18 Problem: Misleading Output Starting clerk and customer threads Clerk serving ticket 1 Clerk serving ticket 2 Clerk serving ticket 3 Customer takes ticket 4 Clerk serving ticket 4 Clerk serving ticket 5 Customer takes ticket 1 Customer takes ticket 2 Customer takes ticket 3 Customer takes ticket 5 … this output does not reflect the true state of the simulation. l If there’s a break (preemption) between a customer’s taking a number and reporting its number, we could get: Logically, our code insures that the clerk can’t serve until a ticket is taken but...

13 Lecture 18 Creating a Critical Section Let TakeANumber report the state. public class TakeANumber { private int next = 0; // Next place in line private int serving = 0; // Next customer to serve public synchronized int nextNumber(int custId) { next = next + 1; System.out.println( "Customer " + custId + " takes ticket " + next ); return next; } public synchronized int nextCustomer() { ++serving; System.out.println(" Clerk serving ticket " + serving ); return serving; } public synchronized boolean customerWaiting() { return next > serving; } } // TakeANumber Critical Sections: Synchronized methods cannot be preempted.

14 Lecture 18 Revised Customer and Clerk l The clerk and customers do no reporting. public void run() { // Customer.run() try { sleep((int)(Math.random() * 2000)); takeANumber.nextNumber(id); } catch (InterruptedException e) { System.out.println("Exception: " + e.getMessage() ); } } // run() Just take a number. public void run() { // Clerk.run() for (int k = 0; k < 10; k++) { try { sleep( (int)(Math.random() * 1000)); if (takeANumber.customerWaiting()) takeANumber.nextCustomer(); } catch (InterruptedException e) { System.out.println("Exception: " + e.getMessage()); } } // for } // run() Just serve a customer.

15 Lecture 18 The Thread Coordination Principle Starting clerk and customer threads Customer takes ticket 1 Clerk serving ticket 1 Customer takes ticket 2 Customer takes ticket 3 Clerk serving ticket 2 Customer takes ticket 4 Customer takes ticket 5 Clerk serving ticket 3 Clerk serving ticket 4 Clerk serving ticket 5 Customers served in the correct order no matter how they arrive. l Correct output: l Effective Design: Use critical sections to enforce mutual exclusion and to coordinate threads.

16 Lecture 18 Busy Waiting Problem Even though its threads are properly coordinated, our bakery simulation uses busy waiting in the Clerk class: public void run() { for (int k = 0; k < 10; k++) { try { sleep( (int)(Math.random() * 1000)); if (takeANumber.customerWaiting()) takeANumber.nextCustomer(); } catch (InterruptedException e) { System.out.println("Exception: " + e.getMessage()); } } // for } // run() Busy waiting. Wait in a loop.

17 Lecture 18 Using wait/notify to Coordinate Threads The wait() method puts a thread into a waiting state, and notify() takes a thread out of waiting and places it in the ready queue. l Alternative Design: The clerk waits to be notified by a customer. This requires modifications to TakeANumber and Clerk classes. l Producer/Consumer Model: two threads share a resource, one serving to produce it and the other to consume it.

18 Lecture 18 Revised TakeANumber Class public synchronized int nextCustomer() { try { while (next <= serving) { System.out.println(" Clerk waiting "); wait(); } } catch(InterruptedException e) { System.out.println("Exception " + e.getMessage() ); } finally { ++serving; System.out.println(" Clerk serving ticket " + serving ); return serving; } } // nextCustomer() When a clerk calls this method, it must wait for a customer to arrive. public synchronized int nextNumber(int custId) { next = next + 1; System.out.println( "Customer " + custId + " takes ticket " + next ); notify(); return next; } // nextNumber() When a customer calls this method, it notifies the clerk that is has arrived.

19 Lecture 18 Revised Clerk Class The Clerk.run() method is now simplified to: public void run() { while (true) { try { sleep((int)(Math.random() * 1000)); takeANumber.nextCustomer(); } catch (InterruptedException e) { System.out.println("Exception: " + e.getMessage() ); } } // while } // run() Infinite loop. Starting clerk and customer threads Customer takes ticket 1 Customer takes ticket 2 Clerk serving ticket 1 Clerk serving ticket 2 Customer takes ticket 3 Customer takes ticket 4 Clerk serving ticket 3 Customer takes ticket 5 Clerk serving ticket 4 Clerk serving ticket 5 Clerk waiting New Output Clerk will be notified when a new customer arrives.

20 Lecture 18 Restrictions on wait/notify Mechanism Both wait() and notify() are methods of the Object class. This enables them to lock objects. A wait() method can be used within any synchronized method, not just within a Thread. Both wait() and notify() must be used within synchronized methods. Otherwise you will cause a IllegalMonitorStateException with the message “current thread not owner.” When wait() is used within a synchronized method, the lock on that object is released, allowing other methods to call the object’s synchronized methods.