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

Similar documents
CMSC 330: Organization of Programming Languages

Multi-threading in Java. Jeff HUANG

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

Object Oriented Programming. Week 10 Part 1 Threads

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

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

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

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

7. MULTITHREDED PROGRAMMING

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

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

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

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

Threads Questions Important Questions

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

Threads Chate Patanothai

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

Threads and Java Memory Model

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

Module - 4 Multi-Threaded Programming

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

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

Software Practice 1 - Multithreading

Synchronized Methods of Old Versions of Java

Synchronization

Multithreaded Programming

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

Advanced Programming Concurrency

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

Performance Throughput Utilization of system resources

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

Unit - IV Multi-Threading

Concurrent Programming

CMSC 330: Organization of Programming Languages. Concurrency & Multiprocessing

Multithreaded Programming

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

Java Threads. COMP 585 Noteset #2 1

Concurrency in Java Prof. Stephen A. Edwards

Concurrent Programming using Threads

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

Multithread Computing

CS193k, Stanford Handout #8. Threads 3

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

CS 351 Design of Large Programs Threads and Concurrency

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

CIS233J Java Programming II. Threads

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Introduction to Java Threads

CMSC 132: Object-Oriented Programming II

UNIT V CONCURRENT PROGRAMMING

Animation Part 2: MoveableShape interface & Multithreading

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

CS 556 Distributed Systems

What is a thread anyway?

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

Java Threads. Introduction to Java Threads

Threads, Concurrency, and Parallelism

Problems with Concurrency. February 19, 2014

Multithreading Pearson Education, Inc. All rights reserved.

SUMMARY INTRODUCTION CONCURRENT PROGRAMMING THREAD S BASICS. Introduction Thread basics. Thread states. Sequence diagrams

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

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

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

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

Threads and Parallelism in Java

THREADS & CONCURRENCY

Threads in Java (Deitel & Deitel)

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

Chapter 32 Multithreading and Parallel Programming

Advanced Concepts of Programming

Multithreading using Java. Dr. Ferdin Joe John Joseph

Overview of Java Threads (Part 2)

Programming Java. Multithreaded Programming

Principles of Software Construction: Concurrency, Part 2

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

EDAF65 Processes and Threads

EDA095 Processes and Threads

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

THREADS AND CONCURRENCY

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

Model Requirements and JAVA Programs MVP 2 1

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

JAVA - MULTITHREADING

Dr.-Ing. Michael Eichberg

Only one thread can own a specific monitor

Multiple Inheritance. Computer object can be viewed as

CST242 Concurrency Page 1

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

User Space Multithreading. Computer Science, University of Warwick

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

F. Tip and M. Weintraub CONCURRENCY

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

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

Techniques of Java Programming: Concurrent Programming in Java

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Transcription:

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

Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read & write files to disk Perform useful computation (editor, browser, game) How does computer do everything at once? Multitasking Multiprocessing 2

Multitasking (Time-Sharing) Approach Computer does some work on a task Computer then quickly switch to next task Tasks managed by operating system (scheduler) Computer seems to work on tasks concurrently Can improve performance by reducing waiting 3

Multitasking Can Aid Performance Single task Two tasks 4

Multiprocessing (Multithreading) Approach Multiple processing units (multiprocessor) Computer works on several tasks in parallel Performance can be improved Dual-core AMD Athlon X2 32 processor Pentium Xeon Titan at ORNL 5

Perform Multiple Tasks Using Processes Process Definition à executable program loaded in memory Has own address space Ø Variables & data structures (in memory) Each process may execute a different program Communicate via operating system, files, network May contain multiple threads 6

Perform Multiple Tasks Using Threads Thread Sequentially executed stream of instructions Has own execution context Ø Program counter, call stack (local variables) Communicate via shared access to data Also known as lightweight process 7

Motivation for Multithreading Captures logical structure of problem May have concurrent interacting components Can handle each component using separate thread Simplifies programming for problem Example Web Server uses threads to handle Multiple simultaneous web browser requests 8

Motivation for Multithreading Better utilize hardware resources When a thread is delayed, compute other threads Given extra hardware, compute threads in parallel Reduce overall execution time Example Multiple simultaneous web browser requests Handled faster by multiple web servers 9

Programming with Threads Concurrent programming Writing programs divided into independent tasks Tasks may be executed in parallel on multiprocessors Multithreading Executing program with multiple threads in parallel Special form of multiprocessing 10

Creating Threads in Java Two approaches to create threads Extending Thread class (NOT RECOMMENDED) Runnable interface approach (PREFERED) 11

Extending Thread class We overload the Thread class run() method The run() methods defines the actual task the thread performs Example public class MyThread extends Thread { public void run( ) { // work for thread MyThread t = new MyThread( ) ;// create thread t.start( ); // begin running thread // thread executing in parallel 12

Runnable interface Define a class (worker) that implements the Runnable interface public interface Runnable { public void run(); // work done by thread Create thread to execute the run() method Ø Alternative 1: Create thread object and pass worker object to Thread constructor Ø Alternative 2: Hand worker object to an executor Example public class Worker implements Runnable { public void run( ) { // work for thread Thread t = new Thread(new Worker( )); // create thread t.start(); // begin running thread // thread executing in parallel 13

Extending Thread Approach Not Recommended Not a big problem for getting started But a bad habit for industrial strength development Methods of worker and Thread class intermixed Hard to migrate to more efficient approaches Thread Pools 14

Thread Class public class Thread extends Object implements Runnable { public Thread(); public Thread(String name); // Thread name public Thread(Runnable R); public Thread(Runnable R, String name); public void run(); public void start(); eventually it can run... // if no R, work for thread // thread gets in line so it 15

More Thread Class Methods public class Thread extends Object { public static Thread currentthread() public String getname() public void interrupt() // alternative to stop (deprecated) public boolean isalive() public void join() public void setdaemon() public void setname() public void setpriority() public static void sleep() public static void yield() 16

Creating Threads in Java Note Thread eventually starts executing only if start() is called Runnable is interface Ø So it can be implemented by any class Ø Required for multithreading in applets Do not call the run method directly 17

Threads Thread States Java thread can be in one of these states New à thread allocated & waiting for start() Runnable à thread can begin execution Running à thread currently executing Blocked à thread waiting for event (I/O, etc.) Dead à thread finished Transitions between states caused by Invoking methods in class Thread Ø new(), start(), yield(), sleep(), wait(), notify() Other (external) events Ø Scheduler, I/O, returning from run() In Java states defined by Thread.State http://docs.oracle.com/javase/7/docs/api/java/lang/thread.state.html 18

Threads Thread States State diagram new start new scheduler runnable running yield, time slice notify, notifyall, IO complete, sleep expired, join complete blocked terminate IO, sleep, wait, join dead Running is a logical state indicates runnable thread is actually running 19

Daemon Threads Java threads types User Daemon Ø Provide general services Ø Typically never terminate Ø Call setdaemon() before start() Program termination All user threads finish Daemon threads are terminated by JVM 20

Threads Scheduling Scheduler Determines which runnable threads to run Ø When context switching takes place Can be based on thread priority Part of OS or Java Virtual Machine (JVM) Scheduling policy Non-preemptive (cooperative) scheduling Preemptive scheduling 21

Threads Non-preemptive Scheduling Threads continue execution until Thread terminates Executes instruction causing wait (e.g., IO) Thread volunteering to stop (invoking yield or sleep) 22

Threads Preemptive Scheduling Threads continue execution until Same reasons as non-preemptive scheduling Preempted by scheduler 23

Thread Scheduling Observations Order thread is selected is indeterminate Depends on scheduler Scheduling may not be fair Some threads may execute more often Thread can block indefinitely (starvation) If other threads always execute first Your code should work correctly regardless the scheduling policy in place 24

Java Thread Example public class ThreadNoJoin extends Thread { public void run() { for (int i = 0; i < 3; i++) { try { sleep((int)(math.random() * 5000)); // 5 secs catch (InterruptedException e) { e.printstacktrace(); System.out.println(i); public static void main(string[] args) { Thread t1 = new ThreadNoJoin(); Thread t2 = new ThreadNoJoin(); t1.start(); t2.start(); System.out.println("Done"); To understand this example better, let s assume we want to make a sandwich 25

Java Thread Example Output Possible outputs 0,1,2,0,1,2,Done // thread 1, thread 2, main() 0,1,2,Done,0,1,2 // thread 1, main(), thread 2 Done,0,1,2,0,1,2 // main(), thread 1, thread 2 0,0,1,1,2,Done,2 // main() & threads interleaved main (): thread 1, thread 2, println Done thread 1: println 0, println 1, println 2 thread 2: println 0, println 1, println 2 26

Thread Class join( ) Method Can wait for thread to terminate with join( ) Method prototype public final void join( ) Ø Returns when thread is done Ø Throws InterruptedException if interrupted 27

Java Thread Example (Join) public class ThreadJoin extends Thread { public void run() { for (int i = 0; i < 3; i++) { try { sleep((int)(math.random()*5000));// 5 secs catch (InterruptedException e) { e.printstacktrace(); System.out.println(i); public static void main(string[] args) { Thread t1 = new ThreadJoin(); Thread t2 = new ThreadJoin(); t1.start(); t2.start(); try { t1.join(); t2.join(); catch (InterruptedException e) { e.printstacktrace(); System.out.println("Done"); 28

About Join Important: You will limit the concurrency level if you do not start/join correctly Suppose you want to run many threads concurrently. Start them all and then execute the join for each one. Do not start one thread, then join on that thread, start the second one, join on that thread, etc. The following is WRONG! t1.start() t1.join() t2.start() t2.join() Feel free to use arrays, sets, etc., to keep track of your threads 29

Terminating Threads A thread ends when the run() method ends Sometimes we may need to stop a thread before it ends For example, you may have created several threads to find a problem solution and once one thread finds it, there is no need for the rest How to stop thread? Using stop() method à WRONG! This is a deprecated method. Using it can lead to problems when data is shared Using interrupt() method Ø Ø This method does not stop the thread. Instead, it notifies the thread that it should terminate. The method sets a boolean variable in the thread and that value can be checked by the thread (by using the method interrupted()) It is up to the thread to terminate or not Ø public void run() { while(!thread.interrupted()) { // work // release resource, cleaning tasks 30

Thread Example Swing uses a single-threaded model Long computations in the EDT freezes the GUI Example: Progress Bar Example 31

Example x = 0 initially. Then these threads are executed: T1 y = x; T2 z = x; x = y+1; x = z+2; What is the value of x afterward 3 1 2 T1 y = x; T2 x = y+1; T1 y = x; T2 x = y+1; z = x; x = z+2; z = x; x = z+2; T1 T2 z = x; x = z+2; y = x; x = y+1; T1 T2 z = x; y = x; x = y+1; x = z+2; 32

Data Races That was an example of a data race Threads are racing to read, write x The value of x depends on who wins (3, 1, 2) Languages rarely specify who wins data races The outcome is nondeterministic So programmers restrict certain outcomes Synchronization with locks, condition variables And they often mess up Leading to bugs that are hard to track down 33

Thread API Concepts Thread management Creating, killing, joining (waiting for) threads Sleeping, yielding, prioritizing Synchronization Controlling order of execution, visibility, atomicity Locks: Can prevent data races, but watch out for deadlock! Condition variables: supports communication between threads Most languages have similar APIs, details differ 34

Synchronization Example public class Example extends Thread { private static int cnt = 0; public void run() { synchronized (this) { int y = cnt; cnt = y + 1; Acquires the lock associated w/ current object; only succeeds if lock not held by another thread, otherwise blocks Releases the lock 35

Condition Variables A condition variable represents a set of threads waiting for a condition to become true Implemented, at least conceptually, as a wait set Since different threads may access the variable at once, we protect the wait set with a lock Thus avoiding possible data races 36

Synchronization, the traditional way public class Example extends Thread { private static int cnt = 0; static Object lock = new Object(); public void run() { synchronized (lock) { int y = cnt; cnt = y + 1; Object uses as a Lock Acquires the intrinsic lock; only succeeds if lock not held by another thread, otherwise blocks Releases the lock when exiting block 37

Synchronization, with explicit Locks public class Example extends Thread { private static int cnt = 0; static Lock lock = new ReentrantLock(); public void run() { lock.lock(); int y = cnt; cnt = y + 1; lock.unlock(); Lock, for protecting the shared state Acquires the lock; only succeeds if lock not held by another thread, otherwise blocks Releases the lock 38

Producer / Consumer Solution Lock lock = new ReentrantLock(); Condition ready = lock.newcondition(); boolean bufferready = false; Object buffer; void produce(object o) { lock.lock(); while (bufferready) ready.await(); buffer = o; bufferready = true; ready.signalall(); lock.unlock(); Object consume() { lock.lock(); while (!bufferready) ready.await(); Object o = buffer; bufferready = false; ready.signalall(); lock.unlock(); return o; Uses single condition per lock (like intrinsics) 39

Producer / Consumer Solution Lock lock = new ReentrantLock(); Condition producers = lock.newcondition(); Condition consumers = lock.newcondition(); boolean bufferready = false; Object buffer; void produce(object o) { lock.lock(); while (bufferready) producers.await(); buffer = o; bufferready = true; consumers.signalall(); lock.unlock(); Object consume() { lock.lock(); while (!bufferready) consumers.await(); Object o = buffer; bufferready = false; producers.signalall(); lock.unlock(); return o; Uses 2 conditions per lock for greater efficiency 40

Producer / Consumer Solution Lock lock = new ReentrantLock(); Condition producers = lock.newcondition(); Condition consumers = lock.newcondition(); boolean bufferready = false; Object buffer; void produce(object o) { lock.lock(); while (bufferready) producers.await(); buffer = o; bufferready = true; consumers.signal (); lock.unlock(); Object consume() { lock.lock(); while (!bufferready) consumers.await(); Object o = buffer; bufferready = false; producers.signal (); lock.unlock(); return o; Wakes up only one thread: More efficient, still! 41