COMP 322: Fundamentals of Parallel Programming

Similar documents
COMP 322: Fundamentals of Parallel Programming. Lecture 30: Java Synchronizers, Dining Philosophers Problem

CMSC 433 Programming Language Technologies and Paradigms Fall Locks & Conditions

Simpler, Faster, Better: Concurrency Utilities in JDK Software Version 5.0

Network Applications: High-Performance Network Servers

Concurrent Programming Benoît Garbinato

Lecture 29: Java s synchronized statement

Outline. Network Applications: High-Performance Network Servers. Recap: Server Processing Steps. Worker. Recap: Main Thread. Recap: Progress So Far

G52CON: Concepts of Concurrency

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

Lecture 32: Volatile variables, Java memory model

Dr.-Ing. Michael Eichberg

CONCURRENT API AND PARALLEL PROGRAMMING

Daemon thread, 7 Deadlock, 27

Producer/Consumer CSCI 201 Principles of Software Development

Multi-Threaded Programming Design CSCI 201 Principles of Software Development

Dr.-Ing. Michael Eichberg

Threads Questions Important Questions

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

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

COMP 322: Fundamentals of Parallel Programming

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

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

Buffer & File Optimization. Cloud Databases DataLab CS, NTHU

Concurrency Utilities: JSR-166

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

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

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

CSE Lecture 25: Concurrency 27 April Nate Nystrom UTA

Chapter 32 Multithreading and Parallel Programming

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

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

Java ReentrantLock (Part 1)

COMP 322: Fundamentals of Parallel Programming

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

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

CPIT 305 Advanced Programming Multithreading

CSE332: Data Abstractions Lecture 23: Data Races and Memory Reordering Deadlock Readers/Writer Locks Condition Variables. Dan Grossman Spring 2012

More thread safety and Concurrent collections. Nasser Giacaman CompSci 230: Software Design and Construction

Teaching predicates and invariants on shared data structures in concurrent programming

-Aditya Bhave CSCI-5448 Graduate Presentation

Collec&ons & Concurrency

Northeastern University - Seattle CS5510 Professor Ian Gorton

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

Two Types of Semaphores

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

CMSC 330: Organization of Programming Languages

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

Lecture 27: Safety and Liveness Properties, Java Synchronizers, Dining Philosophers Problem

Synchronized Methods of Old Versions of Java

Josh Bloch Charlie Garrod Darya Melicher

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, Part 2 Can't live with it. Cant live without it.

Concurrent Queues, Monitors, and the ABA problem. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Race Conditions & Synchronization

Practical Concurrency. Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M)

Paradigmas de Computação Paralela

Java Concurrency. Towards a better life By - -

Only one thread can own a specific monitor

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

CSE 332: Locks and Deadlocks. Richard Anderson, Steve Seitz Winter 2014

Programming in Java

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

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

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

JFokus Finding And Solving Java Deadlocks

CS11 Java. Fall Lecture 7

Java Barrier Synchronizers: CountDownLatch (Part 1)

CSE 153 Design of Operating Systems

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, Part 2 (Can't live with it, can't live without it.

TEST CONCURRENT PROGRAMMING. code: date: 19 June 2015 time:

CSE 332: Data Abstractions Lecture 22: Data Races and Memory Reordering Deadlock Readers/Writer Locks Condition Variables. Ruth Anderson Spring 2014

Condition Synchronization

Part IV Other Systems: I Java Threads

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

Problems with Concurrency. February 19, 2014

7.1 Processes and Threads

Programmazione di sistemi multicore

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

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

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

CONCURRENT PROGRAMMING - EXAM

JAVA EXAMPLES - SOLVING DEADLOCK

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

Principles of Software Construction: Concurrency, Part 2

Operating Systems ECE344

Race Conditions & Synchronization

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Java Semaphore (Part 1)

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

Chapter 8 Applying Thread Pools. Magnus Andersson

CSE 120 Principles of Operating Systems Spring 2016

Java Threads. Introduction to Java Threads

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Concurrency API. Bram Inniger October 25, Based on previous work by Pavel Grigorenko and Rein Raudjärv

EPITA Première Année Cycle Ingénieur. Atelier Java - J3

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Chapter 6: Process Synchronization

Transcription:

COMP 322: Fundamentals of Parallel Programming https://wiki.rice.edu/confluence/display/parprog/comp322 Lecture 30: Advanced locking in Java Vivek Sarkar Department of Computer Science Rice University vsarkar@rice.edu COMP 322 Lecture 30 4 April 2011

Acknowledgments for Todayʼs Lecture" Combined handout for Lectures 27-30 (to be updated) Introduction to Concurrent Programming in Java, Joe Bowbeer, David Holmes, OOPSLA 2007 tutorial slides Contributing authors: Doug Lea, Brian Goetz Java Concurrency Utilities in Practice, Joe Bowbeer, David Holmes, OOPSLA 2007 tutorial slides Contributing authors: Doug Lea, Tim Peierls, Brian Goetz ECE 3005 course slides from Georgia Tech http://users.ece.gatech.edu/~copeland/jac/3055-05/ppt/ch07- sync-b.ppt A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency, Lecture 6, Dan Grossman, U. Washington http://www.cs.washington.edu/homes/djg/teachingmaterials/ grossmanspac_lec6.pptx 2

Announcements" Homework 6 deadline extended to 5pm on Wednesday, April 6 th due to difficulties in accessing SUG@R nodes Please use special COMP322 queue for SUG@R during lab hours 3

Complete Bounded Buffer using Java Synchronization (Recap)" public class BoundedBuffer implements Buffer { private static final int BUFFER SIZE = 5; private int count, in, out; private Object[] buffer; public BoundedBuffer() { // buffer is initially empty count = 0; in = 0; out = 0; buffer = new Object[BUFFER SIZE]; public synchronized void insert(object item) { // See previous slides public synchronized Object remove() { // See previous slides 4

insert() with wait/notify Methods" public synchronized void insert(object item) { while (count == BUFFER SIZE) { try { wait(); catch (InterruptedException e) { ++count; buffer[in] = item; in = (in + 1) % BUFFER SIZE; notify(); 5

remove() with wait/notify Methods" public synchronized Object remove() { Object item; while (count == 0) { try { wait(); catch (InterruptedException e) { --count; item = buffer[out]; out = (out + 1) % BUFFER SIZE; notify(); return item; 6

Entry and Wait Sets" Scenario in which multiple producers and consumers can be in wait set for BUFFER_SIZE = 1 Time-step Entry set Buffer state Wait set t P0 EMPTY C0, C1 t+1 C0, P1 FULL C1 t+2 C0 FULL P1, C1 7

java.util.concurrent General purpose toolkit for developing concurrent applications import java.util.concurrent.* Goals: Something for Everyone! Make some problems trivial to solve by everyone Develop thread-safe classes, such as servlets, built on concurrent building blocks like ConcurrentHashMap Make some problems easier to solve by concurrent programmers Develop concurrent applications using thread pools, barriers, latches, and blocking queues Make some problems possible to solve by concurrency experts Develop custom locking classes, lock-free algorithms HJ approach Build HJ runtime on top of java.util.concurrent library 8

List of j.u.c. libraries" 9 Atomics: java.util.concurrent.atomic Atomic[Type] Atomic[Type]Array Atomic[Type]FieldUpdater Atomic{Markable,StampableReference Concurrent Collections ConcurrentMap ConcurrentHashMap CopyOnWriteArray{List,Set Locks: java.util.concurrent.locks Lock Condition ReadWriteLock AbstractQueuedSynchronizer LockSupport ReentrantLock ReentrantReadWriteLock Synchronizers CountDownLatch Semaphore Exchanger CyclicBarrier Executors Executor ExecutorService ScheduledExecutorService Callable Future ScheduledFuture Delayed CompletionService ThreadPoolExecutor ScheduledThreadPoolExecutor AbstractExecutorService Executors FutureTask ExecutorCompletionService Queues BlockingQueue ConcurrentLinkedQueue LinkedBlockingQueue ArrayBlockingQueue SynchronousQueue PriorityBlockingQueue DelayQueue

Key Functional Groups in j.u.c." Atomic variables The key to writing lock-free algorithms Concurrent Collections: Queues, blocking queues, concurrent hash map, Data structures designed for concurrent environments Locks and Conditions More flexible synchronization control Read/write locks Executors, Thread pools and Futures Execution frameworks for asynchronous tasking Synchronizers: Semaphore, Latch, Barrier, Exchanger Ready made tools for thread coordination 10

Locks" Example of hand-over-hand locking: L1.lock() L2.lock() L1.unlock() L3.lock() L2.unlock(). 11

java.util.concurrent.locks.lock interface" interface Lock { void lock(); void lockinterruptibly() throws InterruptedException; boolean trylock(); boolean trylock(long timeout, TimeUnit unit) throws InterruptedException; void unlock(); Condition newcondition(); // can associate multiple condition vars with lock java.util.concurrent.locks.lock interface is implemented by java.util.concurrent.locks.reentrantlock class 12

Simple ReentrantLock() example" 13

java.util.concurrent.locks.condition interface" Can be allocated by calling ReentrantLock.newCondition() Supports multiple condition variables per lock Methods supported by an instance of condition void await() // NOTE: not wait Causes current thread to wait until it is signaled or interrupted Variants available with support for interruption and timeout void signal() // NOTE: not notify Wakes up one thread waiting on this condition void signalall() // NOTE: not notifyall() Wakes up all threads waiting on this condition For additional details see http://download.oracle.com/javase/1.5.0/docs/api/java/util/ concurrent/locks/condition.html 14 14

BoundedBuffer implementation using two conditions, notfull and notempty" 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;... 15

BoundedBuffer implementation using two conditions, notfull and notempty (contd)" 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(); 16

BoundedBuffer implementation using two conditions, notfull and notempty (contd)" 17 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();

Reading vs. writing" Recall that the use of synchronization is to protect interfering accesses Multiple concurrent reads of same memory: Not a problem Multiple concurrent writes of same memory: Problem Multiple concurrent read & write of same memory: Problem So far: But: If concurrent write/write or read/write might occur, use synchronization to ensure one-thread-at-a-time This is unnecessarily conservative: we could still allow multiple simultaneous readers Consider a hashtable with one coarse-grained lock So only one thread can perform operations at a time But suppose: There are many simultaneous lookup operations insert operations are very rare 18

java.util.concurrent.locks.readwritelock interface" interface ReadWriteLock { Lock readlock(); Lock writelock(); Even though the interface appears to just define a pair of locks, the semantics of the pair of locks is coupled as follows Case 1: a thread has successfully acquired writelock().lock() No other thread can acquire readlock() or writelock() Case 2: no thread has acquired writelock().lock() Multiple threads can acquire readlock() No other thread can acquire writelock() java.util.concurrent.locks.readwritelock interface is implemented by java.util.concurrent.locks.readwritereentrantlock class 19

Example code" class Hashtable<K,V> { // coarse-grained, one lock for table ReadWriteLock lk = new new ReentrantReadWriteLock(); V lookup(k key) { int bucket = hasher(key); lk.readlock().lock(); // only blocks writers read array[bucket] lk.readlock().unlock(); void insert(k key, V val) { int bucket = hasher(key); lk.writelock().lock(); // blocks readers and writers write array[bucket] lk.writelock().unlock(); 20