Computer Science 2 06A-Java Multithreading

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
CS 11 java track: lecture 7 This week: Web tutorial:
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Multithreading A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program.
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.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Java Programming: Advanced Topics
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 19 Multithreading.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Internet Software Development Controlling Threads Paul J Krause.
Threads.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multi-Threading in Java
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
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.
1 Chapter 19 Multithreading. 2 Objectives F To understand the concept of multithreading and apply it to develop animation (§19.2). F To develop thread.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
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.
Multithreading (Based on Chapter 29 (Multithreading) Liang book and Chapter 5 (Threads) Elliotte Rusty book) Dr. Tatiana Balikhina 1.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Java Thread Programming
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Principles of Software Development
Multithreading / Concurrency
Chapter 30 Multithreading and Parallel Programming
Chapter 13: Multithreading
Multi Threading.
Java Multithreading.
Multithreading.
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreaded Programming in Java
EE 422C Multithreading & Parallel Programming
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading Chapter 23.
Multithreading.
Multithreading.
Multithreaded Programming
برنامه‌نویسی چندنخی Multi-Thread Programming
Chapter 15 Multithreading
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Computer Science 2 06A-Java Multithreading Sean P. Strout (sps@cs.rit.edu) 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) What is a Process? What happens when you run a Java program? /usr/local/pub/sps/courses/cs2/threads/Process/MyProgram.java Launch 3 instances while monitoring with top On a single CPU architecture, the operating system manages how processes share CPU time process java #1 java #2 java #3 Others… time 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) What is a Process? A process is a running instance of a program including all variables and other states The Java interpreter is the process which runs your program Besides running your program, the JVM must also do other tasks, like managing the memory your code uses (garbage collection) How does the JVM manage multiple tasks within a single process? 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) What is a Thread A thread is a flow of execution of a task in a program Java has built-in support for multithreading Multiple tasks running concurrently within a single process Multiple processes/threads sharing CPU time: T3 Process #1 T1 T2 T3 Process #2 T2 T3 T1 T4 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Why Use Threads? Multithreading can make your program more responsive and interactive, and run faster than a non-threaded version A thread to handle the graphical user interface A thread to handle auto-saving A thread to handle keyboard input A thread to handle background printing 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Java Threads When your program executes as an application, the JVM starts a thread for the main() method When your program runs as an applet, the web browser starts a thread to run the applet Each new thread is an object of a class that: Extends the Thread class –OR- Implements the Runnable interface The new object is referred to as a runnable object 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Creating Threads by Extending Thread A template for developing a custom thread class which extends the Thread class Java.lang.Thread MyThread // Client class public class Client { public void method(…) { // Create thread1 MyThread t1 = new MyThread(…); // Start thread1 t1.start(); // Create thread2 MyThread t2 = new MyThread(…); // Start thread2 t2.start(); } // Custom thread class public class MyThread extends Thread { public MyThread(…) { … } // Override the run method // in Thread public void run() { // Run the thread 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Creating Threads by Extending Thread Write a program that creates and runs three threads: The first thread prints the letter a 5000 times The second thread prints the letter b 5000 times The third thread prints the integers 1 through 5000 We’ll make one thread class to handle the first two threads, PrintChar The third thread will be implemented by the PrintNum class /usr/local/pub/sps/courses/cs2/threads/TestThread/TestThread.java 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Creating Threads by Implementing Runnable A template for developing a custom thread class which implements the Runnable interface Java.lang.Runnable MyThread // Client class public class Client { public void method(…) { // Create thread1 Thread t1 = new Thread ( new MyThread(…)); // Start thread1 t1.start(); // Create thread2 Thread t2 = new Thread ( new MyThread(…)); // Start thread2 t2.start(); } // Custom thread class public class MyThread implements Runnable { public MyThread(…) { … } // Override the run method // in Thread public void run() { // Run the thread 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Creating Threads by Implementing Runnable If your custom thread subclass already inherits from another superclass, you must implement the Runnable interface The interface only requires the run() method to be defined The custom thread class gets wrapped into a Thread object when created Thread t1 = new Thread ( new PrintChar(‘a’, 5000) ); Thread t2 = new Thread ( new PrintChar(‘b’, 5000) ); Thread t3 = new Thread ( new PrintInt(5000) ); /usr/local/pub/sps/courses/cs2/threads/TestRunnable/TestRunnable.java 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Thread class java.lang.Runnable java.lang.Thread + Thread() + Thread(target : Runnable) + run() : void + start() : void + interrupt() : void + isAlive() : boolean + setPriority(p : int) : void + join() : void + sleep(millis : long) : void + yield() : void + isInterrupted() : boolean + currentThread() : Thread 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Thread Control - yield Use yield() to temporarily release CPU time for other threads /usr/local/pub/sps/courses/cs2/threads/TestYield/TestYield.java T1 yields T1 T1 Thread #1 Scheduled time Scheduled time Thread #2 T2 T2 yields 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Thread Control - sleep sleep() causes the thread to cease execution for a specified number of milliseconds /usr/local/pub/sps/courses/cs2/threads/TestSleep/TestSleep.java T1 sleeps T1 wakes up T1 ZzZzZzZzZzZzZzZzZz……… T1 Thread #1 Scheduled time T2 allowed to run T2 must stop running Thread #2 T2 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Thread Control - join Use join() to force one thread to wait for another thread to finish /usr/local/pub/sps/courses/cs2/threads/TestJoin/TestJoin.java join on T2 T2 finished T1 T1 Thread #1 T2 finishes Scheduled time Thread #2 T2 T2 Thread #3 T3 T3 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Thread Control – wait(), notify() and notifyAll() There are three more methods defined in the Object class which are used to facilitate communication among active threads public final void wait() throws InterruptedException Forces the thread to wait until the notify or notifyAll method is called for the object to which wait is called public final void notify() Awakens one of the threads that are waiting on this object. Which one is notified depends on system implementation public final void notifyAll() Awakens all the threads that are waiting on this object 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Thread Control – wait(), notify() and notifyAll() wait on obj T1 allowed to run T1 T1 Thread #1 notify on obj Scheduled time Thread #2 T2 T2 T2 wait on obj Thread #3 T3 Scheduled time 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Threads can be in one of five states Thread States Threads can be in one of five states yield() or timeout Thread created Running start() run() returns New Ready run() Finished interrupt() join() sleep() wait() Wait for target to finish Wait for time out Wait to be notified Timeout notify() or notifyAll() Blocked interrupt() 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Thread States The Running state is the only time the CPU is executing the thread’s code isAlive() returns true if the thread is in the Ready, Blocked or Running state interrupt() interrupts a thread in the following way: If the thread is in the Ready or Running state, its interrupted flag is set and it’s moved to the Blocked state If the thread is currently Blocked, it is awakened and enters the Ready state, and a java.lang.InterruptedException is thrown 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Computer Science 2 (4003-232) isAlive() This solution works, but is there a better method? public class WorkerThread extends Thread { private int result = 0; public void run() { // Perform a complicated time consuming calculation // and store the answer in the variable result } public static void main(String args[]) { WorkerThread t = new WorkerThread(); t.start(); while ( t.isAlive() ); System.out.println( result ); What happens if this statement is left out? Use t.join() instead. 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Thread Priorities Java assigns every thread a priority. By default, a thread inherits the priority of the thread that spawned it Mutator is setPriority() and accessor is getPriority() Priorities range from 1 to 10 Thread.MIN_PRIORITY = 1 Thread.NORM_PRIORITY = 5 Thread.MAX_PRIORITY = 10 A situation known as contention or starving occurs when higher priority threads don’t yield and lower priority threads never get a chance to run /usr/local/pub/sps/courses/cs2/threads/TestPriority/TestPriority.java 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Thread Groups A thread group is a set of threads with similar functionality on which you can perform the same operations Construct a thread group with a unique name: ThreadGroup grp = new ThreadGroup(“thread group”); Using the Thread constructor, place it in the group: Thread t = new Thread(grp, new MyThread(), “label”); To find out how many threads in the group are running: grp.activeCount(); Each thread must be started individually 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Resource Conflict Write a program which launches 100 threads, each of which adds a penny to an account. Assume the account is initially empty. /usr/local/pub/sps/courses/cs2/threads/AccountConflict/AccountConflict.java java.lang.Thread 100 AddAPennyThread AccountConflict Account + run() : void bank : Account Thread : Thread[] + main(args : String[]) : void + balance : int + getBalance() : int + deposit(amt : int) : void 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Resource Conflict Consider this scenario with the previous example: Step balance thread[0] thread[1] 1 0 newBalance = balance+1 2 0 newBalance = balance+1 3 1 balance = newBalance 4 1 balance = newBalance Multiple threads can store the value of the common balance before it is updated individually This is known as a race condition A class is thread-safe if it does not cause a race condition in the presence of multiple threads 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Critical Region To avoid race conditions, you must prevent more than one thread from accessing the critical region of a program Only one thread should be allowed to enter the deposit method at a time class Account: public void deposit(int amount) { int newBalance = balance + amount; balance = newBalance; } 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Synchronizing Instance Methods To synchronize an instance methods means to require a thread to obtain a lock on the object for which the method was invoked To make deposit thread-safe in Account: public synchronized void deposit(int amount) { … } /usr/local/pub/sps/courses/cs2/threads/AccountSync/AccountSync.java class AddAPennyThread: class AddAPennyThread extends Thread { public void run() { account.deposit(1); } Synchronize on the account object 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Synchronizing Instance Methods The preceding scenario with synchronization: Thread[0] Thread[0] Acquire lock on account Waiting to acquire lock on account Enter deposit Release the lock Acquire lock on account balance = 1 Enter deposit Release the lock balance = 2 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Synchronized Statements You could lock on a portion of a method using synchronized statements This can increase concurrency and improve performance class AddAPennyThread: class AddAPennyThread extends Thread { public void run() { synchronized (account) { account.deposit(1); } 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Thread Cooperation Synchronization avoids race conditions by ensuring mutual exclusion to critical regions Threads also need a way to cooperate without requiring threads to finish The Object methods wait(), notify() and notifyAll() must be called in a synchronized method/block or else an IllegalMonitorStateException will occur 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Thread Cooperation The template for coordinating threads: Thread 1 Thread 2 synchronized (obj) { try { // Wait for condition while (!condition) obj.wait(); // Do something… } catch InterruptedException ex) { ex.printStackTrace(); synchronized (obj) { // Do stuff… // When condition is true obj.notify(); } resume Or obj.notifyAll() to wake up all threads 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Producer/Consumer The producer/consumer problem is a classic operating system issue involving threads The idea is you have a producer which creates new resources and adds them to some collection The consumer takes elements out of the same collection, when available, and uses them We must be careful to design a solution which avoids: Producer generating resources which the consumer misses Consumer getting the same element more than once 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

ProducerConsumerTest cubbyHole : CubbyHole number : int + run() : void cubbyHole : CubbyHole number : int + run() : void CubbyHole ProducerConsumerTest contents : int available : boolean + get() : int + put() : int + main(args : String[]) : void /usr/local/pub/sps/courses/cs2/threads/ProducerConsumer1 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Deadlock Sometimes two or more threads need to acquire the locks on several shared objects Deadlock occurs when each thread has the lock on one of the objects and is waiting for the lock on the other Thread 1 Thread 2 synchronized (obj1) { // do stuff… synchronized (obj2) { } synchronized (obj2) { // do stuff… synchronized (obj1) { } Waiting for thread 2 to release lock on obj2 Waiting for thread 1 to release lock on obj1 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Deadlock /usr/local/pub/sps/courses/cs2/threads/Deadlock/Deadlock.java To fix deadlock, use the technique known as resource ordering Assign an order on all the objects whose locks must be acquired and assure each thread acquires the lock in that order Thread 1 Thread 2 synchronized (obj1) { // do stuff… synchronized (obj2) { } synchronized (obj1) { // do stuff… synchronized (obj2) { } 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

Review - Java’s Monitor The mechanism that Java uses to support synchronization is often called the monitor The two types of thread synchronization are: Mutual exclusion using synchronized keeps threads from interfering with one another when sharing data Coooperation via wait and notify allows threads to safely share data to achieve a common goal A monitor region is like a room with some data that only one thread is allowed into at a time 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)

CS2 - 06A-Java Multithreading (v1.00) Computer Science 2 (4003-232) Revision History Revision History v1.00, 1/17/2005 7:35 AM, sps Initial revision. 4/10/2019 CS2 - 06A-Java Multithreading (v1.00)