11-Jun-15 Producer-Consumer An example of using Threads.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Written by: Dr. JJ Shepherd
CS Concurrent Programming CS 3331 Fall 2009.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
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:
02/05/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
16-Jun-15 Producer-Consumer An example of using Threads.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
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.
02/02/2004CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
28-Jun-15 Producer-Consumer An example of using Threads.
Definitions Process – An executing program
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Threads Just Java: C10–pages 251- C11–pages 275-
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.
Tutorial 2 Adventures in Threading presented by: Antonio Maiorano Paul Di Marco.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Java Threads Instructor: Mainak Chaudhuri
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
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.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Threading Eriq Muhammad Adams J
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
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.
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.
COMPSCI 230 S2C 2015 Software Design and Construction Synchronization (cont.) Lecture 4 of Theme C.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
23-Dec-15 Threads and Taking Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
 2002 Prentice Hall, Inc. All rights reserved. Outline HoldIntegerUnsyn chronized.java Line 4 Lines 7-13 Lines // Fig. 15.6: HoldIntegerUnsynchronized.java.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Written by: Dr. JJ Shepherd
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1.
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.
Component-Based Software Engineering Understanding Thread Safety Paul Krause.
Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Threads and Multithreading
USING ECLIPSE TO CREATE HELLO WORLD
THREADS.
Section 5.7 Concurrency, Interference, and Synchronization.
Threads Chate Patanothai.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Threads and Multithreading
Problems with Locks Andrew Whitaker CSE451.
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

11-Jun-15 Producer-Consumer An example of using Threads

The problem One Thread, the producer, is “producing” information (creating objects), while another Thread, the consumer, is “consuming” (using) it We want the consumer to use the objects in the same order as the producer creates them We don’t want either Thread to be “loafing”—if there is work for it to do, the Thread should be doing it We want to absolutely avoid busy loops Example busy loop: while (!ready) { } (the other Thread will reset the ready variable) Busy loops will consume all available CPU cycles and seriously slow down everything on the computer

The overall structure import java.util.Vector; public class CommandList { static Vector list = new Vector(); public static void put(String s) { // code on next slide } public static String get() { // code on slide after next } }

The put method public static void put(String s) { synchronized (list) { list.add(s); list.notify(); } } The synchronized(list) block will take the list object as soon as it becomes available, and “lock” it so no other synchronized(list) can use it until this block is completed There is nothing special about the list object; any object can be locked for synchronization

The get method public static String get() { if (list.size() > 0) { synchronized (list) { return (String)list.remove(0); } } else { try { synchronized (list) { list.wait(); return get(); } } catch (InterruptedException e) { return "InterruptedException"; } } } }

The Consumer class public class Consumer extends Thread { public void run() { while (true) { String s = CommandList.get(); System.out.println("Consuming " + s); } } }

The test class public class CommandListTester { public static void main(String[] args) { Consumer consumer = new Consumer(); consumer.start(); CommandList.put("one"); CommandList.put("two"); sleep(2000); CommandList.put("three"); CommandList.put("four"); } private static void sleep(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } } } Output:  one two  three four

The End