Multithreading Horstmann ch.9. Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
1 Race Conditions When threads share access to a common object/data, they can conflict with each other and mess up the consistency of the object/data.
1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Chapter 9 (Horstmann’s Book) Multithreading Hwajung Lee.
Multithreading. RHS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.
©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.
1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Multithreading. 2 Threads Program units that execute independently; multiple threads run “simultaneously” Virtual machine executes each thread for short.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
ThreadThread Thread Basics Thread Synchronization Animations.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Chapter 21 – Multithreading Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSE 219 Computer Science III Multithreaded Issues.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading. Chapter Goals To understand how multiple threads can execute in parallel To learn how to implement threads To understand race conditions.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
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.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Multithreading. DCS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.
Multithreading / Concurrency
Multi Threading.
Practice Session 8 Lockfree LinkedList Blocking queues
Multithreading Chapter 9.
Chapter 19 Java Never Ends
Threads Chate Patanothai.
ITEC324 Principle of CS III
Threads II IS
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Multithreading 2 Lec 24.
Using threads for long running tasks.
Threads and Multithreading
Chapter 9 Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Multithreading Horstmann ch.9

Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library Animation example

Single vs. Multiple Threads Finish each line of bullets before starting new line Allow simultaneous drawing of several lines ThreadTester1 ThreadTester2

Single vs. Multiple Threads Using single thread: in method actionPerformed:... for (int i=0; i<10; i++) { ”draw single bullet”; Thread.sleep(500); }...

Running Threads public class MyRunnable implements Runnable { public void run() { thread action } }... Runnable r = new MyRunnable(); Thread t = new Thread(r); t.start(); Specify action Construct thread from Runnable object Start thread

Single vs. Multiple Threads Using single thread: in method actionPerformed:... for (int i=0; i<10; i++) { ”draw single bullet”; Thread.sleep(500); }... Using multiple threads: In method actionPerformed: Runnable r = new Runnable() { public void run() { } Thread t = new Thread(r); t.start();

QUIZ public static void main(String[] args) { Runnable r1 = new Producer(); Runnable r2 = new Producer(); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); } How many threads? 1.None 2.One 3.Two 4.Three 5.I don’t know

Starting Two Threads public static void main(String[] args) { Runnable r1 = new Producer(); Runnable r2 = new Producer(); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); }

Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library Animation example

Thread states Each thread has –State (new, runnable, blocked, dead), and –priority Thread blocked when Sleeping Waiting for I/O Waiting to acquire lock Waiting for condition Scheduler activates the runnable thread of max priority if a thread has completed its time slice a thread has blocked itself a thread with higher priority has become runnable

Thread scheduling public static void main(String[] args) { Runnable r1 = new Producer(“goddag”); Runnable r2 = new Producer(“farvel”); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); } Output depends on scheduler: “goddag” “farvel” “goddag” “farvel” … public class Producer implements Runnable { public Producer(String a) { greeting = a; } public void run() { try { for (int i = 1; i <= 10; i++) { System.out.println(i + ": " + greeting); Thread.sleep(100); } } catch (InterruptedException e) {} } private String greeting; }

QUIZ Thread T1 based on run method public void run { try { Thread.sleep(100000); } catch (InterruptedException e) {} } Thread T2 based on run method public void run { factor( ) } Which thread(s) become blocked? 1.None 2.T1 3.T2 4.Both 5.I don’t know factor method from ”noter” ch. 1

Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library Animation example

Thread interaction Recursive Fibonacci –May take long time –Window freezes Compute in separate threads –Old slow thread may overwrite result from new fast thread? –Interrupt old threads! ThreadTester3

Terminating Threads Thread terminates when run exits Or ask thread to finish by calling interrupt Thread may check for interrupt by call Thread.currentThread().isInterr upted() sleep, wait throw InterruptedException when thread interrupted

private Thread t = null; public void actionPerformed(ActionEvent e) { if (t!=null) t.interrupt(); Runnable r = new Runnable() { public void run() { try { long res = fib(Integer.parseInt(input.getText())); if (!Thread.currentThread().isInterrupted()) recResult.setText(res+""); } catch (InterruptedException e) {} } }; t = new Thread(r); t.start(); } private long fib(int n) throws InterruptedException { if (Thread.currentThread().isInterrupted()) throw new InterruptedException(); if (n <= 1) return 1; else return fib(n - 1) + fib(n - 2); } Code of anonymous class implementing ActionListener

QUIZ public class Test1 implements Runnable { public void run() { try { for (int i=1; i<=10; i++) { System.out.println(i); Thread.sleep(1000); } } catch (InterruptedException e) { } } } The catch statement is empty? 1.Compiler error 2.Legal code, but better style to omit try-catch 3.Legal code, try-catch is necessary, but empty catch is bad style 4.Legal code, try-catch is necessary, empty catch is good style 5.I don’t know

Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library Animation example

Shared Resource Producer Thread ”goddag” Producer Thread ”farvel” Consumer Thread Queue object Bounded Queue: max 10 elements

Producer Thread int i = 1; while (i <= 100) { if (!queue.isFull()) { queue.add(i + ": " + greeting); i++; } Thread.sleep((int)(Math.random() * DELAY)); }

Consumer Thread int i = 1; while (i <= 100) { if (!queue.isEmpty()) { Object greeting = queue.remove(); System.out.println(greeting); i++; } Thread.sleep((int)(Math.random() * DELAY)); }

Expected Program Output 1: goddag 1: farvel 2: goddag 3: goddag... 99: farvel 100: farvel Possible output... 10: goddag 11: farvel... 15: goddag 11: farvel... Why?

Queue: Circular Array Implementation public void add(E newValue) { elements[tail] = newValue; tail++; size++; if (tail == elements.length) { tail = 0; }

Race Condition First thread calls add and executes elements[tail] = newValue; First thread at end of time slice Second thread calls add and executes elements[tail] = newValue; tail++; Second thread at end of time slice First thread executes tail++;

QUIZ Producer Thread: BoundedQueue queue = new BoundedQueue (10); for (int i = 1; i <= 100; i++) { queue.add(i + ": " + greeting); Thread.sleep((int)(Math.random() * DELAY)); } Consumer Thread: BoundedQueue queue = new BoundedQueue (10); for (int i = 1; i <= 100; i++) { Object greeting = queue.remove(); System.out.println(greeting); Thread.sleep((int)(Math.random() * DELAY)); } Could race condition occur when running these thread in parallel? 1.Yes 2.No 3.I don’t know

Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library Animation example

Locks Thread can acquire lock When another thread tries to acquire same lock, it blocks When first thread releases lock, other thread is unblocked and tries again Two kinds of locks –Objects of class implementing java.util.concurrent.Lock interface type, usually ReentrantLock –Locks that are built into every Java object

private Lock aLock = new ReentrantLock();... public void add(E newValue) { aLock.lock(); try { elements[tail] = newValue; tail++; size++; if (tail==elements.length) tail = 0; } finally { aLock.unlock(); } Consumer or producer thread executing lock() has exclusive access to add/remove methods – even when time slice is up – until unlock() public E remove() { aLock.lock(); try { E r = (E) elements[head]; head++; size--; if (head==elements.length) head = 0; return r; } finally { aLock.unlock(); }

Producer Thread int i = 1; while (i <= 100) { if (!queue.isFull()) { queue.add(i + ": " + greeting); i++; } Thread.sleep((int)(Math.random() * DELAY)); } Good: add is protected by lock Bad: Another producer thread may fill up the queue between isFull() check and add() Solution: Must move isFull() check inside protected add()

QUIZ public void add(E newValue) throws InterruptedException { aLock.lock(); try { while( isFull() ) { Thread.sleep(1000); }... } finally {aLock.unlock();} } Does it work? 1: No, race condition may still occur 2: Yes, race condition cannot occur, and this code works fine 3: ??? – race condition cannot occur – but a new problem arises… 4: I don’t know isfull() check is now inside lock protected add()

QUIZ public void add(E newValue) throws InterruptedException { aLock.lock(); try { while( isFull() ) { Thread.sleep(1000); }... } finally {aLock.unlock();} } Does it work? 1: No, race condition may still occur 2: Yes, race condition cannot occur, and this code works fine 3: ??? – race condition cannot occur – but a new problem arises… 4: I don’t know isfull() check is now inside lock protected add() ”Deadlock”: Since the queue remains locked, no consumer thread can execute remove() and make isFull() false

Using condition object to avoid deadlock private Lock aLock = new ReentrantLock(); private Condition spaceAvailableCondition = aLock.newCondition(); public void add(E newValue) throws InterruptedException { aLock.lock(); try { while (isFull()) spaceAvailableCondition.await();... } finally {aLock.unlock();} } public E remove() throws InterruptedException { aLock.lock(); try {... spaceAvailableCondition.signalAll(); return r; } finally {aLock.unlock();} } Calling await blocks the thread and releases the lock Calling signalAll() unblocks all threads awaiting this condition (they remain blocked until getting the lock)

Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library Animation example

Object Locks Each object has a lock Calling a synchronized method acquires lock of implicit parameter Leaving the synchronized method releases lock Easier than explicit Lock objects public class BoundedQueue { public synchronized void add(E newValue) {... } public synchronized E remove() {... }... }

Object Locks Each implicit lock has one associated (anonymous) condition object wait() blocks current thread and adds it to wait set notifyAll() unblocks waiting threads public synchronized void add(E newValue) throws InterruptedException { while (isFull()) wait(); elements[tail] = anObject;... notifyAll(); } Just one condition object Used by both threads waiting due to full queue and due to empty queue

QUIZ What is not reason for blocking a thread? 1.Sleeping 2.Waiting for I/O 3.Time slice is up 4.Waiting to acquire lock 5.Waiting for a condition 6.I don’t know

QUIZ When is InterruptedException thrown? 1.when Thread.sleep(..) returns 2.when.await() returns 3.when other thread calls this threads interrupt method while this thread is blocked 4.I don’t know

Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library Animation example

Use java.util.concurrent library Main thread: BlockingQueue queue = new LinkedBlockingQueue (10); Producer Thread: for (int i = 1; i <= 100; i++) { queue.put(i + ": " + greeting); Thread.sleep((int)(Math.random() * DELAY)); } Consumer Thread: for (int i = 1; i <= 100; i++) { Object greeting = queue.take(); System.out.println(greeting); Thread.sleep((int)(Math.random() * DELAY)); } Synchronized queue with space for 10 elements Producer thread is made to wait until queue has available space Consumer thread is made to wait until queue is nonempty

Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library Animation example

Algorithm Animation Use thread to make progress in algorithm Display algorithm state Example: Animate MergeSorter (similar to java.util.Arrays.sort ) Pause inside compare method Pass custom comparator Comparator comp = new Comparator () { public int compare(Double d1, Double d2) { draw current state pause thread return d1.compareTo(d2); } };

Algorithm Animation

public class Sorter implements Runnable { public Sorter(Double[] values, ArrayComponent panel) { this.values = values; this.panel = panel; } public void run() { Comparator comp = new Comparator () { public int compare(Double d1, Double d2) { panel.setValues(values, d1, d2); try { Thread.sleep(DELAY); } catch (InterruptedException exception) { Thread.currentThread().interrupt(); } return d1.compareTo(d2); }; MergeSorter.sort(values, comp); panel.setValues(values, null, null); } private Double[] values; private ArrayComponent panel; private static final int DELAY = 100; } array being sorted

Pausing and Running the Animation Want to pause animation until "Run" or "Step" button is clicked Need to coordinate UI thread, animation thread Try to use built-in thread-safe construct in java.util.concurrent Trick: Use a blocking queue Button click adds string "Run" or "Step" to queue Animation thread calls take on the queue, blocks if no string inserted BlockingQueue queue;

User Interface Thread stepButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { queue.add("Step"); runButton.setEnabled(true); } }); runButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { runButton.setEnabled(false); queue.add("Run"); } });

Animation Thread public int compare(Double d1, Double d2) { try { String command = queue.take(); if (command.equals("Run")) { Thread.sleep(DELAY); if (!"Step".equals(queue.peek())) queue.add("Run"); } } catch (InterruptedException exception) { Thread.currentThread().interrupt(); } panel.setValues(values, d1, d2); return d1.compareTo(d2); }... private BlockingQueue queue;