CS 5704 Fall 00 1 Monitors in Java Model and Examples.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
Chapter 6: Process Synchronization
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 ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Slides for Chapter 6: Operating System support From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 3, © Addison-Wesley.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Multithreading.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
1 Java Threads and Synchronization Review Modified from slides taken from
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
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.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
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.
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.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Distributed and Parallel Processing George Wells.
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Threads in Java Two ways to start a thread
Multithreading / Concurrency
PROCESS MANAGEMENT IN MACH
Day 13 Concurrency.
Day 15 Concurrency.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Slides for Chapter 6: Operating System support
Multithreading.
Multithreading.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Critical section problem
Multithreading in java.
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Threads and Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

CS 5704 Fall 00 1 Monitors in Java Model and Examples

CS 5704 Fall 00 2 Threads A “thread” represents an independent activity animations (one thread is performing the animation while another thread is responding to the user) servers (separate threads are created for each client) is separately scheduled by the system can manipulated by the program coexists with other threads in the same address space

CS 5704 Fall 00 3 Motivations for Using Threads performance (on multiprocessor machines) simplicity (direct modeling of autonomous events) availability(unblocked threads can make progress) controllability (start/stop threads as needed) asynchrony (thread can block without halting program) required (by some Java services)

CS 5704 Fall 00 4 Classical Problems Reader-Writer Database Reader Writer Reader Producer - Consumer Buffer ProducersConsumers

CS 5704 Fall 00 5 Java as a Concurrency Programming Language Language features: threads class and synchronization constructs platform independent Libraries for basic network programming sockets/URL Remote Method Invocation (RMI) Used to implement distributed agent systems Aglets Voyager Odyssey

CS 5704 Fall 00 6 Java Threads Thread class: extends Object class implements Runnable interface Attributes of a thread: target - in what object it will begin execution name - for identification by program group - of which it is a member priority - for scheduling

CS 5704 Fall 00 7 Thread Basics Operations performed on a thread: start - begin execution stop - end execution (deprecated) suspend - await resumption (deprecated) resume - awake suspended thread (deprecated) interrupt - cause exception to be thrown Operations performed by a thread: sleep - dormant for specified time period yield - allow scheduler to select among ready threads join - await completion of another thread wait - delay until notified notify/notifyAll - awaken one/all waiting threads

CS 5704 Fall 00 8 Application-Specific Threads There are two ways of defining a thread to perform application specific activities by creating a class that: extends the Thread class using inheritance implements the Runnable interface In each case, the application specific class defines a run() method where execution of the thread begins.

CS 5704 Fall 00 9 Extending the Thread Class public class Worker extends Thread { … public void run() //defines where thread will begin { // code for worker } } public class Boss { private worker Worker; … public startWorker() { worker = new Worker(); worker.start(); } }

CS 5704 Fall Implementing the Runnable Interface public class Worker implements Runnable { … public void run() //defines where thread will begin { // code for worker } } public class Boss { private thread workerThread; … public startWorker() { Worker worker = new Worker(); workerThread = new Thread(worker); workerThread.start(); } }

CS 5704 Fall Forms of Synchronization mutual exclusion ­ preventing concurrent access to shared objects to preserve the consistency of the object condition synchronization ­ blocking attempted operations on a shared object until that object is in a state where the operation will preserve the consistency of the object (Monitor model)

CS 5704 Fall Mutual Exclusion The synchronized keyword can be used to: provide mutual exclusion among methods of the same object provide mutually exclusive ownership of an object Mutual exclusion is needed to insure the consistency of the state of objects that can be accessed by multiple threads.

CS 5704 Fall Synchronized Methods public class Value { private int current; synchronized public void increment() { current = current + 1; } synchronized public void decrement() { current = current - 1; } public int current() { return current } }

CS 5704 Fall Mutually Exclusive Ownership synchronized(worker) { // access and output // worker information } synchronized(worker) { // update worker // information } Employee worker; // shared object Thread 2 Thread 1

CS 5704 Fall Monitors in Java every object has a single lock and a waiting queue synchronized methods acquire the lock before executing the wait() operation suspends the executing thread on the object’s waiting queue and releases the object’s lock the notify() operation awakens exactly one thread suspended on the object’s waiting queue but does not release the lock the notifyAll() operation awakens all threads suspended on the object’s waiting queue but does not release the lock wait, notify, and notifyAll must be in synchronized methods awakened threads must reacquire the lock before continuing

CS 5704 Fall Structure of a Java Monitor object Synchronized method acquire call return release lock lock wait queue

CS 5704 Fall wait() Operation object Synchronized method acquire call wait() release waiting queue lock

CS 5704 Fall notify() Operation object Synchronized method acquire call notify() awaken waiting queue acquire lock

CS 5704 Fall notifyAll() Operation object Synchronized method acquire call notify() awaken waiting queue acquire lock

CS 5704 Fall Generic Monitor Code public class MonitorClass {... // private data public synchronized void enter(…) {... while ( ! condition ) // test for desired condition { wait(); // block execution... // continue here when notified } } public synchronized void change(…) {... // change the object’s state notify(); // unblock any single waiter... notifyAll(); // unblock all waiters } }

CS 5704 Fall Semaphore Example // file Semaphore.java // note: some details missing public class Semaphore { private int count; public Semaphore(int initial) { count = initial; } synchronized public void P() { count = count - 1; if(count < 0) wait(); } synchronized public void V() { count = count + 1; if (count <= 0) notify(); }

CS 5704 Fall Readers-Writers Example public class ReadersWriters //some details missing { //see Wait Exceptions private boolean writing; private int readers; public ReadersWriters() { writing = false; readers = 0; } public synchronized void startRead() { while (writing) wait(); readers = readers + 1; } public synchronized void endRead() { readers = readers - 1; if (readers == 0) notify(); } public synchronized void startWrite() { while (writing || readers > 0) wait(); } writing = true; } public synchronized void endWrite() { writing = false; notifyAll(); }

CS 5704 Fall Wait Exceptions public class MonitorClass { public synchronized void enter(…) {... while ( ! condition ) // test for desired condition { try { wait(); } // block execution catch(InterruptedException(ie) {... } // handle wait exception... // continue here when notified } }... } The wait operation is defined to return an exception if the wait is terminated abnormally. So the wait must be written as follows.

CS 5704 Fall Timed Waits public class Resource { private boolean available = true;... public synchronized boolean timedAcquire(int maxTime) { if (! available) { try { wait(maxTime); } catch(InterruptedException ex) { return false; } } available = false; return true; }... } The time to wait for a synchronization condition can be bounded.