1 Announcements HW9: Java Concurrency Due Monday May 9 th Tests in HW Server coming up Check your grades in Rainbow Grades! Homework Server survey:

Slides:



Advertisements
Similar presentations
Identity and Equality Based on material by Michael Ernst, University of Washington.
Advertisements

CS492B Analysis of Concurrent Programs Lock Basics Jaehyuk Huh Computer Science, KAIST.
Relaxed Consistency Models. Outline Lazy Release Consistency TreadMarks DSM system.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Concurrency 101 Shared state. Part 1: General Concepts 2.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Synchronization. Shared Memory Thread Synchronization Threads cooperate in multithreaded environments – User threads and kernel threads – Share resources.
/ PSWLAB Atomizer: A Dynamic Atomicity Checker For Multithreaded Programs By Cormac Flanagan, Stephen N. Freund 24 th April, 2008 Hong,Shin.
CS 263 Course Project1 Survey: Type Systems for Race Detection and Atomicity Feng Zhou, 12/3/2003.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
PARALLEL PROGRAMMING with TRANSACTIONAL MEMORY Pratibha Kona.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
1 Lecture 21: Transactional Memory Topics: consistency model recap, introduction to transactional memory.
1 Lecture 7: Transactional Memory Intro Topics: introduction to transactional memory, “lazy” implementation.
1 Lecture 23: Transactional Memory Topics: consistency model recap, introduction to transactional memory.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
C. FlanaganType Systems for Multithreaded Software1 Cormac Flanagan UC Santa Cruz Stephen N. Freund Williams College Shaz Qadeer Microsoft Research.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
/ PSWLAB Type-Based Race Detection for J AVA by Cormac Flanagan, Stephen N. Freund 22 nd Feb 2008 presented by Hong,Shin Type-Based.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Concurrency Server accesses data on behalf of client – series of operations is a transaction – transactions are atomic Several clients may invoke transactions.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
CMSC 330: Organization of Programming Languages Threads.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.
Week 9, Class 3: Java’s Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Concurrent Programming Acknowledgements: Some slides adapted from David Evans, U. Virginia.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
1 Announcements HW8 due today HW9: Java Concurrency Will post tonight Due May 9 th Java infrastructure Download the Eclipse IDE Rainbow grades HW 1-6 Exam.
Lecture 20: Consistency Models, TM
Multithreading / Concurrency
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Threads Cannot Be Implemented As a Library
CSE 332: Locks and Deadlocks
Threads and Memory Models Hal Perkins Autumn 2011
Atomicity in Multithreaded Software
Lecture 6: Transactions
More on Thread Safety CSE451 Andrew Whitaker.
Threads and Memory Models Hal Perkins Autumn 2009
Lecture 22: Consistency Models, TM
CSCI1600: Embedded and Real Time Software
Kernel Synchronization II
CSE 153 Design of Operating Systems Winter 19
Threads and Multithreading
Lecture 8 Thread Safety.
CSCI1600: Embedded and Real Time Software
Lecture: Consistency Models, TM
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Threads and concurrency / Safety
Presentation transcript:

1 Announcements HW9: Java Concurrency Due Monday May 9 th Tests in HW Server coming up Check your grades in Rainbow Grades! Homework Server survey: Link off of Announcements page Spring 16 CSCI 4430, A Milanova

2 Announcements Final Exam Friday, 5/20, pm, Room: TBD Check for conflicts with other classes Practice Problems for Final Announcements page I’ll post back tests and practice problems The final will ask you to define terms E.g., What is type safety? What is a closure? What is a higher-order function, etc. Spring 16 CSCI 4430, A Milanova

3 Final Exam is Cumulative Programming Language Syntax (Ch ) Logic Programming and Prolog (Ch. 11) Scoping (Ch ) Programming Language Semantics: Attribute Grammars (Sc. Ch ) Functional programming and Scheme (Ch. 10) map/fold problems as in Exam 2 Lambda calculus (notes + Ch on CD) Data abstraction: Types (Ch. 7.1 – 7.7.1) Control abstraction: Parameter Passing (Ch ) Object-oriented languages ( ) Concurrency ( ). What can go wrong questions Dynamic languages and Python (Ch. 13 optional) Comparative Programming Languages Spring 16 CSCI 4430, A Milanova

4 Last Class Intro to Concurrency Concurrency in Java Threads What can go wrong with threads? Synchronized blocks The Executor framework Spring 16 CSCI 4430, A Milanova

5 Today’s Lecture Outline Java Programming Assignment Consistency with two-phase locking Avoiding deadlock Concurrency in Java Threads and locks What can go wrong? Data races, atomicity errors, deadlocks More on synchronization

Java Programming Assignment Executing transactions In parallel Transactions are atomic --- they execute at once, as a single indivisible operation Any outcome that corresponds to a serialized execution is considered consistent/correct A: 0, B: 1, C: 2, D: 3 Transaction 1Transaction 2Transaction 3 B=C; A=A+B B=D+AD=C 6

Java Programming Assignment Transactions execute under the optimistic (i.e., speculative) concurrency model Each transaction proceeds optimistically, i.e., expecting that no conflicts will occur If a transaction detects a conflict with another transaction, it aborts (or rolls back) and retries later Transaction 1Transaction 2Transaction 3 B=C; A=A+BB=D+AD=C What conflicts do we have here? 7

Conflicts A conflict occurs if two transactions access the same account “simultaneously” and at least one access is a write Transaction 1Transaction 2Transaction 3 B=C; A=A+B B=D+A D=C A write-write conflict between T1 and T2 (on B) A read-write conflict between T1 and T2 (on A and B) A read-write conflict between T2 and T3 (on D) 8

Account class value writer // Only ONE writer thread is allowed readers // MANY reader threads are allowed peek() // peek the value of Account object // Can be called before OPEN update() // Must be called after account has // been OPENED (i.e., locked) for // writing by current transaction 9 Spring 16 CSCI 4430, A Milanova

Account class open(writing/reading) // tries to “lock” account: // if it detects conflict then abort else “lock” // open checks for conflicts: If opening for writing and another transaction holds the “write lock” (i.e., writer != null) abort. Detected write-write conflict! If opening for writing and another transaction holds a “read lock”, abort transaction. If opening for reading and another transaction holds the “write lock”, abort transaction. Detected read-write conflict! 10

What you need to do (roughly) Task executes transaction in local cache Task executes the transaction on local cache, keeping track of each account that is written or read Task writes back local cache into global accounts (may fail due to conflicts, in which case task must retry transaction) 11

Example Global shared accounts: A: 0B: 1 C: 2 Task runs transaction A=B+C; B=A+B Task works in local cache: Original values: A: 0, B: 1, C: 2 Local cache: A: 3, B: 4, C: 2 written:A: yes, B: yes, C: no read:A: yes, B: yes, C: yes 12

Write back Now, task needs to write local cache to global shared accounts Here is an outline of the process (but there are details you need to figure out yourselves!) Phase 1. Open (i.e., lock) all accounts Account.open checks for conflicts Wrap Account.open. Catch AbortException Phase 2. Once all accounts are successfully opened (i.e. locked), write back (commit) to accounts and close (i.e., unlock) all accounts 13

One more thing… I didn’t specify in what order transaction opens all accounts What can happen if transaction opens the accounts in the order it sees them E.g., A = B opens A then B, and B = A opens B then A DEADLOCK! Solution: each transaction opens accounts in the same well-defined order, from A to Z 14

15 Today’s Lecture Outline Java Programming Assignment Consistency with two-phase locking Avoiding deadlock Concurrency in Java Threads and Locks What can go wrong? Data races, atomicity errors, deadlocks More on synchronization Memory consistency models

16 Back to Java. Threads … Java has explicit support for multiple threads Two ways to create new threads: Extend java.lang.Thread Override “run()” method Implement Runnable interface Include a “run()” method in your class Starting a thread new MyThread().start(); new Thread(runnable).start(); Abstracted away by Executor framework Spring 16 CSCI 4430, A Milanova

17 What Can Go Wrong? class Account { int balance = 0; void deposit (int x) { this.balance = this.balance + x; } class AccountTask implements Runnable { public void run() { Main.act.deposit(10); } public class Main { static Account act = new Account(); public static void main(String args[]) { new Thread(new AccountTask()).start(); // Thread A new Thread(new AccountTask()).start(); // Thread B } Account object is shared mutable state.

18 What Can Go Wrong? Thread A:Thread B: r1 = act.balance r1 += 10 act.balance = r1r2 = act.balance r2 += 10 act.balance = r2

19 Using Synchronized Blocks How do we make the Account class “safe”? synchronized void deposit(int x) { … } Thread A: synchronized (this) { r1 = balance r1 += 10 balance = r1Thread B: }synchronized (this) { r2 = balance r2 += 10 balance = r2 } this refers to global Account object Spring 16 CSCI 4430, A Milanova

20 So… What Can Go Wrong? New types of bugs occur in concurrent programs Race conditions Atomicity violations Deadlocks There is nondeterminism in concurrency, which makes reasoning about program behavior extremely difficult Spring 16 CSCI 4430, A Milanova

21 What Can Go Wrong? Real-world Example (Java 1.1) class Vector { private Object elementData[]; private int elementCount; synchronized void trimToSize() { … } synchronized boolean removeAllElements() { elementCount = 0; trimToSize(); } synchronized int lastIndexOf(Object elem, int n) { for (int i = n-1; i >= 0; i--) if (elem.equals(elementData[i])) return i; return -1; } int lastIndexOf(Object elem) { n = elementCount; return lastIndexOf(elem, n); } … }

22 What Can Go Wrong? A Real-world Example (Java 1.1) Thread A:Thread B: removeAllElements lastIndexOf(elem) elementCount=0n=elementCount trimToSize …elementData=… lastIndexOf(elem,n) …elementData[n-1]… There is a data race on elementCount : Will raise an exception because elementData has been reset by thread A. Spring 16 CSCI 4430, A Milanova

23 What Can Go Wrong? java.lang.StringBuffer (1.4) Has a Bug public final class StringBuffer { private int count; private char[ ] value;.. public synchronized StringBuffer append(StringBuffer sb) { if (sb == null) sb = NULL; int len = sb.length(); int newcount = count + len; if (newcount > value.length) expandCapacity(newcount); sb.getChars(0, len, value, count); count = newcount; return this; } public synchronized int length( ) { return count; } public synchronized void getChars(...) {... } } Spring 16 CSCI 4430, A Milanova

Method append is not “atomic”: 24 Thread A: Thread B: sb.length() sb.delete(…) sb.getChars() What Can Go Wrong? java.lang.StringBuffer (1.4) Has a Bug Will raise an exception because sb’s value array has been updated by thread B. Spring 16 CSCI 4430, A Milanova

25 Atomicity Violation Method StringBuffer.append is not ”atomic” Informally, a method is said to be atomic if its “sequential behavior” (i.e., behavior when method is executed in one step), is the same as its “concurrent behavior” (i.e., behavior when method is interrupted by other threads) A method is atomic if it appears to execute in “one step” even in the presence of multiple threads Atomicity is a stronger correctness pr operty than race freedom Spring 16 CSCI 4430, A Milanova

26 Using Synchronization Lock-based synchronization helps avoid race conditions and atomicity violations But synchronization can cause deadlocks! Lock granularity Synchronized blocks that are too long (i.e., coarse grained locking) sacrifice concurrency and may lead to slow down Force sequential execution as threads wait for locks Synchronized blocks that are too short (i.e., fine grained locking) may miss race conditions! Spring 16 CSCI 4430, A Milanova

Concurrent Programming is Difficult Concurrent programming is about managing shared mutable state Exponential number of interleavings of thread operations OO concurrency: complex shared mutable state Defense: immutable classes, objects, or references Defense: avoid representation exposure 27

28 What Can Go Wrong? ArrayList seen = new ArrayList(); // seen is shared state … void search(Node node) { … Runnable task = new Runnable() { public void run() { … synchronized (this) { // synchronize access to seen if (!seen.contains(node.pos)) seen.add(node.pos); else return; } // check if current node is a solution … // compute legal moves, call search(child) … } }; e.execute(task); }

What Can Go Wrong? JDK public abstract class Writer { protected Object lock; Writer() { this.lock = this; } public void write(char ch) { … } public void write(String str){ … } } 29 Spring 16 CSCI 4430, A Milanova

What Can Go Wrong? (cont.) public class PrintWriter extends Writer { protected Writer out; public PrintWriter(Writer out) { super(); this.out = out; } continues on next page -> 30

What Can Go Wrong? (cont.) public void print(int x) { synchronized (lock) { out.write(Integer.toString(x)) } } public void println() { synchronized (lock) { out.write(lineSeparator); } } public void println(int x) { synchronized (lock) { print(x); println(); } } } 31

32 Today’s Lecture Outline Java Programming Assignment Consistency with two-phase locking Avoiding deadlock Concurrency in Java Threads and Locks What can go wrong? Data races, atomicity errors, deadlocks More on synchronization

33 Synchronization Synchronization refers to any mechanism that allows the programmer to control the relative order of thread operations. Synchronization serves two purposes: Atomicity Require that thread executes operation “at once” In Java, we use mutex locks and synchronized blocks Condition synchronization Require that thread waits on a condition (expressed as a predicate on one or more shared variables) Until Java 5, Java had poor support for condition synchronization (essentially just wait(), notify() ) Spring 16 CSCI 4430, A Milanova

Busy-wait Condition Synchronization Busy-wait: thread remains active until a condition becomes true; thread isn’t performing anything useful Two forms of busy-wait synchronization: Spin lock: intuitively, the thread “spins” in a loop repeatedly, checking if the lock has become available (or the condition has become true) Barrier: a group of threads must all reach a common point; a thread cannot proceed until all other threads have reached the point Spring 16 CSCI 4430, A Milanova 34

Implementing Busy-wait Implementing busy-wait condition synchronization relies on hardware support for atomic read-modify-write instructions Simplest atomic test_and_set: sets a boolean variable to true and returns an indication of whether the variable was previously false More advanced versions: atomic compare_and_swap (CAS): takes 3 arguments, a location, an expected value and a new value; if location’s value is the expected value, CAS swaps value with new value 35 Spring 16 CSCI 4430, A Milanova

… in Java Java 5 has the java.util.concurrent library: Executor thread pools AtomicInteger etc., which provide atomic read- modify-write methods Synchronizers which provide efficient barriers, semaphores, locks, etc. Huge improvement of support for condition synchronization 36