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

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

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

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

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

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

Jonathan Aldrich Charlie Garrod

Threads and Java Memory Model

Threads Questions Important Questions

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

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

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

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

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

Principles of Software Construction: Concurrency, Part 2

Synchronization Lecture 23 Fall 2017

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

Charlie Garrod Bogdan Vasilescu

CS11 Java. Fall Lecture 7

COMP 322: Fundamentals of Parallel Programming

Java Threads Vs Processes. CS Concurrent Programming. Java Threads. What can a thread do? Java Concurrency

The New Java Technology Memory Model

Race Conditions & Synchronization

Models of concurrency & synchronization algorithms

Monitors; Software Transactional Memory

Charlie Garrod Bogdan Vasilescu

Chapter 32 Multithreading and Parallel Programming

What is a thread anyway?

Principles of Software Construction: Objects, Design, and Concurrency

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

Synchronization Lecture 24 Fall 2018

Java Threads. Introduction to Java Threads

Daemon thread, 7 Deadlock, 27

Principles of Software Construction: Objects, Design and Concurrency. Mutability and Java Potpourri. toad Fall 2013

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

Principles of Software Construction: Objects, Design, and Concurrency. Distributed System Design, Part 2. Charlie Garrod Jonathan Aldrich.

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

Only one thread can own a specific monitor

Race Conditions & Synchronization

F. Tip and M. Weintraub CONCURRENCY

Faculty of Computers & Information Computer Science Department

Concurrency Utilities: JSR-166

CSC 543 Multiprocessing & Concurrent Programming, Fall 2016

Results of prelim 2 on Piazza

Basics of. Multithreading in Java

Synchronization in Concurrent Programming. Amit Gupta

Synchronization in Java

Java Concurrency. Towards a better life By - -

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

Lecture 29: Java s synchronized statement

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

Producing Production Quality Software. Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2004

Monitors; Software Transactional Memory

SFDV3006 Concurrent Programming

Object Oriented Programming and Design in Java. Session 18 Instructor: Bert Huang

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

CS 159: Parallel Processing

Java Platform Concurrency Gotchas

Principles of Software Construction: Concurrency, Part 2

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

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

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

Concurrent Programming using Threads

Concurrent Programming Benoît Garbinato

CS153: Deadlock. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

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

Charlie Garrod Michael Hilton

Problems with Concurrency. February 19, 2014

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

CS 347 Parallel and Distributed Data Processing

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

CS 351 Design of Large Programs Threads and Concurrency

CS 153 Design of Operating Systems Winter 2016

Atomic Variables & Nonblocking Synchronization

Mutual Exclusion: Classical Algorithms for Locks

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

RACE CONDITIONS AND SYNCHRONIZATION

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

Atomicity CS 2110 Fall 2017

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

Charlie Garrod Bogdan Vasilescu

CS 347 Parallel and Distributed Data Processing

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

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Principles of Software Construction: Objects, Design and Concurrency. Packages and Polymorphism. toad Fall 2012

Josh Bloch Charlie Garrod Darya Melicher

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

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

Java Programming Lecture 23

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Transactional Memory

Problems with Concurrency

Parallel Code Smells: A Top 10 List

Last Class: Monitors. Real-world Examples

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

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

CSE 374 Programming Concepts & Tools

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

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

Transcription:

Principles of Software Construction: Objects, Design and Concurrency 15-214 toad The Perils of Concurrency, Part 2 Can't live with it. Cant live without it. Fall 2013 Jonathan Aldrich Charlie Garrod School of Computer Science 2012-13 C Garrod, J Aldrich, and W Scherlis

Administrivia Homework 5: The Framework Strikes Back You should have already selected a partner(s) 5a due at your scheduled time next Wednesday Commit/push designs by 8:59 a.m. 2

Key topics from Tuesday 3

Last time: Concurrency, part 1 The concurrency backstory Motivation, goals, problems, 4

Last time: Concurrency, part 1 The concurrency backstory Motivation, goals, problems, http://www.genome.gov/sequencingcosts/ 5

Bad news: some simple actions are not atomic Consider a single 64-bit long value high bits low bits Concurrently: Thread A writing high bits and low bits Thread B reading high bits and low bits Precondition: long i = 10000000000;! Thread A: i = 42;! Thread B: ans = i;! ans:!01001 00000000 ans:!00000 00101010 ans:!01001 00101010 (10000000000) (42) (10000000042 or ) 6

Today: Concurrency, part 2 Race conditions, revisited Primitive concurrency in Java Explicit synchronization with threads and shared memory More concurrency problems Higher-level abstractions for concurrency (still mostly not today) Data structures Higher-level languages and frameworks Hybrid approaches 7

Race conditions in real life E.g., check-then-act on the highway R C L 8

Race conditions in your life E.g., check-then-act in simple code public class StringConverter {! private Object o;! public void set(object o) {! this.o = o;! }! public String get() {! if (o == null) return "null";! return o.tostring();! }! }! See StringConverter.java, Getter.java, Setter.java 9

Primitive concurrency control in Java Each Java object has an associated intrinsic lock All locks are initially unowned Each lock is exclusive: it can be owned by at most one thread at a time The synchronized keyword forces the current thread to obtain an object's intrinsic lock E.g., synchronized void foo() { } // locks "this"!!!synchronized(fromacct) {!!! if (fromacct.getbalance() >= 30) {! toacct.deposit(30);! fromacct.withdrawal(30);! }! } See SynchronizedIncrementTest.java 10

Primitive concurrency control in Java java.lang.object allows some coordination via the intrinsic lock: void wait();! void wait(long timeout);! void wait(long timeout, int nanos);! void notify();! void notifyall();! See Blocker.java, Notifier.java, NotifyExample.java 11

Primitive concurrency control in Java Each lock can be owned by only one thread at a time Locks are re-entrant: If a thread owns a lock, it can lock the lock multiple times A thread can own multiple locks synchronized(lock1) {! // do stuff that requires lock1!! synchronized(lock2) {! // do stuff that requires both locks! }!! //! } 12

Another concurrency problem: deadlock E.g., Alice and Bob, unaware of each other, both need file A and network connection B Alice gets lock for file A Bob gets lock for network connection B Alice tries to get lock for network connection B, and waits Bob tries to get lock for file A, and waits See Counter.java and DeadlockExample.java 13

Dealing with deadlock (abstractly, not with Java) Detect deadlock Statically? Dynamically at run time? Avoid deadlock Alternative approaches Automatic restarts Optimistic concurrency control 14

Detecting deadlock with the waits-for graph The waits-for graph represents dependencies between threads Each node in the graph represents a thread A directed edge T1->T2 represents that thread T1 is waiting for a lock that T2 owns Deadlock has occurred iff the waits-for graph contains a cycle b a d e c f g Got a problem with this? h i 15

Deadlock avoidance algorithms Prevent deadlock instead of detecting it E.g., impose total order on all locks, require locks acquisition to satisfy that order Thread: acquire(lock1) acquire(lock2) acquire(lock9) acquire(lock42) // now can't acquire lock30, etc Got a problem with this? 16

Avoiding deadlock with restarts One option: If thread needs a lock out of order, restart the thread Get the new lock in order this time Another option: Arbitrarily kill and restart longrunning threads 17

Avoiding deadlock with restarts One option: If thread needs a lock out of order, restart the thread Get the new lock in order this time Another option: Arbitrarily kill and restart longrunning threads Optimistic concurrency control e.g., with a copy-on-write system Don't lock, just detect conflicts later Restart a thread if a conflict occurs 18

Another concurrency problem: livelock In systems involving restarts, livelock can occur Lack of progress due to repeated restarts Starvation: when some task(s) is(are) repeatedly restarted because of other tasks 19

Concurrency control in Java Using primitive synchronization, you are responsible for correctness: Avoiding race conditions Progress (avoiding deadlock) Java provides tools to help: volatile fields java.util.concurrent.atomic! java.util.concurrent! 20

The Java happens-before relation Java guarantees a transitive, consistent order for some memory accesses Within a thread, one action happens-before another action based on the usual program execution order Release of a lock happens-before acquisition of the same lock Object.notify happens-before Object.wait returns Thread.start happens-before any action of the started thread Write to a volatile field happens-before any subsequent read of the same field Assures ordering of reads and writes A race condition can occur when reads and writes are not ordered by the happens-before relation 21

The java.util.concurrent.atomic package Concrete classes supporting atomic operations AtomicInteger!!int get();!!void set(int newvalue);!!int getandset(int newvalue);!!int getandadd(int delta);!!! AtomicIntegerArray! AtomicBoolean! AtomicLong!! 22

The java.util.concurrent package Interfaces and concrete thread-safe data structure implementations ConcurrentHashMap! BlockingQueue! ArrayBlockingQueue! SynchronousQueue! CopyOnWriteArrayList! Other tools for high-performance multi-threading ThreadPools and Executor services! Locks and Latches 23

java.util.concurrent.concurrenthashmap! Implements java.util.map<k,v>! High concurrency lock striping Internally uses multiple locks, each dedicated to a region of the hash table Locks just the part of the table you actually use You use the ConcurrentHashMap like any other map Locks Hashtable 24

java.util.concurrent.blockingqueue! Implements java.util.queue<e>! java.util.concurrent.synchronousqueue Each put directly waits for a corresponding poll! Internally uses wait/notify! java.util.concurrent.arrayblockingqueue! put blocks if the queue is full poll blocks if the queue is empty Internally uses wait/notify 25

The CopyOnWriteArrayList! Implements java.util.list<e>! All writes to the list copy the array storing the list elements 26

Next week: More concurrency 27