COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors

Similar documents
C340 Concurrency: Semaphores and Monitors. Goals

Monitors & Condition Synchronization

Processes & Threads. Concepts: processes - units of sequential execution.

Chapter 2. Processes & Threads. Concurrency: processes & threads 1. Magee/Kramer

Lecture 9: Introduction to Monitors

CSCI 5828: Foundations of Software Engineering

Model Requirements and JAVA Programs MVP 2 1

CSCI 5828: Foundations of Software Engineering

Concurrent processes. Processes and Threads. Processes and threads. Going back to Concurrency. Modelling Processes. Modeling processes

Monitors & Condition Synchronization

Monitors & Condition Synchronisation

Monitors & Condition Synchronization

Communications Software Engineering Condition Synchronization and Liveness

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

Shared Objects & Mutual Exclusion

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

COMP30112: Concurrency Topics 5.2: Properties

Java Threads. COMP 585 Noteset #2 1

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

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Threads and Parallelism in Java

Part IV Other Systems: I Java Threads

CMSC 132: Object-Oriented Programming II

Concurrent processes. Processes and Threads. A question asked by your friend. Warm-up exercises at beginning of Lecture 3. My question for you

Multiple Inheritance. Computer object can be viewed as

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Concurrency 6 - Deadlock

CS11 Java. Fall Lecture 7

Problems with Concurrency. February 19, 2014

Threads Chate Patanothai

Software Practice 1 - Multithreading

Concurrency. Fundamentals of Computer Science

Plan for rest of course

Michele Van Dyne MUS 204B Concurrency Issues

CS18000: Programming I

Lecture 10: Introduction to Semaphores

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

Synchronization in Java

Multithreaded Programming

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

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

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

Concurrency. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Concurrency in Java Prof. Stephen A. Edwards

Concurrent Computing CSCI 201 Principles of Software Development

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

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

Le L c e t c ur u e e 7 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Multithreading

Exam Number/Code : 1Z Exam Name: Name: Java Standard Edition 6. Demo. Version : Programmer Certified Professional Exam.

Software Architecture

Multithreading Pearson Education, Inc. All rights reserved.

Chapter 6. Deadlock. DM519 Concurrent Programming

Programming Language Concepts: Lecture 11

Principles of Software Construction: Concurrency, Part 2

Multithreaded Programming

Unit - IV Multi-Threading

User Space Multithreading. Computer Science, University of Warwick

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

COMP346 Winter Tutorial 4 Synchronization Semaphores

Programming Java. Multithreaded Programming

Threads and Java Memory Model

7. MULTITHREDED PROGRAMMING

CS 556 Distributed Systems

04-Java Multithreading

Component-Based Software Engineering

CSCD 330 Network Programming

Module - 4 Multi-Threaded Programming

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

G52CON: Concepts of Concurrency

Lecture 7: Process & Thread Introduction

Multithreading. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

F. Tip and M. Weintraub CONCURRENCY

Animation Part 2: MoveableShape interface & Multithreading

CSCD 330 Network Programming

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

What is a thread anyway?

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

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

CS 351 Design of Large Programs Threads and Concurrency

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

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

Question Points Score Total 100

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

CS 159: Parallel Processing

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

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

COE518 Lecture Notes Week 7 (Oct 17, 2011)

Unit 5 - Exception Handling & Multithreaded

NITI NITI I PRIORITET

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

CONCURRENT PROGRAMMING - EXAM

Synchronization synchronization.

2018/2/5 话费券企业客户接入文档 语雀

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

MVP1: Introduction to concurrency in JAVA

Monitors CSCI 201 Principles of Software Development

Java Threads. Introduction to Java Threads

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

System Programming. Practical Session 4: Threads and Concurrency / Safety

Transcription:

COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors Howard Barringer Room KB2.20: email: Howard.Barringer@manchester.ac.uk February 2009

Outline Monitors FSP Models-to-Java Monitors Producers/Consumers

Outline Monitors FSP Models-to-Java Monitors Producers/Consumers

About Monitors passive shared objects: methods invoked by (active) threads

About Monitors passive shared objects: methods invoked by (active) threads synchronized methods to exclusively access private variables.

About Monitors passive shared objects: methods invoked by (active) threads synchronized methods to exclusively access private variables. Condition Synchronization: guarded execution:

About Monitors passive shared objects: methods invoked by (active) threads synchronized methods to exclusively access private variables. Condition Synchronization: guarded execution: looped guard-test containing wait()

About Monitors passive shared objects: methods invoked by (active) threads synchronized methods to exclusively access private variables. Condition Synchronization: guarded execution: looped guard-test containing wait() notify()/notifyall() after guard-state change

Synchronisation Demo - I class Monitor { private int x = 0; void addone(int n, int delay) throws InterruptedException { System.out.println("Thread " + n + ": x is " + x); int t = x + 1; System.out.println("Thread " + n + ": t is " + t); Thread.sleep(delay); x = t; System.out.println("Thread " + n + ": x is " + x);

Synchronisation Demo - I class Monitor { private int x = 0; synchronized void addone(int n, int delay) throws InterruptedException { System.out.println("Thread " + n + ": x is " + x); int t = x + 1; System.out.println("Thread " + n + ": t is " + t); Thread.sleep(delay); x = t; System.out.println("Thread " + n + ": x is " + x);

Synchronisation Demo - II class T extends Thread { Monitor obj; private int name, threaddelay; T(Monitor o, int n, int delay){ obj = o; name = n; threaddelay = delay; public void run(){ try {obj.addone(name, threaddelay); catch (InterruptedException e) {

Synchronisation Demo - III public class MonitorExample { public static void main(string [] args) throws InterruptedException { int delay = Integer.parseInt(args[0]); Monitor m = new Monitor(); T t1 = new T(m, 1, delay); T t2 = new T(m, 2, delay/2); T t3 = new T(m, 3, delay/3); t1.start(); t2.start(); t3.start(); Thread.sleep(10); m.addone(0, 0);

And what happens... without synchronisation $ java MonitorExample 20 Thread 1: x is 0 Thread 1: t is 1 Thread 2: x is 0 Thread 2: t is 1 Thread 3: x is 0 Thread 3: t is 1 Thread 3: x is 1 Thread 0: x is 1 Thread 0: t is 2 Thread 0: x is 2 Thread 2: x is 1 Thread 1: x is 1 $

And what happens... without synchronisation $ java MonitorExample 20 Thread 1: x is 0 Thread 1: t is 1 Thread 2: x is 0 Thread 2: t is 1 Thread 3: x is 0 Thread 3: t is 1 Thread 3: x is 1 Thread 0: x is 1 Thread 0: t is 2 Thread 0: x is 2 Thread 2: x is 1 Thread 1: x is 1 $ $ java MonitorExample 30 Thread 1: x is 0 Thread 1: t is 1 Thread 2: x is 0 Thread 2: t is 1 Thread 3: x is 0 Thread 3: t is 1 Thread 0: x is 0 Thread 0: t is 1 Thread 0: x is 1 Thread 3: x is 1 Thread 2: x is 1 Thread 1: x is 1 $

And what happens... with synchronisation $ java MonitorExample 20 Thread 1: x is 0 Thread 1: t is 1 Thread 1: x is 1 Thread 0: x is 1 Thread 0: t is 2 Thread 0: x is 2 Thread 3: x is 2 Thread 3: t is 3 Thread 3: x is 3 Thread 2: x is 3 Thread 2: t is 4 Thread 2: x is 4 $

And what happens... with synchronisation $ java MonitorExample 20 Thread 1: x is 0 Thread 1: t is 1 Thread 1: x is 1 Thread 0: x is 1 Thread 0: t is 2 Thread 0: x is 2 Thread 3: x is 2 Thread 3: t is 3 Thread 3: x is 3 Thread 2: x is 3 Thread 2: t is 4 Thread 2: x is 4 $ Thread 1 runs to completion Thread 0 runs to completion Thread 3 runs to completion Thread 2 runs to completion no matter what delay

Now with Conditional synchronisation.. class Monitor { private int x = 0; synchronized void addone(int n, int delay) throws InterruptedException { System.out.println("Thread " + n + ": x is " + x); int t = x + 1; System.out.println("Thread " + n + ": t is " + t); Thread.sleep(delay); x = t; System.out.println("Thread " + n + ": x is " + x);

Now with Conditional synchronisation.. class Monitor { private int x = 0; synchronized void addone(int n, int delay) throws InterruptedException { while (!(x==n)) wait(); System.out.println("Thread " + n + ": x is " + x); int t = x + 1; System.out.println("Thread " + n + ": t is " + t); Thread.sleep(delay); x = t; System.out.println("Thread " + n + ": x is " + x);

Now with Conditional synchronisation.. class Monitor { private int x = 0; synchronized void addone(int n, int delay) throws InterruptedException { while (!(x==n)) wait(); System.out.println("Thread " + n + ": x is " + x); int t = x + 1; System.out.println("Thread " + n + ": t is " + t); Thread.sleep(delay); x = t; notifyall(); System.out.println("Thread " + n + ": x is " + x);

And this is what happens... $ java MonitorExample 20 Thread 0: x is 0 Thread 0: t is 1 Thread 0: x is 1 Thread 1: x is 1 Thread 1: t is 2 Thread 1: x is 2 Thread 2: x is 2 Thread 2: t is 3 Thread 2: x is 3 Thread 3: x is 3 Thread 3: t is 4 Thread 3: x is 4 $

And this is what happens... $ java MonitorExample 20 Thread 0: x is 0 Thread 0: t is 1 Thread 0: x is 1 Thread 1: x is 1 Thread 1: t is 2 Thread 1: x is 2 Thread 2: x is 2 Thread 2: t is 3 Thread 2: x is 3 Thread 3: x is 3 Thread 3: t is 4 Thread 3: x is 4 $ We will always get: Thread 0 runs to completion Thread 1 runs to completion Thread 2 runs to completion Thread 3 runs to completion no matter what delays

Outline Monitors FSP Models-to-Java Monitors Producers/Consumers

FSP Models to Java Monitors FSP: when (guard) actiona -> P Java: synchronized type actiona (...) { while (!guard) { wait() ; // Code for process P When guard is changed, signal with notify() Active objects initiate actions: implement as Threads Passive (shared) objects respond to actions: implement as Monitors

Outline Monitors FSP Models-to-Java Monitors Producers/Consumers

Producers and Consumers: the concept Producers: produce items which are sent to consumer(s)

Producers and Consumers: the concept Producers: produce items which are sent to consumer(s) Consumers: receive items and process them independently

Producers and Consumers: the concept Producers: produce items which are sent to consumer(s) Consumers: receive items and process them independently synchronous or buffered communication

Booth Street East Car Park A Passive Object

FSP description CARPARKCONTROL(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). CARPARK = (ARRIVALS CARPARKCONTROL(4) DEPARTURES).

class CarParkControl { protected int spaces; protected int capacity; CarParkControl(int n) { capacity = spaces = n; Car Park Control Class synchronized void arrive() throws InterruptedException { while (spaces==0) wait(); --spaces; notifyall(); synchronized void depart() throws InterruptedException{ while (spaces==capacity) wait(); ++spaces; notifyall();

CountDown Timer - An Active Object COUNTDOWN (N=3) = ( start -> COUNTDOWN[N]), COUNTDOWN[i:0..N]= ( when (i>0) tick -> COUNTDOWN[i-1] when (i==0) beep -> STOP stop -> STOP ).

Java for CountDown Timer - I public class CountDown extends Applet implements Runnable { Thread counter; int i; final static int N = 3; AudioClip beepsound, ticksound; NumberCanvas display; public void init() {... // Applet methods public void start() {... // (don t confuse with public void stop() {... // Thread start/stop ) public void run() {... private void tick(){... private void beep(){... // required by Runnable; // called by Thread counter // local

Java for CountDown Timer - II public void start() { counter = new Thread(this); i = N; counter.start(); public void stop() { counter = null; // method start for Applet // method start for Thread counter // method stop for Applet // (not using Thread stop method) public void run() { while(true) { if (counter == null) return; if (i>0) { tick(); --i; if (i==0) { beep(); return;