java multithreading

28
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park

Upload: s-r-krishnan

Post on 12-Nov-2014

2.951 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Multithreading

Multithreading in Java

Nelson Padua-PerezBill Pugh

Department of Computer ScienceUniversity of Maryland, College Park

Page 2: Java Multithreading

Problem

Multiple tasks for computerDraw & display images on screenCheck keyboard & mouse inputSend & receive data on networkRead & write files to diskPerform useful computation (editor, browser, game)

How does computer do everything at once?MultitaskingMultiprocessing

Page 3: Java Multithreading

Multitasking (Time-Sharing)

ApproachComputer does some work on a taskComputer then quickly switch to next taskTasks managed by operating system (scheduler)

Computer seems to work on tasks concurrentlyCan improve performance by reducing waiting

Page 4: Java Multithreading

Multitasking Can Aid PerformanceSingle task

Two tasks

Page 5: Java Multithreading

Multiprocessing (Multithreading)

ApproachMultiple processing units (multiprocessor)Computer works on several tasks in parallelPerformance can be improved

4096 processorCray X1

32 processorPentium Xeon

Dual-core AMDAthlon X2

Page 6: Java Multithreading

Perform Multiple Tasks Using…

ProcessDefinition – executable program loaded in memoryHas own address space

Variables & data structures (in memory)Each process may execute a different programCommunicate via operating system, files, networkMay contain multiple threads

Page 7: Java Multithreading

Perform Multiple Tasks Using…

ThreadDefinition – sequentially executed stream ofinstructionsShares address space with other threadsHas own execution context

Program counter, call stack (local variables)Communicate via shared access to dataMultiple threads in process execute same programAlso known as “lightweight process”

Page 8: Java Multithreading

Web Server usesthreads to handle …

Multiple simultaneousweb browser requests

Motivation for Multithreading

Captures logical structure of problemMay have concurrent interacting componentsCan handle each component using separate threadSimplifies programming for problem

Example

Page 9: Java Multithreading

Multiple simultaneousweb browser requests…

Handled faster bymultiple web servers

Motivation for Multithreading

Better utilize hardware resourcesWhen a thread is delayed, compute other threadsGiven extra hardware, compute threads in parallelReduce overall execution time

Example

Page 10: Java Multithreading

Multithreading Overview

Motivation & backgroundThreads

Creating Java threadsThread statesScheduling

SynchronizationData racesLocksWait / Notify

Page 11: Java Multithreading

Programming with Threads

Concurrent programmingWriting programs divided into independent tasksTasks may be executed in parallel on multiprocessors

MultithreadingExecuting program with multiple threads in parallelSpecial form of multiprocessing

Page 12: Java Multithreading

Creating Threads in Java

You have to specify the work you want thethread to doDefine a class that implements the Runnableinterface

public interface Runnable { public void run();}

Put the work in the run methodCreate an instance of the worker class andcreate a thread to run it

or hand the worker instance to an executor

Page 13: Java Multithreading

Thread Classpublic class Thread {

public Thread(Runnable R); // Thread ⇒ R.run() public Thread(Runnable R, String name);

public void start(); // begin thread execution ...}

Page 14: Java Multithreading

More Thread Class Methodspublic class Thread { …

public String getName(); public void interrupt(); public boolean isAlive(); public void join(); public void setDaemon(boolean on); public void setName(String name); public void setPriority(int level);

public static Thread currentThread();

public static void sleep(long milliseconds); public static void yield();}

Page 15: Java Multithreading

Creating Threads in Java

Runnable interfaceCreate object implementing Runnable interfacePass it to Thread object via Thread constructor

Examplepublic class MyT implements Runnable { public void run() { … // work for thread }}Thread t = new Thread(new MyT()); // create threadt.start(); // begin running thread… // thread executing in parallel

Page 16: Java Multithreading

Alternative (Not Recommended)

Directly extend Thread classpublic class MyT extends Thread {

public void run() { … // work for thread }}MyT t = new MyT(); // create threadt.start(); // begin running thread

Page 17: Java Multithreading

Why not recommended?

Not a big problem for getting startedbut a bad habit for industrial strength development

The methods of the worker class and theThread class get all tangled up

Makes it hard to migrate to Thread Pools andother more efficient approaches

Page 18: Java Multithreading

Threads – Thread States

Java thread can be in one of these statesNew – thread allocated & waiting for start()Runnable – thread can executeBlocked – thread waiting for event (I/O, etc.)Terminated – thread finished

Transitions between states caused byInvoking methods in class Thread

start(), yield(), sleep()Other (external) events

Scheduler, I/O, returning from run()…

Page 19: Java Multithreading

Threads – Thread StatesState diagram

runnablenew

terminated

blocked

new start

terminate

IO, sleep, join,request lock

IO complete,sleep expired,join complete,acquire lock

Page 20: Java Multithreading

Threads – Scheduling

SchedulerDetermines which runnable threads to runCan be based on thread priorityPart of OS or Java Virtual Machine (JVM)Many computers can run multiple threadssimultaneously (or nearly so)

Page 21: Java Multithreading

Java Thread Examplepublic class ThreadExample implements Runnable { public void run() { for (int i = 0; i < 3; i++)

System.out.println(i);

} public static void main(String[] args) { new Thread(new ThreadExample()).start();

new Thread( new ThreadExample()).start(); System.out.println("Done"); }}

Page 22: Java Multithreading

Java Thread Example – Output

Possible outputs0,1,2,0,1,2,Done // thread 1, thread 2, main()0,1,2,Done,0,1,2 // thread 1, main(), thread 2Done,0,1,2,0,1,2 // main(), thread 1, thread 20,0,1,1,2,Done,2 // main() & threads interleaved

thread 1: println 0, println 1, println 2

main (): thread 1, thread 2, println Done

thread 2: println 0, println 1, println 2

Page 23: Java Multithreading

Daemon ThreadsWhy doesn’t the program quit as soon as Done isprinted?Java threads types

UserDaemon

Provide general servicesTypically never terminateCall setDaemon() before start()

Program terminationIf all non-daemon threads terminate, JVM shuts down

Page 24: Java Multithreading

Might not see different interleavings

The threads in that example are too shortEach started thread will probably completebefore the next thread starts

Let’s make more threads that run longer

Page 25: Java Multithreading

Data Racespublic class DataRace implements Runnable { static volatile int x; public void run() { for (int i = 0; i < 10000; i++) { x++; x--; } } public static void main(String[] args) throws Exception { Thread [] threads = new Thread[100]; for (int i = 0; i < threads.length; i++) threads[i] = new Thread(new DataRace()); for (int i = 0; i < threads.length; i++) threads[i].start(); for (int i = 0; i < threads.length; i++) threads[i].join(); System.out.println(x); // x not always 0! }}

Page 26: Java Multithreading

Why volatile

We’ll spend more time on volatile laterBut volatile tells the compiler:

other threads might see reads/writes of this variabledon’t change/reorder eliminate the reads and writes

An optimizing compiler should, if it seesx++; x--;

replace it with a no-opif x isn’t volatile

Page 27: Java Multithreading

Thread Scheduling Observations

Order thread is selected is indeterminateDepends on scheduler, timing, chance

Scheduling is not guaranteed to be fairSome schedules/interleavings can causeunexpected and bad behaviors

Synchronizationcan be used to control thread execution order

Page 28: Java Multithreading

Using Synchronizationpublic class DataRace implements Runnable { static volatile int x; static Object lock = new Object(); public void run() { for (int i = 0; i < 10000; i++)

synchronized(lock) { x++; x--; } } public static void main(String[] args) throws Exception { Thread [] threads = new Thread[100]; for (int i = 0; i < threads.length; i++) threads[i] = new Thread(new DataRace()); for (int i = 0; i < threads.length; i++) threads[i].start(); for (int i = 0; i < threads.length; i++) threads[i].join(); System.out.println(x); // x always 0! }}