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

Similar documents
Concurrency. Fundamentals of Computer Science

Michele Van Dyne MUS 204B Concurrency Issues

Threads. Fundamentals of Computer Science

Michele Van Dyne MUS 204B Threads

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2012

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2012

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

CS18000: Programming I

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

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

Lecture 35. Threads. Reading for next time: Big Java What is a Thread?

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

CMSC 132: Object-Oriented Programming II

Exercise Session Week 8

Principles of Software Construction: Concurrency, Part 1

Exercise Session Week 8

Processes and Threads. Industrial Programming. Processes and Threads (cont'd) Processes and Threads (cont'd)

7. MULTITHREDED PROGRAMMING

F. Tip and M. Weintraub CONCURRENCY

Question Points Score Total 100

Project. Threads. Plan for today. Before we begin. Thread. Thread. Minimum submission. Synchronization TSP. Thread synchronization. Any questions?

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

Systems Programming & Scripting

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

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

Threads and Parallelism in Java

Network Programming COSC 1176/1179. Lecture 6 Concurrent Programming (2)

COE518 Lecture Notes Week 7 (Oct 17, 2011)

Part IV Other Systems: I Java Threads

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

Threads and Java Memory Model

Common Java Concurrency pitfalls. Presented by : Kunal Sinha Austin Java Users Group 03/26/2019

Concurrency and Java Programming

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

Multiple Inheritance. Computer object can be viewed as

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

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

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, Instructions

Programming in Parallel COMP755

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

COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors

Concurrency CSCI 201 Principles of Software Development

Animation Part 2: MoveableShape interface & Multithreading

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

CmpSci 187: Programming with Data Structures Spring 2015

Multithreading Pearson Education, Inc. All rights reserved.

Single processor CPU. Memory I/O

Problems with Concurrency. February 19, 2014

Note: Each loop has 5 iterations in the ThreeLoopTest program.

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

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

Introduction to Java Threads

User Space Multithreading. Computer Science, University of Warwick

JAVA EXAMPLES - SOLVING DEADLOCK

Principles of Software Construction: Concurrency, Part 1

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

COMP346 Winter Tutorial 4 Synchronization Semaphores

Threads Chate Patanothai

Synchronization in Java

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

JAVA - MULTITHREADING

Module - 4 Multi-Threaded Programming

CMPSCI 187: Programming With Data Structures. Lecture #20: Concurrency and a Case Study David Mix Barrington 24 October 2012

Concurrent Computing CSCI 201 Principles of Software Development

Multithreaded Programming

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

THREADS AND CONCURRENCY

Introduction to Locks. Intrinsic Locks

CS193k, Stanford Handout #8. Threads 3

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

COMP 213. Advanced Object-oriented Programming. Lecture 23. Shared Variables and Synchronization

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

CISC 4700 L01 Network & Client-Server Programming Spring Cowell Chapter 15: Writing Threaded Applications

Principles of Software Construction: Concurrency, Part 2

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

CS 351 Design of Large Programs Threads and Concurrency

Lecture 9: Introduction to Monitors

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

CMSC 330: Organization of Programming Languages

Threads and Concurrency in Java

CSCI 201L Written Exam #2. 10% of course grade

Problems with Concurrency

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

Programming Java. Multithreaded Programming

Java Threads. Introduction to Java Threads

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

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

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

Concurrent Programming: Threads. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

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

By: Abhishek Khare (SVIM - INDORE M.P)

Synchronizers Latches & Barriers

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

Java s Implementation of Concurrency, and how to use it in our applications.

Multithreading. A thread is a unit of control (stream of instructions) within a process.

CS 159: Parallel Processing

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Synchronization

Software Practice 1 - Multithreading

Transcription:

Concurrency http://csunplugged.org/routing-and-deadlock CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Overview Multi-threaded programs Multiple simultaneous paths of execution Seemingly at once (single core) Actually at the same time (multiple cores) Concurrency issues The dark side of threading Unpredictability of thread scheduler can get you Protecting shared data: synchronized methods Deadlock The really dark side of threading 2

Review: creating & starting a thread public class BlastOff implements Runnable public void run() for (int i = 10; i > 0; i--) System.out.print(i + " "); System.out.println("BLAST OFF!"); public class Launch public static void main(string [] args) System.out.println("prepare for launch"); Thread thread = new Thread(new BlastOff()); thread.start(); System.out.println("done with launch"); % java Launch prepare for launch done with launch 10 9 8 7 6 5 4 3 2 1 BLAST OFF! % java Launch prepare for launch 10 9 done with launch 8 7 6 5 4 3 2 1 BLAST OFF! % java Launch prepare for launch 10 done with launch 9 8 7 6 5 4 3 2 1 BLAST OFF! 3

Review: Multithreading for speed Goal: Count how often different integers occur In a large array of integers Randomly generated in [0, 100) Have one thread handle each target integer % java ParallelSearch 1000000 2 7 16 42 99 Starting workers... Count of 2 = 9888 Count of 7 = 10222 Count of 16 = 9989 Count of 42 = 10099 Count of 99 = 9894 4

public class SearchWorker implements Runnable private int target = 0; private int [] data = null; private int result = 0; public SearchWorker(int target, int [] data) this.target = target; this.data = data; public int getresult() return result; public int gettarget() return target; public void run() for (int i = 0; i < data.length; i++) if (data[i] == target) result++; Worker object: One of these is created for each target integer we want to search for. Needs to keep track of its input: what number to search for, the array to search in. Must remember its output: count of the target in the array. 5

... final int DATA_SIZE = Integer.parseInt(args[0]); final int WORKERS = args.length - 1; int [] data = new int[data_size]; for (int i = 0; i < DATA_SIZE; i++) data[i] = (int) (Math.random() * 100); SearchWorker [] workers = new SearchWorker[WORKERS]; Thread [] threads = new Thread[WORKERS]; for (int i = 0; i < WORKERS; i++) workers[i] = new SearchWorker(Integer.parseInt(args[i + 1]), data); threads[i] = new Thread(workers[i]); threads[i].start(); for (int i = 0; i < WORKERS; i++) try threads[i].join(); catch (InterruptedException e) e.printstacktrace(); System.out.printf("Count of %d = %d\n",... Client program: 1. Parses command line arguments. 2. Creates random array of data to search in. 3. Creates each worker, launches each worker in its own thread. 4. Waits for each thread to finish, printing out the worker s result. workers[i].gettarget(), workers[i].getresult()); 6

Trouble in Concurrency City: Act 1 Lost update problem Multiple threads All sharing a single counter object Each thread increments fixed number of times public class Count private int count = 0; public int getcount() return count; public class IncrementWorker implements Runnable private Count count = null; public IncrementWorker(Count count) this.count = count; public void increment() count++; public void run() for (int i = 0; i < 1000; i++) count.increment(); 7

Lost update problem... Count count = new Count(); Thread [] threads = new Thread[N]; for (int i = 0; i < N; i++) threads[i] = new Thread(new IncrementWorker(count)); threads[i].start(); for (int i = 0; i < N; i++) try threads[i].join(); catch (InterruptedException e) e.printstacktrace(); System.out.println("Final count = " + count.getcount());... % java Increment 1 Final count = 1000 % java Increment 2 Final count = 2000 % java Increment 20 Final count = 20000 % java Increment 20 Final count = 19761 % java Increment 20 Final count = 19014 8

Synchronizing methods Only allow 1 worker in increment at a time! Tell Java this using synchronized keyword public class Count private int count = 0; public int getcount() return count; public synchronized void increment() count++; % java Increment 20 Final count = 20000 % java Increment 20 Final count = 20000 % java Increment 20 Final count = 20000 9

Locking an object Each object instance has a lock Multiple methods can be marked synchronized Only one can be executing at any time Locking is on the object level On the instance of the class, not the class data type itself Locking and unlocking takes time Only synchronize methods if necessary public void run() for (int i = 0; i < 1000; i++) synchronized (count) count.increment(); Another approach to fixing the lost update problem. Synchronization can be enforced on a block of code instead of an entire method. 10

Trouble in Concurrency City: Act 2 Concurrent access to same data structure Many built-in containers are not thread-safe! e.g. ArrayList, HashMap Program will crash (probably) Protect all reading/writing to shared structure Via synchronized method or synchronized code block 11

public class ArrayListBad implements Runnable private static ArrayList<Integer> list = new ArrayList<Integer>(); public void run() for (int i = 0; i < 10; i++) list.add((int) (Math.random() * 100)); System.out.println("Thread all done..."); public static void main(string[] args) Thread t1 = new Thread(new ArrayListBad()); t1.start(); Thread t2 = new Thread(new ArrayListBad()); t2.start(); try t1.join(); t2.join(); catch (InterruptedException e) e.printstacktrace(); System.out.println("Phew, we made it!"); May sometimes work, especially if loop in run() is short. But most likely you ll get some sort of exception: Exception in thread "Thread-1" Thread all done... java.lang.arrayindexoutofboundsexception: 15 at java.util.arraylist.add(unknown Source) at ArrayListBad.run(ArrayListBad.java:12) at java.lang.thread.run(unknown Source) Phew, we made it! 12

public class ArrayListGood implements Runnable private static ArrayList<Integer> list = new ArrayList<Integer>(); public void run() for (int i = 0; i < 100000; i++) synchronized(list) list.add((int) (Math.random() * 100)); System.out.println("Thread all done...");... Adding synchronized block protects the shared static list instance variable, now only one thread is allowed to add at any point in time. 13

Trouble in Concurrency City: Act 3 Deadlock Program stops doing anything useful All you need is 2 objects and 2 threads 14

Programming activity Goal: Increment/decrement all ints in an array Create class NumHolder, holds array 100 integers Create increment() and decrement() methods Methods go through all 100 integers and ++ or -- them Create run() method Loop 1000 times, each loop flip coin and call either increment() or decrement() Create main program in NumHolderLaunch Create a single NumHolder object Create two threads Print out NumHolder object Start threads, wait for them to finish Print out NumHolder again 15