Programming Language Concepts: Lecture 11

Similar documents
Programming Language Concepts: Lecture 13

Lecture 9: Introduction to Monitors

Threads Chate Patanothai

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

Introduction to Java Threads

7. MULTITHREDED PROGRAMMING

Advanced Concepts of Programming

Java Threads. COMP 585 Noteset #2 1

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

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

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

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

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

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

Monitors; Software Transactional Memory

What is a thread anyway?

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

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

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Concurrent Programming using Threads

Threads and Parallelism in Java

Synchronization in Java

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

Multithread Computing

Problems with Concurrency. February 19, 2014

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

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Object Oriented Programming (II-Year CSE II-Sem-R09)

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

Multiple Inheritance. Computer object can be viewed as

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

Synchronization

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

Multithreaded Programming

CS18000: Programming I

Shared Objects & Mutual Exclusion

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

CS 351 Design of Large Programs Threads and Concurrency

Unit - IV Multi-Threading

Multithreaded Programming

What's wrong with Semaphores?

CMSC 132: Object-Oriented Programming II. Threads in Java

Chapter 32 Multithreading and Parallel Programming

CS11 Java. Fall Lecture 7

Object Oriented Programming. Week 10 Part 1 Threads

Robotics and Autonomous Systems

Only one thread can own a specific monitor

CS 159: Parallel Processing

THREADS AND MULTITASKING ROBOTS

ROBOTICS AND AUTONOMOUS SYSTEMS

Animation Part 2: MoveableShape interface & Multithreading

Module - 4 Multi-Threaded Programming

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

Lecture 8: September 30

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

Java Threads. Introduction to Java Threads

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

Faculty of Computers & Information Computer Science Department

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

public class Shared0 { private static int x = 0, y = 0;

CS 556 Distributed Systems

Monitors; Software Transactional Memory

CS180 Review. Recitation Week 15

Info 408 Distributed Applications Programming Exercise sheet nb. 4

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

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

Exercise Session Week 8

Models of concurrency & synchronization algorithms

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST)

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

Exercise Session Week 8

Synchronized Methods of Old Versions of Java

Basics of. Multithreading in Java

Concurrent Programming

Parallel Programming Practice

Monitors & Condition Synchronization

COMP 401 THREAD COORDINATION. Instructor: Prasun Dewan

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

Parallel Programming Practice

CSCD 330 Network Programming

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

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

CST242 Concurrency Page 1

COE518 Lecture Notes Week 7 (Oct 17, 2011)

i219 Software Design Methodology 11. Software model checking Kazuhiro Ogata (JAIST) Outline of lecture

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

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

Concurrent Programming Benoît Garbinato

COMP346 Winter Tutorial 4 Synchronization Semaphores

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

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

Note: in this document we use process and thread interchangeably.

Multi-threading in Java. Jeff HUANG

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

Component-Based Software Engineering

Unit III Rupali Sherekar 2017

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

Transcription:

Programming Language Concepts: Lecture 11 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in PLC 2011, Lecture 11, 01 March 2011

Concurrent Programming Monitors [Per Brinch Hansen, CAR Hoare] Attach synchronization control to the data that is being protected Monitor is like a class in an OO language Data definition to which access is restricted across threads Collections of functions operating on this data all are implicitly mutually exclusive Monitor guarantees mutual exclusion if one function is active, any other function will have to wait for it to finish

Monitors monitor bank_account{ double accounts[100]; boolean transfer (double amount, int source, int target){ // transfer amount accounts[source] -> accounts[target] if (accounts[source] < amount){ return false; accounts[source] -= amount; accounts[target] += amount; return true; double audit(){ // compute the total balance across all accounts double balance = 0.00; for (int i = 0; i < 100; i++){ balance += accounts[i]; return balance;

Monitors... transfer(500.00,i,j); transfer(400.00,j,k); Mechanism for a thread to suspend itself and give up the monitor A suspended process is waiting for monitor to change its state Separate internal queue, as opposed to external queue where initially blocked threads wait Dual operation to wake up suspended processes

Monitors... boolean transfer (double amount, int source, int target){ while (accounts[source] < amount){ wait(); accounts[source] -= amount; accounts[target] += amount; notify(); return true; What happens when a process executes notify()? Signal and exit notifying process immediately exits the monitor Signal and wait notifying process swaps roles and goes into the internal queue of the monitor Signal and continue notifying process keeps control till it completes and then one of the notified processes steps in

Monitors... Makes sense to have more than one internal queue monitor bank_account{ double accounts[100]; queue q[100]; // one internal queue for each account boolean transfer (double amount, int source, int target){ while (accounts[source] < amount){ q[source].wait(); // wait in the queue associated with source accounts[source] -= amount; accounts[target] += amount; q[target].notify(); // notify the queue associated with target return true;

Monitors in Java Java implements monitors with a single internal queue Monitors incorporated within existing class definitions

Monitors in Java Java implements monitors with a single internal queue Monitors incorporated within existing class definitions Function declared synchronized is to be executed atomically Trying to execute a synchronized function while another is in progress blocks the second thread into an external queue

Monitors in Java Java implements monitors with a single internal queue Monitors incorporated within existing class definitions Function declared synchronized is to be executed atomically Trying to execute a synchronized function while another is in progress blocks the second thread into an external queue Each object has a lock To execute a synchronized method, thread must acquire lock Thread gives up lock when the method exits Only one thread can have the lock at any time

Monitors in Java Java implements monitors with a single internal queue Monitors incorporated within existing class definitions Function declared synchronized is to be executed atomically Trying to execute a synchronized function while another is in progress blocks the second thread into an external queue Each object has a lock To execute a synchronized method, thread must acquire lock Thread gives up lock when the method exits Only one thread can have the lock at any time wait() and notify() to suspend and resume notify() signals one (arbitrary) waiting process notifyall() signals all waiting processes Java uses signal and continue

Monitors in Java... public class bank_account{ double accounts[100]; public synchronized boolean transfer (double amount, int source, int target){ while (accounts[source] < amount){ wait(); accounts[source] -= amount; accounts[target] += amount; notifyall(); return true; public synchronized double audit(){ double balance = 0.0; for (int i = 0; i < 100; i++){ balance += accounts[i]; return balance; public double current_balance(int i){ // not synchronized! return accounts[i];

Object locks Every object has a lock in Java Can synchronize arbitrary blocks of code public class XYZ{ Object o = new Object(); public int f(){.. synchronized(o){... public double g(){.. synchronized(o){...

Object locks Every object has a lock in Java Can synchronize arbitrary blocks of code public class XYZ{ Object o = new Object(); public int f(){.. synchronized(o){... public double g(){.. synchronized(o){... f() and g() can start in parallel Only one of the threads can grab the lock for o

Object locks... Each object has its own internal queue Object o = new Object(); public int f(){.. synchronized(o){... o.wait(); // Wait in queue attached to "o"... public double g(){.. synchronized(o){... o.notifyall();... // Wake up queue attached to "o"

Object locks... Can convert methods from externally synchronized to internally synchronized public double h(){ synchronized(this){... Anonymous wait(), notify(), notifyall() abbreviate this.wait(), this.notify(), this.notifyall()

Object locks... Actually, wait() can be interrupted by an InterruptedException Should write try{ wait(); catch (InterruptedException e) {... ;

Object locks... Actually, wait() can be interrupted by an InterruptedException Should write try{ wait(); catch (InterruptedException e) {... ; Error to use wait(), notify(), notifyall() outside synchronized method IllegalMonitorStateException Likewise, use o.wait(), o.notify(), o.notifyall() only in block synchronized on o

Java threads Have a class extend Thread Define a function run() where execution can begin in parallel public class Parallel extends Thread{ private int id; public Parallel(int i){ id = i; public void run(){ for (int j = 0; j < 100; j++){ System.out.println("My id is "+id); try{ sleep(1000); // Go to sleep for 1000 ms catch(interruptedexception e){

Java threads... Invoking threads public class TestParallel { public static void main(string[] args){ Parallel p[] = new Parallel[5]; for (int i = 0; i < 5; i++){ p[i] = new Parallel(i); p[i].start(); // Start off p[i].run() // in concurrent thread

Java threads... Invoking threads public class TestParallel { public static void main(string[] args){ Parallel p[] = new Parallel[5]; for (int i = 0; i < 5; i++){ p[i] = new Parallel(i); p[i].start(); // Start off p[i].run() // in concurrent thread p[i].start() initiates p[i].run() in a separate thread Directly calling p[i].run() does not execute in separate thread!

Java threads... sleep(...) is a static function in Thread Argument is time to sleep, in milliseconds Use Thread.sleep(...) if current class does not extend Thread sleep(..) throws InterruptedException (like wait())

Java threads... Cannot always extend Thread Single inheritance Instead, implement Runnable public class Parallel implements Runnable{ // only this line // has changed private int id; public Parallel(int i){... // Constructor public void run(){...

Java threads... To use Runnable class, must explicitly create a Thread and start() it public class TestParallel { public static void main(string[] args){ Parallel p[] = new Parallel[5]; Thread t[] = new Thread[5]; for (int i = 0; i < 5; i++){ p[i] = new Parallel(i); t[i] = new Thread(p[i]); // Make a thread t[i] from p[i] t[i].start(); // Start off p[i].run() concurrently // Note: t[i].start(), not p[i].start()

Life cycle of a Java thread A thread can be in four states New: Created but not start()ed. Runnable: start()ed and ready to be scheduled. Need not be actually running No guarantee made about how scheduling is done Most Java implementations use time-slicing Blocked: not available to run Within sleep(..) unblocked when sleep timer expires Suspended by wait() unblocked by notify() or notfifyall(). Blocked on input/output unblocked when the i/o succeeds. Dead: thread terminates.

Interrupts One thread can interrupt another using interrupt() p[i].interrupt(); interrupts thread p[i] Raises InterruptedException within wait(), sleep()

Interrupts One thread can interrupt another using interrupt() p[i].interrupt(); interrupts thread p[i] Raises InterruptedException within wait(), sleep() No exception raised if thread is running!

Interrupts One thread can interrupt another using interrupt() p[i].interrupt(); interrupts thread p[i] Raises InterruptedException within wait(), sleep() No exception raised if thread is running! interrupt() sets a status flag interrupted() checks interrupt status and clears the flag Detecting an interrupt while running or waiting public void run(){ try{ j = 0; while(!interrupted() && j < 100){ System.out.println("My id is "+id); sleep(1000); // Go to sleep for 1000 ms j++; catch(interruptedexception e){

Interrupts Check another thread s interrupt status using interrupted t.isinterrupted() to check status of t s interrupt flag Does not clear flag isalive() checks running status of a thread t.isalive() is true if t is Runnable or Blocked t.isalive() is false if t is New or Dead Can also stop(), suspend() and resume() a thread, but should not!

An exercise in concurrent programming A narrow North-South bridge can accommodate traffic only in one direction at a time. When a car arrives at the bridge 1. Cars on the bridge going in the same direction can cross 2. No other car on the bridge can cross (implicitly sets direction) 3. Cars on the bridge going in the opposite direction wait for the bridge to be empty Cars waiting to cross from one side may enter bridge in any order after direction switches in their favour. When bridge becomes empty and cars are waiting, yet another car can enter in the opposite direction and makes them all wait some more.

An example... Design a class Bridge to implement consistent one-way access for cars on the highway synchronization primitives Should permit multiple cars to be on the bridge at one time (all going in the same direction!) Bridge has a public method public void cross(int id, boolean d, int s) id is identity of car d indicates direction true is North false is South s indicates time taken to cross (milliseconds)

An example... public void cross(int id, boolean d, int s) Method cross prints out diagnostics 1. A car is stuck waiting for the direction to change Car 7 going North stuck at Thu Mar 13 23:00:11 IST 2009 2. The direction changes Car 5 switches bridge direction to North at Thu Mar 13 23:00:14 IST 2009 3. A car enters the bridge. Car 8 going North enters bridge at Thu Mar 13 23:00:14 IST 2009 4. A car leaves the bridge. Car 16 leaves at Thu Mar 13 23:00:15 IST 2009 Use java.util.date to generate time stamps

Analysis The data that is shared is the Bridge State of the bridge is represented by two quantities Number of cars on bridge an int Current direction of bridge a boolean The method public void cross(int id, boolean d, int s) changes the state of the bridge Concurrent execution of cross can cause problems...... but making cross a synchronized method is too restrictive Only one car on the bridge at a time Problem description explicitly disallows such a solution

Analysis... Break up cross into a sequence of actions enter get on the bridge travel drive across the bridge leave get off the bridge enter and leave can print out the diagnostics required Which of these affect the state of the bridge? enter : increment number of cars, perhaps change direction leave : decrement number of cars Make enter and leave synchronized travel is just a means to let time elapse use sleep

Analysis... Code for cross public void cross(int id, boolean d, int s){ // Get onto the bridge (if you can!) enter(id,d); // Takes time to cross the bridge try{ Thread.sleep(s); catch(interruptedexception e){ // Get off the bridge leave(id);

Analysis... Entering the bridge If the direction of this car matches the direction of the bridge, it can enter If the direction does not match but the number of cars is zero, it can reset the direction and enter Otherwise, wait() for the state of the bridge to change In each case, print a diagnostic message

Code for enter private synchronized void enter(int id, boolean d){ Date date; // While there are cars going in the wrong direction while (d!= direction && bcount > 0){ date = new Date(); System.out.println("Car "+id+" going "+ direction_name(d)+" stuck at "+date); // Wait for our turn try{ wait(); catch (InterruptedException e){...

Code for enter private synchronized void enter(int id, boolean d){... while (d!= direction && bcount > 0){... wait()...... // Switch direction, if needed if (d!= direction){ direction = d; date = new Date(); System.out.println("Car "+id+" switches bridge direction to "+direction_name(direction)+" at "+date); // Register our presence on the bridge bcount++; date = new Date(); System.out.println("Car "+id+" going "+direction_name(d)+" enters bridge at "+date);

Analysis... Leaving the bridge is much simpler Decrement the car count notify() waiting cars... provided car count is zero private synchronized void leave(int id){ Date date = new Date(); System.out.println("Car "+id+" leaves at "+date); // "Check out" bcount--; // If everyone on the bridge has checked out, notify the // cars waiting on the opposite side if (bcount == 0){ notifyall();