Lecture 9: Introduction to Monitors

Similar documents
Monitors & Condition Synchronization

COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors

Monitors & Condition Synchronisation

Monitors & Condition Synchronization

C340 Concurrency: Semaphores and Monitors. Goals

Monitors & Condition Synchronization

CSCI 5828: Foundations of Software Engineering

CSCI 5828: Foundations of Software Engineering

Q1. From post-it note. Question asked after Prev. Lecture. thread life-cycle in Java. Q2. Concurrency and parallelism. Definitions

Lecture 10: Introduction to Semaphores

Programming Language Concepts: Lecture 11

Shared Objects & Mutual Exclusion

Shared Objects & Mutual Exclusion

Shared Objects & Mutual Exclusion

COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall

Lecture 7: Process & Thread Introduction

CS11 Java. Fall Lecture 7

Problems with Concurrency. February 19, 2014

Threads Chate Patanothai

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

CS 159: Parallel Processing

Java Threads. COMP 585 Noteset #2 1

Communications Software Engineering Condition Synchronization and Liveness

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Java Threads. Written by John Bell for CS 342, Spring 2018

Threads and Parallelism in Java

Concurrent Programming Lecture 10

Faculty of Computers & Information Computer Science Department

Concurrent Programming using Threads

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

CMSC 330: Organization of Programming Languages

Multi-threaded programming in Java

Java Threads. Introduction to Java Threads

Lecture 17: Sharing Objects in Java

Writing Parallel Programs COMP360

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Lecture 8: September 30

Answer the first question and two further questions. i. action prefix ( -> ) [3 marks]

Introduction to Java Threads

Concurrency in Java Prof. Stephen A. Edwards

Multiple Inheritance. Computer object can be viewed as

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

What's wrong with Semaphores?

Contents. 6-1 Copyright (c) N. Afshartous

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

CS18000: Programming I

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Models of concurrency & synchronization algorithms

Synchronization in Java

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

What is a thread anyway?

Programming Language Concepts: Lecture 13

Lecture 21: Concurrency in Other Environments (Part 2)

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Part IV Other Systems: I Java Threads

Chapter 32 Multithreading and Parallel Programming

Project. Threads. Plan for today. Before we begin. Thread. Thread. Minimum submission. Synchronization TSP. Thread synchronization. Any questions?

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

CMSC 132: Object-Oriented Programming II

MVP1: Introduction to concurrency in JAVA

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

Component-Based Software Engineering

Java s Implementation of Concurrency, and how to use it in our applications.

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Chapter 6. Deadlock. DM519 Concurrent Programming

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

G52CON: Concepts of Concurrency

Programming in Parallel COMP755

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Advanced Concepts of Programming

CS 351 Design of Large Programs Threads and Concurrency

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

COMP346 Winter Tutorial 4 Synchronization Semaphores

Multithreaded Programming

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Software Architecture

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

COMP30112: Concurrency Topics 5.2: Properties

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

COMP 213. Advanced Object-oriented Programming. Lecture 23. Shared Variables and Synchronization

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Chapter 10. Message Passing. Concurrency: message passing 1. Magee/Kramer

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

Threads and Java Memory Model

Concurrent and Real-Time Programming in Java

CmpSci 187: Programming with Data Structures Spring 2015

Transcription:

COMP 150-CCP Concurrent Programming Lecture 9: Introduction to Monitors Dr. Richard S. Hall rickhall@cs.tufts.edu Concurrent programming February 14, 2008

Abstracting Locking Details Recall our discussion of abstracting details const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u]->var[u] write[v:t]->var[v]). We can hide the locking details of a shared resource by hiding its internal actions and only exposing the desired external actions (e.g., similar to public methods in an object) LOCK = (acquire->release->lock). INCREMENT = (acquire->read[x:t] ->(when (x<n) write[x+1] ->release->increment->increment ) )+{read[t],write[t]. COUNTER = (INCREMENT LOCK VAR)@{increment.

Abstraction Simplifies Model Minimized LTS for synchronized COUNTER process A simpler process that also describes a synchronized counter COUNTER = COUNTER[0] COUNTER[v:T] = (when (v<n) increment ->COUNTER[v+1]). This process generates the same LTS as the previous COUNTER definition, thus they describe the same atomic increment behavior

Benefits of Abstracted Model Encapsulates state The counter variable is no longer directly accessible Exposes only the allowable actions COUNTER = COUNTER[0] COUNTER[v:T] = (when (v<n) increment ->COUNTER[v+1]). In this case, the increment action Guarantees mutually exclusive access This is the definition of a monitor

Monitor Concept A monitor is a high-level data abstraction mechanism for mutual exclusion Monitors encapsulate state Monitors provide operations to access and modify their state These operations are the only means to modify the state Monitors guarantee mutual exclusion among operations Only one operation can execute at a time, thus the operation has exclusive access to the state Monitors sound very similar to what?

Monitors as Java Classes Classes in Java are data abstractions, just like monitors are a data abstractions It is possible to implement a monitor using a Java class by following two simple rules All data members must be declared private All methods that access data members must be declared synchronized Why is this high-level? Because someone using the data encapsulated in the monitor does not need to worry about mutual exclusion issues at all i.e., no locking protocol to worry about

Monitor Example public class Counter { private int MAX = 5; private int count = 0; public Counter(int max) { MAX = max; public synchronized void increment() { if (count < MAX) count++; public synchronized void decrement() { if (count > 0) count--; public synchronized int getcount() { return count; What are the semantics of this counter?

Counter Monitor Example The counter in this example may ignore increments or decrements if the count is at the maximum or minimum, respectively. How do can we not ignore increments or decrements?

Naïve Counter Solution 1 // Shared counter object counter = new Counter(MAX);... // Try to make sure increment is not ignored while (counter.getcount() >= MAX) { // Wait until is decremented. counter.increment();... This fails because it is not atomic. Even if it did work, it forces users of Counter to do additional work (i.e., another protocol to remember).

Naïve Counter Solution 2... // Modify Counter public synchronized void increment() { while (count >= MAX) { // Wait until count is decremented count++; public synchronized void decrement() { while (count <= 0) { // Wait until count is incremented count--;... This does not work, since the thread holds the lock while waiting; thus, no other thread can perform the needed operation.

Naïve Counter Solution 3... // Modify Counter public void increment() { while (true) { synchronized (this) { if (count < MAX) { count++; break; // Free the lock and wait a little while. Thread.currentThread().sleep(100);... This works, but it is not very efficient.

Condition Variable Concept Monitors are usually not used alone, but are combined with a low-level synchronization mechanism, called condition variables (also referred to as condition synchronization) Condition variables Offer wait and notify operations, both can only be called from inside a monitor This means that in order to use these operations, the caller must own the monitor lock! When a process invokes wait on a condition variable, it gives up the lock and is suspended until another process performs a notify on the condition variable Each condition variable has a waiting queue that can have any number of processes waiting on it

Condition Variables in Java In Java, every object has a lock and condition variable associated with it, inherited from java.lang.object. public final void wait() throws InterruptedException Calling thread waits to be notified by another thread. The waiting thread releases the lock associated with the monitor. When notified, the thread must wait to reacquire the monitor lock before resuming execution. public final void notify() Wakes up a single thread that is waiting on this object's queue. public final void notifyall() Wakes up all threads that are waiting on this object's queue.

Condition Variables in FSP and Java FSP when (cond) act -> NEWSTAT Java public synchronized void act() throws InterruptedException { while (!cond) wait(); // modify monitor data notifyall(); The while loop in Java is necessary to re-test the wait condition to ensure that it is indeed satisfied when the thread re-enters the monitor. notifyall() is used to awaken other threads that may be waiting on the object instance's condition variable wait queue.

Blocked and Waiting Threads in Java If a thread is unable to enter a synchronized method because another threads owns the object's lock, then this thread is said to be blocked Blocking and unblocking of threads is performed transparently, we do not worry about this If a thread owns an object's lock and calls wait() on that object, then that thread is said to be waiting on the object's wait queue Adding and removing threads from the wait queue is specifically handled by the program using a combination of wait()/notify()/notifyall() calls

Condition Variables in FSP and Java Counter that does not ignore increments and decrements public class StrictCounter extends Counter {... public synchronized void increment() { while (getcount() >= MAX) { try { wait(); catch (InterruptedException ex) { super.increment(); notifyall(); public synchronized void decrement() { while (getcount() <= 0) { try { wait(); catch (InterruptedException ex) { super.decrement(); notifyall();

Parking Lot Example Arrivals Departures A controller is required for a parking lot, which only permits cars to enter when the lot is not full and does not permit cars to leave when there are no cars in the lot.

Modeling the Parking Lot Actions of interest arrive and depart Processes of interest ARRIVALS, DEPARTURES, and CONTROL Define processes and interactions (structure) PARKING LOT arrive depart ARRIVALS CONTROL DEPARTURES

Parking Lot Model CONTROL(N=4) = SPACES[N], SPACES[i:0..N] = (when(i>0) arrive->spaces[i-1] when(i<n) depart->spaces[i+1]). ARRIVALS = (arrive->arrivals). DEPARTURES = (depart->departures). PARKINGLOT = (ARRIVALS CONTROL(4) DEPARTURES). Guarded actions are used to control arrive and depart.

Parking Lot Roles In the FSP model all entities are processes interacting by actions For the program we need to identify threads and monitors Process - active entity which initiates actions Monitor - passive entity which responds to actions This is easy in the case of the car park... PARKING LOT arrive depart ARRIVALS CONTROL DEPARTURES Process Monitor Process

Parking Lot Program Arrivals and Departures implement Runnable, Control provides the control (condition synchronization). Instances of these are created by the main() method of the ParklingLot class, which also creates a simple GUI for starting/stopping our threads. public static void main(string[] args) { Control control = new Control(5); Arrivals arrivals = new Arrivals(control); Departures departures = new Departures(control); new Thread(arrivals, "Arrivals").start(); new Thread(departures, "Departures").start(); ParkingLot lot = new ParkingLot(arrivals, departures); lot.setvisible(true);

Parking Lot Arrivals Thread public class Arrivals implements Runnable { private Control control; private boolean paused = true; public Arrivals(Control c) { control = c; public synchronized void pause() { paused =!paused; notify(); public void run() { try { while (true) { synchronized (this) { while (paused) { wait(); control.arrive(); Thread.sleep(500); catch (InterruptedException e) { How do we implement Control? Departures works similarly, except it calls depart()

Parking Lot Control Monitor public class Control { private int spaces; private int capacity; public Control(int n) { capacity = spaces = n; public synchronized void arrive() throws InterruptedException { while (spaces == 0) { System.out.println("No spaces available, waiting..."); wait(); --spaces; System.out.println("Car parked (spaces = " + spaces + ")"); notify(); public synchronized void depart() throws InterruptedException { while (spaces == capacity) { System.out.println("No cars, waiting..."); wait(); ++spaces; System.out.println("Car departed (spaces = " + spaces + ")"); notify(); Why is it safe to use notify() here rather than notifyall()?

Summary: Model to Java Active entities (that initiate actions) are implemented as threads. Passive entities (that respond to actions) are implemented as monitors. Each guarded action in the model of a monitor is implemented as a synchronized method which uses a while loop and wait() to implement the guard. The while loop condition is the negation of the model guard condition. Changes in the state of the monitor are signaled to waiting threads using notify() or notifyall().