Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998.

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

Assignment 5 - FSP and Java 1.The log-flume ride at a theme park operates as follows. This sequence is repeated forever. Once the log is empty it cleaned.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  synchronized methods and statements  wait.
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.
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.
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.
CS 11 java track: lecture 7 This week: Web tutorial:
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
CS444/CS544 Operating Systems Synchronization 3/21/2006 Prof. Searleman
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.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
28-Jun-15 Producer-Consumer An example of using Threads.
CS444/CS544 Operating Systems Classic Synchronization Problems 3/5/2007 Prof. Searleman
Threads Just Java: C10–pages 251- C11–pages 275-
© 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.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Tutorial 5 Even More Synchronization! presented by: Antonio Maiorano Paul Di Marco.
1 Java Threads and Synchronization Review Modified from slides taken from
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
CS884 (Prasad)java8Threads1 Concurrent Programming Multithreading.
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.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
Threading Eriq Muhammad Adams J
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 7: Process Synchronization Background The Critical-Section Problem.
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.
Today’s Agenda  Quick Review  Monitor Advanced Topics in Software Engineering 1.
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.
1 Software Construction and Evolution - CSSE 375 Exception Handling – Chaining & Threading Steve Chenoweth Office: Moench Room F220 Phone: (812)
7.1 Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
 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.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Multithreading and Garbage Collection Session 16.
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.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
CSC321 §8 Implementing FSP Models in Java 1 Section 8 Implementing FSP Models in Java.
Synchronization, part 3 Monitors, classical sync. problems
Java Concurrency.
CSS 430: Operating Systems - Process Synchronization
Day 13 Concurrency.
Day 15 Concurrency.
Java Concurrency.
Object-Oriented Software Engineering
Classical Synchronization Problems
null, true, and false are also reserved.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Synchronization, part 3 Monitors, classical sync. problems
Counter in Java class Counter { private integer count = 0; public void Increment() synchronized { ++count; } public integer GetCount() synchronized.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Sampath Kumar S Assistant Professor, SECE
Synchronization, part 3 Monitors, classical sync. problems
Presentation transcript:

Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998

Declare The Monitor (A Class) class BoundedBuffer {

Shared Data // designed for a single producer thread // and a single consumer thread private int numSlots = 0; private double[] buffer = null; private int putIn = 0, takeOut = 0; private int count = 0;

public BoundedBuffer(int numSlots) { if (numSlots <= 0) throw new IllegalArgumentException("numSlots<=0"); this.numSlots = numSlots; buffer = new double[numSlots]; System.out.println("BoundedBuffer alive, numSlots=" + numSlots); } Initializer Entry Method

Producer Entry Method public synchronized void deposit(double value) { while (count == numSlots) try { wait(); } catch (InterruptedException e) { System.err.println("interrupted out of wait"); } buffer[putIn] = value; putIn = (putIn + 1) % numSlots; count++; // wake up the consumer if (count == 1) notify(); // since it might be waiting System.out.println(" after deposit, count=" + count + ", putIn=" + putIn); }

Consumer Entry Method public synchronized double fetch() { double value; while (count == 0) try { wait(); } catch (InterruptedException e) { System.err.println("interrupted out of wait"); } value = buffer[takeOut]; takeOut = (takeOut + 1) % numSlots; count--; // wake up the producer if (count == numSlots-1) notify(); // since it might be waiting System.out.println(" after fetch, count=" + count + ", takeOut=" + takeOut); return value; }

End The Monitor Declaration } // End the class (just like any other)

Java Fair Reader-Writer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998

Declare The Monitor (A Class) class Database extends MyObject {

Shared Data private int numReaders = 0; private int numWriters = 0; private int numWaitingReaders = 0; private int numWaitingWriters = 0; private boolean okToWrite = true; private long startWaitingReadersTime = 0;

public Database() { super("rwDB"); } // Call the parent class’s constructor Initializer Entry Method

startRead Entry Method public synchronized void startRead(int i) { long readerArrivalTime = 0; if (numWaitingWriters > 0 || numWriters > 0) { numWaitingReaders++; readerArrivalTime = age(); while (readerArrivalTime >= startWaitingReadersTime) try {wait();} catch (InterruptedException e) {} numWaitingReaders--; } numReaders++; }

endRead Entry Method public synchronized void endRead(int i) { numReaders--; okToWrite = numReaders == 0; if (okToWrite) notifyAll(); }

startWrite Entry Method public synchronized void startWrite(int i) { if (numReaders > 0 || numWriters > 0) { numWaitingWriters++; okToWrite = false; while (!okToWrite) try {wait();} catch (InterruptedException e) {} numWaitingWriters--; } okToWrite = false; numWriters++; }

endWrite Entry Method public synchronized void endWrite(int i) { numWriters--; // ASSERT(numWriters==0) okToWrite = numWaitingReaders == 0; startWaitingReadersTime = age(); notifyAll(); }

End The Monitor Declaration } // End the class (just like any other)