Concurrent Programming Benoît Garbinato

Similar documents
CMSC 433 Programming Language Technologies and Paradigms Fall Locks & Conditions

COMP 322: Fundamentals of Parallel Programming

What is a thread anyway?

Concurrency Utilities: JSR-166

G52CON: Concepts of Concurrency

What is a Thread? Why Multicore? What is a Thread? But a fast computer runs hot THREADS AND CONCURRENCY. Concurrency (aka Multitasking)

Multi-Threaded Programming Design CSCI 201 Principles of Software Development

Dr.-Ing. Michael Eichberg

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

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

Dr.-Ing. Michael Eichberg

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

Threads Questions Important Questions

Multi-threading in Java. Jeff HUANG

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

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Threads, Concurrency, and Parallelism

CMSC 330: Organization of Programming Languages

Producer/Consumer CSCI 201 Principles of Software Development

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

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

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

THREADS AND CONCURRENCY

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

Threads and Parallelism in Java

Concurrency in Object Oriented Programs 3. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Chapter 32 Multithreading and Parallel Programming

Teaching predicates and invariants on shared data structures in concurrent programming

THREADS & CONCURRENCY

CMSC 330: Organization of Programming Languages

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

-Aditya Bhave CSCI-5448 Graduate Presentation

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

Buffer & File Optimization. Cloud Databases DataLab CS, NTHU

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

Multiple Inheritance. Computer object can be viewed as

Synchronized Methods of Old Versions of Java

Concurrent Programming using Threads

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

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

Race Conditions & Synchronization

Supporting Distributed Active Objects: A Virtual Machine for Creol on the Java Platform. Master thesis. Ivar Alm

Programming Language Concepts: Lecture 11

Lecture 29: Java s synchronized statement

Threads & Concurrency

Threads & Concurrency

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

Synchronization COMPSCI 386

Principles of Software Construction: Concurrency, Part 2

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

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

CS193k, Stanford Handout #8. Threads 3

Performance Throughput Utilization of system resources

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

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

Part IV Other Systems: I Java Threads

Java Threads. Introduction to Java Threads

Threads Chate Patanothai

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

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

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Concurrency in Java Prof. Stephen A. Edwards

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

RACE CONDITIONS AND SYNCHRONIZATION

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

Java Concurrency. Towards a better life By - -

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

CS 351 Design of Large Programs Threads and Concurrency

CS11 Java. Fall Lecture 7

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Concurrent Programming

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

Buch gefunden? Im Blackboard eingetragen? Abgabe 1. Übung: , 23:59

Northeastern University - Seattle CS5510 Professor Ian Gorton

Condition Synchronization

Animation Part 2: MoveableShape interface & Multithreading

Models of concurrency & synchronization algorithms

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

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

THREADS & CONCURRENCY

7.1 Processes and Threads

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

7. MULTITHREDED PROGRAMMING

CS180 Review. Recitation Week 15

Lecture 9: Introduction to Monitors

The New Java Technology Memory Model

CS 318 Principles of Operating Systems

Programming in Java

Advanced Concepts of Programming

Critical Section Problem: Example

Introduction to Java Threads

Software Practice 1 - Multithreading

Synchronization in Java

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

Programming in Parallel COMP755

Java Technologies. Lecture VII. Valdas Rapševičius

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

Curs 5. Programare Paralela si Distribuita. Forme de sincronizare - Java. Curs 5 - PPD 1

Transcription:

Concurrent Programming Benoît Garbinato 1 Processes & threads A process is a unit of execution managed at the level of the operating system Each process has its own address space, i.e., no other process can access it A thread is a sequential flow of control executing within a process All threads within a process share the same address space, i.e., they share memory 2

Concurrency & parallelism concurrent applications concurrent operating system operating system parallel computer hardware (CPUs) 3 Concurrency & parallelism concurrent applications concurrent operating system operating system parallel computer hardware (CPUs) 4

Pseudo vs. Quasi Parallelism With pseudo-parallelism, a thread can be interrupted by the system at any time (we say that the system is preemptive) With quasi-parallelism, a thread can only be interrupted voluntarily, either explicitly or when it performs a input/ output system call 5 Liveness & safety Safety : nothing bad ever happens In object-oriented terms, this means that the state of no object will ever be corrupted Liveness : something good eventually happens In object-oriented terms, this means that the state of some object will eventually change 6

Creating threads in java Extending Thread Implementing Runnable public class PrimeThread extends Thread { long minprime; public PrimeThread(long minprime) { this.minprime= minprime; // Compute prime larger than minprime... public class PrimeRun implements Runnable { long minprime; public PrimeRun(long minprime) { this.minprime= minprime; // Compute prime larger than minprime... public static void main(string[] args) { PrimeThread pt= new PrimeThread(7); pt.start(); public static void main(string[] args) { PrimeRun pr= new PrimeRun(7); new Thread(pr).start(); 7 Mutual exclusion The readers/writers problem is a typical mutual exclusion problem: no reader should be allowed to read while a writer is writing no writer should be allowed to write while either another writer is writing or a reader is reading 8

public class Data { private String name; private String phone; Readers/Writers public class Reader extends Thread { public Data data; critical resources public void setname(string name) { this.name= name; public String getname() { return name; public void setphone(string phone) { this.phone= phone; public String getphone() { return phone; data= new Data(); w1= new Writer(data, "James", "007"); w2= new Writer(data, "Devil", "666"); r1= new Reader(data); r1.start(); w1.start(); w2.start(); public class Writer extends Thread { private Data data; private String name; private String phone; critical section public Reader(Data data) { this.data= data; while (true) { System.out.print(data.getName()); System.out.println(data.getPhone()); public Writer(Data data, String name, String phone) { this.data= data; this.name= name; this.phone= phone; while (true) { data.setname(name); data.setphone(phone); critical section 9 The concept of monitor A monitor is associated with an object to......ensure mutual exclusion of its methods...explicitly suspend or wake up threads using that object In Java, each object has an associated monitor You have two ways to express mutual exclusion in Java: At the method level At the object level synchronized public void setdata(string name, String phone) { this.name= name; this.phone= phone; synchronized (data) { name= data.getname(); phone= data.getphone(); 10

Readers/Writers revisited public class Data { private String name; private String phone; public String getname() { return name; public String getphone() { return phone; synchronized public void setdata(string name, String phone) { this.name= name; this.phone= phone; public class Reader extends Thread { public Data data; public Reader(Data data) { this.data= data; while (true) { synchronized (data) { System.out.print(data.getName()); System.out.print(data.getPhone()); data= new Data(); w1= new Writer(data, "James", "007"); w2= new Writer(data, "Devil", "666"); r1= new Reader(data); r1.start(); w1.start(); w2.start(); public class Writer extends Thread { private Data data; private String name; private String phone; public Writer(Data data, String name, String phone) { this.data= data; this.name= name; this.phone= phone; while (true) data.setdata(name,phone); 11 Waiting & notifying A monitor is associated to an object to explicitly suspend or wake up threads using that object. public class Object {... public final void wait() {... public final void notify() {... public final void notifyall() {...... 12

Using wait() and notify() public static void main(string[] args) { final Object wham= new Object(); Runnable singer= new Runnable() { try { for (int i= 1; i<=100; i++) synchronized (wham) { wham.wait(); catch (InterruptedException e) { ; Thread george= new Thread(singer, "George"); Thread andrew= new Thread(singer, "Andrew"); george.start(); andrew.start(); int i= 0; while (george.isalive() andrew.isalive()) { synchronized (wham) {wham.notify(); i++; System.out.println("\nI had to send " + i + " notifications."); Question: how many times was notify() called? 13 Lifecycle of a thread created start() o.notify() or o.notifyall() called by another thread waiting sleep time is over sleeping o.wait() sleep() ready chosen by scheduler running run() terminates dead interrupted by scheduler or call to yield() blocked for i/o or waiting for lock (synchronized) data received or lock obtained i/o stream closed by another thread blocked 14

Synchronization The producers/consumers problem is a typical synchronization problem: Let S be a bounded buffer A producer should only be allowed to produce as long as S is not full A consumer should only be allowed to consume as long as S is not empty 15 Producer/Consumer public class Producer extends Thread { private Stack shared; public Producer(Stack shared) { this.shared= shared; while (true) { int d=...; shared.push(d); public class Consumer extends Thread { private Stack shared; public Consumer(Stack shared) { this.shared= shared; while (true) { int d= shared.pop();... public class Stack { private int[] data; private int i; public Stack(int size) { data= new int[size]; i= 0; synchronized public void push(int d) { if (i == data.length) wait(); data[i]= d; i++; notify(); synchronized public int pop() { if (i == 0 ) wait(); i--; int d= data[i]; notify(); return d; 16

Producers/Consumers synchronized public void push(int d) { while (i == data.length) wait(); data[i]= d; i++; notifyall(); synchronized public int pop() { while (i == 0) wait(); i--; int d= data[i]; notifyall(); return d; if! while transformation ensures safety notify! notifyall transformation ensures liveness 17 General pattern ensures safety ensures liveness synchronized public void dosomething(...) {... while ( condition not true ) wait();... notifyall(); A problematic scenario with while and notify (not notifyall) 1 2 3 4 5 Stack S empty full empty full full Consumer C1 state: ready [1] do: wait state: ready [2] do: empty S do: notify P1 do: wait state: wait state: wait state: wait [S.size() == 1] Producer P1 state: ready [2] do: fill S do: notify C1 do: wait state: wait state: ready do: fill S do: notify P2 do: wait state: wait state: wait Producer P2 state: blocked [outside push] state: ready [1] do: wait state: wait state: ready do: wait state: wait 18

Limitations of monitors (1) The monitor abstraction is somewhat too high-level. In particular, it is impossible to: acquire mutual exclusion if it is already granted, or give it up after a timeout or an interrupt acquire mutual exclusion in one method and release it in another method alter the semantics of mutual exclusion, e.g., with respect to reentrancy, reads vs. writes, or fairness 19 Limitations of monitors (2) The monitor abstraction is somewhat too low-level. In particular, it provides no direct support for: Atomic variables (thread-safe single variables) Reader/writer and producer/consumer Highly concurrent collection classes 20

Concurrency utilities (JSE5) Design goals Reduced programming effort Increased performance & reliability Improved maintainability & productivity Features Task scheduling framework Concurrent collections & atomic variables Synchronizers & locks Nanosecond-granularity Packages java.util.concurrent, java.util.concurrent.atomic, java.util.concurrent.locks 21 Locks & condition variables public class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notfull = lock.newcondition(); final Condition notempty = lock.newcondition(); final Object[] items = new Object[100]; int putptr, takeptr, count; public void put(object x) throws InterruptedException { lock.lock(); try { while (count == items.length) notfull.await(); items[putptr] = x; if (++putptr == items.length) putptr = 0; ++count; notempty.signal(); finally { lock.unlock();... public Object take() throws InterruptedException { lock.lock(); try { while (count == 0) notempty.await(); Object x = items[takeptr]; if (++takeptr == items.length) takeptr = 0; --count; notfull.signal(); return x; finally { lock.unlock(); The await() call is equivalent to the wait() call The signal() call is equivalent to the notify() call 22

Atomic variables Atomic variables lock-free & thread-safe programming on single variables public class Sequencer { private long unsafesequencenumber = 0; private AtomicLong safesequencenumber = new AtomicLong(0); public long unsafenext() { return unsafesequencenumber++; synchronized public long blockingnext() { return unsafesequencenumber++; public long safelockfreenext() { return safesequencenumber.getandincrement(); 23 Reader/Writer support Interface ReadWriteLock & class ReentrantReadWriteLock support reader/writer solutions with the following properties: multiple threads can read simultaneously fairness policy can be enforced (arrival-order) public class ReaderWriterDictionary { private final Map<String, String> m = new TreeMap<String, String>(); private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(true); private final Lock r = rwl.readlock(); private final Lock w = rwl.writelock(); public String get(string key) { r.lock(); try { return m.get(key); finally { r.unlock(); public String put(string key, String value) { w.lock(); try { return m.put(key, value); finally { w.unlock(); 24

Producer/Consumer support Interface BlockingQueue & various implementation classes support producers/consumers solutions in a direct manner class Producer implements Runnable { private final BlockingQueue queue; Producer(BlockingQueue q) { queue = q; try { while(true) { queue.put(produce()); catch (InterruptedException ex) {... Object produce() {... class Consumer implements Runnable { private final BlockingQueue queue; Consumer(BlockingQueue q) { queue = q; try { while(true) { consume(queue.take()); catch (InterruptedException ex) {... void consume(object x) {... void main() { BlockingQueue q = new ArrayBlockingQueue(1000,true); Producer p = new Producer(q); Consumer c1 = new Consumer(q); Consumer c2 = new Consumer(q); new Thread(p).start(); new Thread(c1).start(); new Thread(c2).start(); 25 Concurrent collections The Hashtable is already thread-safe, so why define a new class ConcurrentHashMap? With a Hashtable, every method is synchronized, so no two threads can access it concurrently With a ConcurrentHashMap, multiple operations can overlap each other without waiting, i.e., unbounded number of reads can overlap each other up to 16 writes can overlap each other reads can overlap writes 26

Questions? 27