Introduction to Java Threads

Similar documents
Chapter 19 Multithreading

Java Threads. Introduction to Java Threads

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Chapter 32 Multithreading and Parallel Programming

Java Threads. COMP 585 Noteset #2 1

Concurrent Programming using Threads

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

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Multithread Computing

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Advanced Concepts of Programming

Threads Chate Patanothai

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

Threads & Timers. CSE260, Computer Science B: Honors Stony Brook University

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

7. MULTITHREDED PROGRAMMING

Basics of. Multithreading in Java

Multithreaded Programming

Concurrency CSCI 201 Principles of Software Development

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

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

Animation Part 2: MoveableShape interface & Multithreading

Java Threads. Written by John Bell for CS 342, Spring 2018

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Parallel Programming Practice

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Concurrency in Java Prof. Stephen A. Edwards

Parallel Programming Practice

Programming Language Concepts: Lecture 11

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

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

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

CS11 Java. Fall Lecture 7

Object Oriented Programming. Week 10 Part 1 Threads

Multi-threading in Java. Jeff HUANG

Module - 4 Multi-Threaded Programming

CS 556 Distributed Systems

CMSC 330: Organization of Programming Languages

Threads and Parallelism in Java

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

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

COMP346 Winter Tutorial 4 Synchronization Semaphores

CS 351 Design of Large Programs Threads and Concurrency

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

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

Definition: A thread is a single sequential flow of control within a program.

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

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

Unit - IV Multi-Threading

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Object Oriented Programming (II-Year CSE II-Sem-R09)

public class Shared0 { private static int x = 0, y = 0;

User Space Multithreading. Computer Science, University of Warwick

Monitors CSCI 201 Principles of Software Development

COE518 Lecture Notes Week 7 (Oct 17, 2011)

What is a Thread? Why Multicore? What is a Thread? But a fast computer runs hot THREADS AND CONCURRENCY. Concurrency (aka Multitasking)

THREADS AND CONCURRENCY

CMSC 132: Object-Oriented Programming II

Multiple Inheritance. Computer object can be viewed as

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

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

Synchronization in Java

Principles of Software Construction: Concurrency, Part 2

Synchronization in Concurrent Programming. Amit Gupta

Concurrent Programming

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

Multithreading using Java. Dr. Ferdin Joe John Joseph

CMSC 330: Organization of Programming Languages

Performance Throughput Utilization of system resources

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

Programming in Parallel COMP755

Synchronized Methods of Old Versions of Java

Programming Java. Multithreaded Programming

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

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

CS 159: Parallel Processing

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

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

CSCD 330 Network Programming

CSCD 330 Network Programming

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

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

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

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Only one thread can own a specific monitor

Advanced Programming Concurrency

THREADS & CONCURRENCY

Multi-threaded programming in Java

Threads, Concurrency, and Parallelism

THREADS. Laboratório - Java

CS193k, Stanford Handout #8. Threads 3

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Concurrent Computing CSCI 201 Principles of Software Development

Threads and Concurrency in Java

Threads Questions Important Questions

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

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Transcription:

Object-Oriented Programming Introduction to Java Threads RIT CS 1 "Concurrent" Execution Here s what could happen when you run this Java program and launch 3 instances on a single CPU architecture. The operating system manages how processes share CPU time. public class MyProgram { public static void main(string args[]) { int i = 0; while ( true ) { i = i + 1; 2

Modern Multicore CPU What if there are two cores? The operating system's scheduler can allow two programs to run concurrently. public class MyProgram { public static void main(string args[]) { int i = 0; while ( true ) { i = i + 1; 3 What's in a Process? The Java interpreter handles many things, for example, managing memory for your 4 code, including garbage collection. To the computer's operating system, the interpreter is the true program. The Java class code is the program's input. Each process* gets its own memory segment, input/output file descriptors, and other system resources. *more about this in the next course

Threads Sharing memory and other resources between processes is awkward. Need to start processes separately and negotiate resource sharing through the OS. How to develop a program where multiple sequences of execution share resources? Answer: threads OS level (POSIX) pthreads Within a single Java program: class Thread 5 What is a Thread? A thread is a flow of execution. Java has built-in multithreading. Multiple tasks run concurrently in 1 process All the threads in all the processes share CPU time. Another example with 1-core CPU: Process #1 T1 T2 T3 T3 Process #2 T2 T3 T1 T4 6

Java Threads When a program executes, the Java Virtual Machine (JVM) starts a "main" thread to run public static void main(string[]) in the class you specify. The code that executes can start new threads to run concurrently (truly or simulated by fewer cores) within the single process. 7

Ways to create threads You can create a thread of execution by: 1) Extending the Thread class --- or --- 2) Implementing the Runnable interface 9 1. Creating Threads by Extending Thread The example class extends Thread. Java.lang.Thread // Custom thread class public class MyThread extends Thread { public MyThread( ) { MyThread @Override public void run() { // Concurrent // code here // 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(); 10

Example: TestThread Create and run 3 threads. First thread prints the letter a 5000 times. Second thread prints the letter b 5000 times. Third thread prints integers 1 through 5000. One class handles the first two threads: PrintChar A second class handles the third thread: PrintNum 11 2. Creating Threads using External Runnable Using Runnable is more flexible. // Runnable class public class MyStuff implements Runnable { public MyStuff( ) { @Override public void run() { // Concurrent // code here // Client class public class Client { public void method( ) { // Create thread1 Thread t1 = new Thread(new MyStuff( )); // Start thread1 t1.start(); // Create thread2 Thread t2 = new Thread(new MyStuff( )); // Start thread2 t2.start(); 12

Example: TestRunnable Create and run 3 threads First thread prints the letter a 5000 times. Second thread prints the letter b 5000 times. Third thread prints integers 1 through 5000. PrintChar and PrintNum classes are now Runnable implementors. 13 A Common Mistake Every Thread class contains these two methods: run() start() But only one makes concurrency happen! See TestNonThread.java. 14

Lambdas and Threads Runnable is a functional interface: An interface with only one abstract method If code for run() is small, use a lambda. See LambdaThread.java 15 Thread : Selected Methods java.lang.runnable java.lang.thread Underlined methods are static 16 + 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

Thread Control - sleep sleep() pauses thread execution for a specified number of milliseconds. See TestSleep.java sleep( 500 ) Time's up. T1 wakes and runs Thread #1 T1 ZzZz waiting...zzzz... T1 T2 executes T2 stops executing Thread #2 T2 18 Thread Control - join join() makes one thread wait for another thread to finish. See TestJoin.java T2.join() T1 resumes Thread T1 T1 T1 T2 finishes Thread T2 T2 T2 Thread has terminated Thread T3 T3 T3 19

Resource Conflict Scenario Example Situation: AccountConflict A program launches 100 threads, each of which adds a penny to an account. Assume the account is initially empty. What might happen? java.lang.thread AddAPenny 100 AccountConflict Account + run() : void 20 - bank : Account - Thread : Thread[] + main(args : String[]) : void + balance : int + getbalance() : int + deposit(amt : int) : void Java Thread States new Thread() New start() Blocked** Thread becomes unlocked, or unblocked via: notify(), notifyall() sleep(t) wait() join() Thread chooses to wait. Or, thread tries to access a locked, synchronized block Runnable scheduled (Running*) run() returns Terminated 21 yield() or scheduler kicks thread out *Running is not an actual Thread state. It represents the thread currently executing. **There are several kinds of blocked states.

Thread States The JVM manages thread scheduling and 22 execution using thread states. Runnable is the only "state" in which the thread may be executing on a core. On an N-core system, only N threads may be actually running at any moment. Other thread states record the situation of a thread with respect to its execution. t.isalive() returns true if the thread is not in the New or Terminated state. isalive() 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 ); 23

isalive() 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? 24 isalive() 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? Why might this approach be a problem? 25

Thread Interactions & Issues Coordinating Thread Execution 26 Dangers of Concurrency Multiple threads can run the same code. Consider x = x + 1. In byte or machine code, it could be compiles to multiple statements: Read value of x from memory; Increment value, and Store value into memory for x. Think about multiple threads in the middle of this code sequence and... the blatant example in AccountConflict. 27

Resource Conflict Scenario One thread could update the value of the common, shared balance before other(s) complete their update individually This is called a race condition. A class is thread-safe if it does not cause a race condition in the presence of multiple threads running its methods. 28 Step Balance Thread[ 0 ] Thread[ 1 ] In execution Actions Actions 1 0 newbalance = balance+1 2 0 newbalance = balance+1 3 1 balance = newbalance 4 1 balance = newbalance Methods for Thread-Safe Objects To make sure that an object's data does not get corrupted by thread concurrency, some methods should only let one thread in at a time. To make sure only one thread executes at a time inside a method, you must lock the object instance's monitor. The synchronized keyword, when applied to a method, adds the needed logic. 29

Critical Region To avoid race conditions, program code must prevent more than one thread from executing in a critical region. Code in this region may change state shared by multiple threads. Only one thread must be allowed to enter the deposit method at a time 30 public synchronized void deposit(int amount) { int newbalance = balance + amount; balance = newbalance; Critical Region account.deposit( 1 ); Wait until the lock on the Account object's monitor is available. Set the Account object's monitor's lock. public synchronized void deposit(int amount) { int newbalance = balance + amount; balance = newbalance; Release the Account object's monitor's lock. // calling code resumes 31

Synchronizing Instance Methods The scenario with synchronization: Thread 0 gets there first; thread 1 blocks Thread[0] Thread[1] Acquire lock on account Enter deposit Release the lock balance = 1 Blocks trying to acquire lock on account Thread[1] cannot execute here Acquire lock on account AccountSync.java solves the resource conflict problem. 33 Enter deposit Release the lock balance = 2 Synchronized Statements A synchronized block can lock inside a portion of a method. This may shorten lock time to increase concurrency and improve performance. Smaller critical region better performance In AccountSync.java 34 class Account { public void deposit( int amt ) { synchronized( this ) { int newbalance = balance + amount; Balance = newbalance;

Contention & Starvation A situation known as contention may occur when threads compete for execution time on the CPU. A thread might not give other threads a chance to run, and That may lead to starvation What's needed are ways to make threads coordinate and cooperate with each other. 35 Thread Coordination and Cooperation Thread Synchronization 36

Thread Cooperation wait(), notify() and notifyall() Three Object methods support coordination among active threads: Object.wait() blocks the executing thread. Object.notify() releases another waiting thread. Object.notifyAll() releases all waiting threads. It is legal to call these methods only inside a synchronized method or block that has locked the object. 37 Thread Cooperation wait(), notify() and notifyall() public final void wait() throws InterruptedException 38 Forces the thread to wait until a notify or notifyall method is called on the object. If notify never happens, thread blocks forever. public final void notify() Awakens one thread waiting on the object. Which one thread is implementation dependent! public final void notifyall() Running Blocked Blocked Runnable Blocked Runnable Wakes all threads waiting on the object. JVM Scheduler decides which gets to run first.

Thread Cooperation wait(), notify() and notifyall() Threads running within one process 3 Threads, one obj. obj.wait() T1 notified and resumes Thread #1 T1 T1 obj.notify() Thread #2 T2 T2 T2 obj.wait() T3 is still waiting... Thread #3 T3 39 Threads and Interruptions A thread may be interrupted, but it is not necessarily a reliable form of signaling from one thread to another. wait(), sleep() and join() must catch InterruptedException. However, if a thread is interrupted at some other time, the only change is that a flag is set. Therefore the thread must check the flag value. 40

Monitor Structure The monitor structure combines 41 synchronized, wait and notify to coordinate synchronized methoda(){ while ( condition ) { try { // Wait for condition this.wait(); Thread 1 executing amonitor.methoda catch (InterruptedException ex){ //.. // Do something critical Resumes Thread 2 executing amonitor.methodb synchronized methodb(){ // Do things // When condition is // false, then this.notify(); Or this.notifyall() to wake up all threads waiting on this monitor. Forms of Thread Cooperation Mutual exclusion uses synchronized to keep threads from interfering with one another when sharing data. A simple form of cooperation uses join to keep one thread from running until the other 42 is done. Cooperation uses wait and notify so that threads can safely coordinate sharing data. The monitor is like a room where one thread executes at a time.

How They Work notify() arbitrarily picks some thread waiting on the same monitor, and allows it to continue. notifyall() lets all such threads go. The wait() call is particularly complex. 1) Release the monitor's synchronization lock. 2) Block until someone notifies this same monitor. 3) Attempt to reacquire the monitor's lock. 4) Once acquired, go into the Runnable state 43 and "wait" some more... YOU WILL FORGET THIS notify(), notifyall(), and wait() must be called inside a block of code that is synchronized on some object's monitor. Otherwise, you get IllegalMonitorStateException 44

Producer-Consumer Classic Concurrency Pattern The producer creates new elements and puts them in a collection so that they are available to one or more consumers. A consumer takes an element out of the collection and uses it. You carefully design a solution to avoid: Producer creating items that consumers miss; Consumer getting the same item many times or getting something bogus when nothing is available. 45 Producer-Consumer See the packages prodcomm1, prodcomm2, and prodcomm3. ProducerConsumerTest + main(args : String[]) : void Producer - cubbyhole : Box - number : int + run() : void Box - contents : int - available : boolean Consumer - cubbyhole : Box - number : int + run() : void 46 + get() : int + put(int)

Consumer Waiting Behavior In this sequence, the consumer must wait. Name: Consume 1 Package: Dynamic View Version: 1.0 Author: BKSteele What happens when the producer tries to put when the cubby hole is 'not full' -- i.e. there is space to put more. theproducer :Producer 1.2 put(value) thecubby :CubbyHole 1.0 int= get() 1.1 [available == false ] wait 1.3 [available == false ] notifyall 1.5 notifyall 1.4 int= get() theconsumer :Consumer When the consumer tries to get when nothing is available, the cubbyhole monitor makes the consumer's thread wait. when the producer thread comes along, it is able to immediately put in the value. Then it must notify any threads that may be waiting for that data. 1.6 [available == true ] value Notification from the producer's put operation finally causes the consumer to come out of its wait and get the newly available data value. This is NOT A SECOND get() call; the thread was blocked waiting for something to notify the (consumer) thread when there is a new value available. 47 Producer Waiting Behavior Name: Produce 1 Package: Dynamic View Version: 1.0 Author: BKSteele In this sequence, the producer must wait. theproducer :Producer thecubby :CubbyHole theconsumer :Consumer 1.0 put(value) What happens when the producer tries to put when the cubby hole is 'not full' -- i.e. there is space to put more. 1.1 [available == true]: wait 2.0 int= get() 2.1 notifyall 2.2 value A consumer eventually comes along to get what's in the cubby and notify any waiting threads after changing the cubby's available state to false. 3.0 put(value) 3.1 [available == false] proceed to put 48 Notification from the consumer's get operation finally causes the producer to come out of its wait and put another data value into the cubby. This is NOT A SECOND put() call; the thread was blocked waiting for something to notify the (producer) thread when the cubby can accept the put of a new value.