concurrent programming, threads

83
1 Concurrent Programming, Concurrent Programming, Threads Threads James Atlas James Atlas July 8, 2008 July 8, 2008

Upload: brynne-stanley

Post on 03-Jan-2016

63 views

Category:

Documents


5 download

DESCRIPTION

Concurrent Programming, Threads. James Atlas July 8, 2008. Review. Swing/GUIs. Multitasking. What is it?. Multitasking. What is it? Ability to have more than one program running at the same time Or making it look like this is happening. Multitasking. What are goals of multitasking? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Concurrent Programming, Threads

11

Concurrent Programming, Concurrent Programming, ThreadsThreads

James AtlasJames Atlas

July 8, 2008July 8, 2008

Page 2: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 22

ReviewReview

• Swing/GUIsSwing/GUIs

Page 3: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 33

MultitaskingMultitasking

• What is it?What is it?

Page 4: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 44

MultitaskingMultitasking

• What is it?What is it?• Ability to have more than one program Ability to have more than one program

running at the same timerunning at the same timeOr making it Or making it looklook like this is happening like this is happening

Page 5: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 55

MultitaskingMultitasking

• What are goals of multitasking?What are goals of multitasking?

• When are “smart” times to switch between When are “smart” times to switch between tasks?tasks?

CPU

Task 1

Task 2

Page 6: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 66

MultitaskingMultitasking

• Goal: keep CPU completely utilizedGoal: keep CPU completely utilized

• PoliciesPolicies Assign priorities to threadsAssign priorities to threads Switch when one task has to pause for I/OSwitch when one task has to pause for I/O Set time intervalSet time interval

Page 7: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 77

MultithreadingMultithreading

• ProcessProcess: a “program” that can be : a “program” that can be multitasked multitasked To view the processes on your machine: To view the processes on your machine: psps or or

Task ManagerTask Manager

• MultithreadedMultithreaded programs (or programs (or concurrentconcurrent programs)programs)one program can appear to be doing multiple one program can appear to be doing multiple

things at one timethings at one time

• ThreadThread or or thread of executionthread of execution A task; a light-weight processA task; a light-weight process

• How is a thread different than a process?How is a thread different than a process?

Page 8: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 88

Threads vs. ProcessesThreads vs. Processes

• A process runs in its own contextA process runs in its own context Seems to have its own CPU, memory, Seems to have its own CPU, memory,

registers, …registers, … Data is not shared between processesData is not shared between processes

• A thread also seems to have its own CPU, A thread also seems to have its own CPU, execution stack, and registersexecution stack, and registers All of the threads in a program share the same All of the threads in a program share the same

variables and datavariables and data Threads run in the context of a programThreads run in the context of a program

Page 9: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 99

Processes in UNIXProcesses in UNIX

• ‘‘&’ runs a process in the background&’ runs a process in the background

• Can use Control-Z to “suspend” processCan use Control-Z to “suspend” process fg: continue process in foregroundfg: continue process in foreground bg: put process in the background, e.g., to get bg: put process in the background, e.g., to get

use of terminal backuse of terminal back

Page 10: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1010

Advantages of MultithreadingAdvantages of Multithreading

• Program can do multiple things, while one Program can do multiple things, while one task is paused or waiting on somethingtask is paused or waiting on somethingAn internet browser can communicate with An internet browser can communicate with

multiple hosts, open an email editing window, multiple hosts, open an email editing window, and render imagesand render images

• Java uses multithreadingJava uses multithreadingruns a garbage-collection thread in the runs a garbage-collection thread in the

background to deal with memory managementbackground to deal with memory management

Page 11: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1111

Concurrent vs. Parallel ProgramsConcurrent vs. Parallel Programs

• What is the difference?What is the difference?

Page 12: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1212

Concurrent vs. Parallel ProgramsConcurrent vs. Parallel Programs

• Parallel program:Parallel program: performs more than one performs more than one task on task on more than one processormore than one processorE.g., one task on each processorE.g., one task on each processor

• A concurrent program performs more than A concurrent program performs more than one task on one task on oneone processorprocessorLogically/usually, one task/threadLogically/usually, one task/threadIn reality, only one thread runs at a timeIn reality, only one thread runs at a timeEach thread runs for a short period of timeEach thread runs for a short period of timeAfter interval, the After interval, the thread schedulerthread scheduler chooses chooses

another thread to runanother thread to run

Page 13: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1313

Using Threads in JavaUsing Threads in Java

• Use the java.util.Timer class (and others)Use the java.util.Timer class (and others)

• Extend the java.lang.Thread classExtend the java.lang.Thread class

• Implement java.lang.Runnable interfaceImplement java.lang.Runnable interface

Page 14: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1414

java.util.Timerjava.util.Timer• Execute a task after a delayExecute a task after a delay• Execute a task at a specific timeExecute a task at a specific time• Each task is represented as a subclass Each task is represented as a subclass

of of TimerTaskTimerTask with the with the run()run() method method overriddenoverridden

• Tasks should not take a lot of CPU timeTasks should not take a lot of CPU time• Timer starts on constructionTimer starts on construction

Different from threads…Different from threads…

• javax.swing.Timerjavax.swing.Timer Uses an ActionListener insteadUses an ActionListener instead

Reminder.java

Page 15: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1515

Creating a Thread: Approach 1Creating a Thread: Approach 1

• Create a thread by extending the Create a thread by extending the ThreadThread classclass

• A Thread class object must have a A Thread class object must have a runrun() () methodmethodruns when the thread startsruns when the thread startscauses the thread to exit when the run() method causes the thread to exit when the run() method

endsends

• ExampleExamplea thread that will sleep (wait) for a specified a thread that will sleep (wait) for a specified

amount of timeamount of timethen wakes up and prints its infothen wakes up and prints its info

Page 16: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1616

class PrintThread extends Thread {private int sleepTime;public PrintThread(String name) {

super(name);sleepTime = (int)(Math.random()* 5000d);System.out.println(“Name:” + getName() +

“; Sleep:” + sleepTime);}public void run() {

try {System.out.println(getName() +

“ going to sleep.”);Thread.sleep(sleepTime);

} catch (InterruptedException exp) {System.out.println(exp);

}System.out.println(getName() +

“ done sleeping.”);}

} See corrected version

Page 17: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1717

Creating a Thread: Approach 1Creating a Thread: Approach 1

• A thread is created by instantiating an object A thread is created by instantiating an object of a class derived from Threadof a class derived from Thread

• Thread class constructor takes one Thread class constructor takes one parameter: the parameter: the namename of the newly created of the newly created threadthreadName can be any string you wantName can be any string you want

• Helps programmer to keep track of the threads in Helps programmer to keep track of the threads in the programthe program

• Can see in Eclipse debugCan see in Eclipse debugAdditional constructorsAdditional constructors

Page 18: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1818

Creating a Thread: Approach 2Creating a Thread: Approach 2

• What if want a class that is already a What if want a class that is already a subclass to be a thread?subclass to be a thread?Implement the Implement the RunnableRunnable interface interfaceImplement a Implement a runrun() method() method

Page 19: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 1919

Creating a Thread: Approach 2Creating a Thread: Approach 2

• A Thread object must still be created to run the A Thread object must still be created to run the threadthread Pass the Runnable object to the constructor for the Pass the Runnable object to the constructor for the

Thread classThread class

Can name the thread object as well…Can name the thread object as well…

• Registers the run() method of the runnableObject Registers the run() method of the runnableObject as the code to run when this Thread object’s start() as the code to run when this Thread object’s start() method is calledmethod is called

public Thread( Runnable runnableObject )

public Thread( Runnable runnableObject, String threadName )

Page 20: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2020

Other OptionsOther Options

• java.swing.Timerjava.swing.Timer For use in GUIsFor use in GUIs

• java.swing.SwingWorkerjava.swing.SwingWorker Executing threads in the backgroundExecuting threads in the background

Page 21: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2121

Thread LifetimeThread Lifetime• A thread can be in one of four states at any A thread can be in one of four states at any

given moment in timegiven moment in time NewNew RunnableRunnable BlockedBlocked DeadDead

Page 22: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2222

Thread States: NewThread States: New

• When a thread has just been createdWhen a thread has just been created

• In this state, the program has not yet started In this state, the program has not yet started executing the thread’s internal code executing the thread’s internal code

• Can only call start()Can only call start() Otherwise, get IllegalThreadStateExceptionOtherwise, get IllegalThreadStateException

• After the thread’s start() method is called, it After the thread’s start() method is called, it enters the enters the runnable staterunnable state..

Page 23: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2323

Thread States: RunnableThread States: Runnable

• Thread is ready to executeThread is ready to execute• Might not be actually running on the CPUMight not be actually running on the CPU

Operating system must schedule the threadOperating system must schedule the threadWhen OS schedules thread and the thread is When OS schedules thread and the thread is

actually running, Java still considers the thread actually running, Java still considers the thread runnablerunnable

• In most concurrent programming models, In most concurrent programming models, there is a distinction between the runnable there is a distinction between the runnable and running statesand running states

Page 24: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2424

Thread States: RunnableThread States: Runnable

• Each OS has policies for scheduling Each OS has policies for scheduling threadsthreads

• Most Java-supporting OSes support Most Java-supporting OSes support preemptivepreemptive multithreading and multithreading and time-slicingtime-slicingallows one thread to run, then interrupting it to allows one thread to run, then interrupting it to

allow another thread to runallow another thread to run

• Remember: a runnable thread may or may Remember: a runnable thread may or may not be running at any given momentnot be running at any given moment

Page 25: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2525

Thread States: BlockedThread States: Blocked• A thread enters the A thread enters the blocked stateblocked state whenever: whenever:

The thread calls its sleep() methodThe thread calls its sleep() method The thread calls an operation that blocks on input/outputThe thread calls an operation that blocks on input/output

• operation does not return from the call until the I/O operation does not return from the call until the I/O operations are completeoperations are complete

The thread calls wait() methodThe thread calls wait() method The thread tries to lock an object that is currently locked The thread tries to lock an object that is currently locked

by another thread by another thread A different thread calls the suspend() method of the A different thread calls the suspend() method of the

threadthread• suspend() is deprecated; don’t call itsuspend() is deprecated; don’t call it

• When a thread is blocked, another thread is When a thread is blocked, another thread is scheduled to runscheduled to run

Page 26: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2626

Thread States: BlockedThread States: Blocked• When a thread is reactivated, it returns to When a thread is reactivated, it returns to

the the runnablerunnable state state

• The thread scheduler checks if the The thread scheduler checks if the reactivated thread has a higher priority than reactivated thread has a higher priority than the currently running threadthe currently running threadIf thread.getPriority() > running.getPriority()If thread.getPriority() > running.getPriority()

• scheduler preempts currently running thread to scheduler preempts currently running thread to schedule reactivated, higher-priority threadschedule reactivated, higher-priority thread

Page 27: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2727

Thread States: BlockedThread States: Blocked

• A thread returns to the A thread returns to the runnablerunnable state from the state from the blockedblocked state state If the thread called sleep(), the specified number of If the thread called sleep(), the specified number of

milliseconds to sleep has passedmilliseconds to sleep has passed If the thread is waiting for blocked I/O, the specified I/O If the thread is waiting for blocked I/O, the specified I/O

request completedrequest completed If the thread called wait(), some other thread must call If the thread called wait(), some other thread must call

notify() or notifyAll()notify() or notifyAll() If the thread is waiting for an object lock held by another If the thread is waiting for an object lock held by another

thread, other thread must release the lock thread, other thread must release the lock

• One-to-one correspondence between ways to enter One-to-one correspondence between ways to enter the blocked state and ways to return from the the blocked state and ways to return from the blocked stateblocked state

Page 28: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2828

Thread States: DeadThread States: Dead• A thread can enter the A thread can enter the dead statedead state in one of in one of

two cases:two cases: It dies naturally when the run() method exits It dies naturally when the run() method exits

normallynormally It dies abnormally because the run() method It dies abnormally because the run() method

generated an uncaught exceptiongenerated an uncaught exception

• isAlive() methodisAlive() method To find out if a thread is currently aliveTo find out if a thread is currently alive returns true if the thread is runnable or blockedreturns true if the thread is runnable or blocked returns false if the thread is new or deadreturns false if the thread is new or dead

Page 29: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 2929

Thread States: Possible Thread States: Possible TransitionsTransitions

new

runnable

dead

blocked

start()

run method exits

sleep()

done sleeping

wait()

notify()

blockon I/O

I/Ocomplete

Not exhaustive

Page 30: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3030

Thread StatesThread States

• getState()getState()NEWRUNNABLEBLOCKED

• Waiting on a monitorWAITINGTIMED_WAITINGTERMINATED

Page 31: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3131

Interrupting ThreadsInterrupting Threads

• A thread dies when its run() method endsA thread dies when its run() method endsthe only way a thread can end the only way a thread can end

• A thread should periodically test to see if it A thread should periodically test to see if it should terminate…should terminate…

public void run() {while (no die request and still work to do){ do work }

// die request or no more work, so end and thread dies}

Page 32: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3232

Interrupting ThreadsInterrupting Threads

• Threads frequently sleep or wait to give other Threads frequently sleep or wait to give other threads a chance to do work (to use the CPU)threads a chance to do work (to use the CPU)

• When a thread is sleeping, it cannot actively When a thread is sleeping, it cannot actively check to see if it should terminate check to see if it should terminate

• Java provides an Java provides an interruptinterrupt() method() methodWhen this method is called on a thread that is When this method is called on a thread that is

currently blocked, the blocking call (sleep() or currently blocked, the blocking call (sleep() or wait()) is terminated with an wait()) is terminated with an InterruptedExceptionInterruptedException• Checked exception Checked exception What does that mean?

Page 33: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3333

Interrupting ThreadsInterrupting Threads

• Interrupting a thread using the interrupt() Interrupting a thread using the interrupt() method does not necessarily mean that the method does not necessarily mean that the thread should terminatethread should terminateGrabs the attention of the threadGrabs the attention of the threadLike poking at a sleeping roommateLike poking at a sleeping roommate

• Place code that will decide how to react to Place code that will decide how to react to the interruption inside the the interruption inside the catchcatch clause of the clause of the run() methodrun() method

Page 34: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3434

public void run() {try {

// main thread execution loopwhile (more work to do){

do this work}

}catch (InterruptedException exp)// thread was interrupted during sleep/wait{

. . .}

// exit the run method, so the thread dies}

The main execution loop calls sleep or wait. If the thread isinterrupted during one of these calls, the catch clause runs.

Page 35: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3535

Interrupting ThreadsInterrupting Threads

• If the example thread is interrupted when it If the example thread is interrupted when it is not sleeping or waiting, no is not sleeping or waiting, no InterruptedException is generatedInterruptedException is generatedthe thread must check that it has not been the thread must check that it has not been

interrupted during the main execution loopinterrupted during the main execution loopinterrupted()interrupted()

• returns true if the thread has been interruptedreturns true if the thread has been interrupted

while (!interrupted() && more work to do){ // main thread execution loop }

Page 36: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3636

Waiting for Thread CompletionWaiting for Thread Completion

• It is possible to wait for a thread to It is possible to wait for a thread to complete. complete. Call the thread’s join() methodCall the thread’s join() methodFor instance, to wait for thread1 to complete For instance, to wait for thread1 to complete

call…call…

• A timeout value can also be specifiedA timeout value can also be specifiedif the thread does not die within the specified if the thread does not die within the specified

timeout, an timeout, an InterruptedExceptionInterruptedException is generated is generated

thread1.join();

Page 37: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3737

Waiting for Thread CompletionWaiting for Thread Completion

• To wait for 500 milliseconds for thread1 to To wait for 500 milliseconds for thread1 to die, specify the timeout value in the call to the die, specify the timeout value in the call to the join() methodjoin() method

try {thread1.join(500);System.out.println(“Thread exited.”);

}catch (InterruptedException exp1) {

System.out.println(“Timeout expired!”);}

Page 38: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3838

Thread PrioritiesThread Priorities

• Each thread has a Each thread has a priorityprioritya numerical representation of the relative a numerical representation of the relative

“importance” of the thread running, compared to “importance” of the thread running, compared to the other threadsthe other threads

• A thread’s priority can be set using the A thread’s priority can be set using the setPriority() method setPriority() method

• A thread’s priority must be between A thread’s priority must be between MIN_PRIORITY and MAX_PRIORITYMIN_PRIORITY and MAX_PRIORITYConstants, defined as 1 and 10Constants, defined as 1 and 10NORM_PRIORITY, defined as 5NORM_PRIORITY, defined as 5

Page 39: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 3939

Thread Priorities and SchedulingThread Priorities and Scheduling• Fixed-priority SchedulingFixed-priority Scheduling

Whenever the thread scheduler has to schedule a new Whenever the thread scheduler has to schedule a new thread to run, it generally picks the thread to run, it generally picks the highest-priorityhighest-priority runnablerunnable thread thread

• The highest-priority thread keeps running until The highest-priority thread keeps running until eithereither it yields by calling the yield() methodit yields by calling the yield() method it becomes non-runnable (dying or entering the blocked it becomes non-runnable (dying or entering the blocked

state)state) a higher-priority thread becomes runnablea higher-priority thread becomes runnable

• What happens if there is more than one runnable What happens if there is more than one runnable thread with the same highest priority?thread with the same highest priority?

Page 40: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4040

Thread Priorities and SchedulingThread Priorities and Scheduling

• What happens if there is more than one What happens if there is more than one runnable thread with the same highest runnable thread with the same highest priority?priority?Up to the underlying operating system’s policyUp to the underlying operating system’s policy

• It doesn’t matter which thread is chosenIt doesn’t matter which thread is chosenThere is no guarantee this is a fair process!There is no guarantee this is a fair process!

• OS could pick a random choice or pick the first of OS could pick a random choice or pick the first of the highest-priority threadsthe highest-priority threads

Page 41: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4141

Program Termination and Program Termination and ThreadsThreads• Whenever at least one thread is alive Whenever at least one thread is alive

(runnable or blocked), the program/process (runnable or blocked), the program/process the thread(s) belong to is still activethe thread(s) belong to is still active

• Even if the main() method exits, an alive Even if the main() method exits, an alive thread will prevent a program from thread will prevent a program from terminatingterminating

• A daemon thread will not prevent a program A daemon thread will not prevent a program from terminatingfrom terminating

Page 42: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4242

Daemon ThreadsDaemon Threads• Daemon thread: Daemon thread: a thread that runs solely for the a thread that runs solely for the

benefit of other threadsbenefit of other threads Example: the Java garbage collectorExample: the Java garbage collector

• Daemon threads are exactly like normal threads Daemon threads are exactly like normal threads with one differencewith one difference A live daemon thread will not stop the JVM from A live daemon thread will not stop the JVM from

terminatingterminating You need to have at least one non-daemon thread to You need to have at least one non-daemon thread to

keep the program alivekeep the program alive

• To set a thread to be a To set a thread to be a daemondaemon thread, call thread, call setDaemon() before calling start()setDaemon() before calling start()

• Use isDaemon() to see if a thread is running as a Use isDaemon() to see if a thread is running as a daemondaemon

Page 43: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4343

Thread GroupsThread Groups

• To simplify working with multithreaded To simplify working with multithreaded programs, Java introduces the concept of programs, Java introduces the concept of thread groupsthread groups

• Thread GroupThread Group: a convenient way of : a convenient way of grouping related threadsgrouping related threads

• In an internet browser, multiple threads In an internet browser, multiple threads may load different images all to be may load different images all to be displayed on the same pagedisplayed on the same pageIf the user clicks the Stop button, all of these If the user clicks the Stop button, all of these

threads should be interrupted, so all of these threads should be interrupted, so all of these threads are in the same thread groupthreads are in the same thread group

Page 44: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4444

Thread GroupsThread Groups

• Create a new thread group with a name…Create a new thread group with a name…

• When a thread object is constructed, When a thread object is constructed, specify its group in the constructor…specify its group in the constructor…

• To see if any threads in a group are To see if any threads in a group are runnable, call activeCount(), which returns runnable, call activeCount(), which returns an integer number of an integer number of runnablerunnable threads in threads in the thread groupthe thread group

ThreadGroup imgThrs = new ThreadGroup(“ImageThreads”);

Thread thr1 = new Thread(imgThrs, “Image Thread 1”);

if (imgThrs.activeCount()) > 0

Page 45: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4545

Thread GroupsThread Groups

• An entire group of threads can be An entire group of threads can be interrupted at the same timeinterrupted at the same time

• Thread groups can have child subgroupsThread groups can have child subgroups• A newly created thread group is, by default, A newly created thread group is, by default,

a child of the current thread groupa child of the current thread group• Methods such as Methods such as activeCountactiveCount() and () and

interruptinterrupt() work on () work on all threadsall threads in their in their groupgroup and in and in all child groupsall child groups

imgThrs.interrupt(); // interrupts all threads in the thread group

Page 46: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4646

Thread GameThread Game

Page 47: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4747

Java-memory model (JMM)Java-memory model (JMM)

Page 48: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4848

Java-memory model RulesJava-memory model Rules

• AtomicityAtomicity access/update to anything except long/doubleaccess/update to anything except long/double

• VisibilityVisibility changes not always guaranteed visiblechanges not always guaranteed visible

• synchronization doessynchronization does

• volatile variablesvolatile variables

• Thread.join()Thread.join()

• OrderingOrdering instructions in-thread are as-if-serialinstructions in-thread are as-if-serial out-of-thread no guaranteeout-of-thread no guarantee

Page 49: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 4949

Thread SynchronizationThread Synchronization

• In many multithreaded applications, more In many multithreaded applications, more than one thread will need to access the than one thread will need to access the same datasame dataCan turn into a problem when two threads have Can turn into a problem when two threads have

access to the same object and then both modify access to the same object and then both modify the state of the objectthe state of the object

Depending on the order of data access, the Depending on the order of data access, the object can become corruptedobject can become corrupted

Known as a Known as a race conditionrace condition

Page 50: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5050

Thread SynchronizationThread Synchronization

• Suppose we have a program that is handling Suppose we have a program that is handling transactions for a banktransactions for a bank

• For each transfer, the program starts a For each transfer, the program starts a separate thread to perform the transferseparate thread to perform the transfer

• The thread gathers the necessary The thread gathers the necessary information and calls the information and calls the transfertransfer() method of () method of the bank objectthe bank object

Page 51: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5151

class Bank {int num_transfers;. . .public void transfer(int from, int to,

int amount){

if (accounts[from] < amount)return;

accounts[from] -= amount;accounts[to] += amount;num_transfers++;

}}

The transfer function deducts the transfer amount from the indicated account and adds it to the indicated account.

Page 52: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5252

Thread SynchronizationThread Synchronization

• Suppose we have two transfer threads that Suppose we have two transfer threads that run at oncerun at onceRemember that a thread can be interrupted at Remember that a thread can be interrupted at

any timeany time

• Note that the statement Note that the statement accounts[from] -= amount;accounts[from] -= amount;

is really composed of three partsis really composed of three parts

(1) loading accounts[from] into a register(1) loading accounts[from] into a register

(2) subtracting amount from it(2) subtracting amount from it

(3) saving the result back into memory(3) saving the result back into memory

Page 53: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5353

Thread SynchronizationThread Synchronization

• The program has two transfer threads The program has two transfer threads running and both call transfer() on the Bank running and both call transfer() on the Bank objectobjectEach will execute these three stepsEach will execute these three steps

• Where is the potential problem?Where is the potential problem?

Thread 1:

LOAD ACCOUNTS[from], R1ADD R1, amountSAVE R1, ACCOUNTS[from]

Thread 2:

LOAD ACCOUNTS[from], R1ADD R1, amountSAVE R1, ACCOUNTS[from]

Page 54: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5454

Thread SynchronizationThread Synchronization

• Since a thread can be interrupted at any time Since a thread can be interrupted at any time for another thread to run, this order of for another thread to run, this order of execution is possible…execution is possible…

THR1: LOAD ACCOUNTS[from], R1THR1: ADD R1, amount

THR2: LOAD ACCOUNTS[from], R1THR2: ADD R1, amountTHR2: SAVE R1, ACCOUNTS[from]

THR1: SAVE R1, ACCOUNTS[from]

Thread 1 gets preempted before it would have written its new data out

Thread 2 runs and updates this value

Thread 1 runs again and overwrites Thread 2’s changes!

Page 55: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5555

Atomic OperationsAtomic Operations

• The statement that subtracts the amount The statement that subtracts the amount from the account (and the one that adds from the account (and the one that adds the amount to the account) is not an the amount to the account) is not an atomic operationatomic operation..

• Atomic operationAtomic operation: an operation that is : an operation that is guaranteed to run to completion once it guaranteed to run to completion once it has begunhas begunThe subtract and add operations can be The subtract and add operations can be

interrupted after they started and before they interrupted after they started and before they completecomplete

Page 56: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5656

Atomic OperationsAtomic Operations

• If the transfer method was guaranteed to If the transfer method was guaranteed to run to completion once it was entered, this run to completion once it was entered, this race condition could not happenrace condition could not happen

• Traditionally, atomic operations are Traditionally, atomic operations are implemented using mutexes, semaphores, implemented using mutexes, semaphores, and critical sectionsand critical sections More in your OS class!More in your OS class!

• Java introduces a Java introduces a monitormonitor, a simpler way of , a simpler way of handling atomic operationshandling atomic operations

Page 57: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5757

Synchronized MethodsSynchronized Methods

• A method or block of code can be marked A method or block of code can be marked as atomicas atomicUsing keyword Using keyword synchronized synchronized in method’s in method’s

return typereturn typeAfter the section of code starts, the code is After the section of code starts, the code is

guaranteed to complete before another thread guaranteed to complete before another thread can enter another synchronized section can enter another synchronized section belonging to that same objectbelonging to that same object

Page 58: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5858

class Bank {int num_transfers;. . .public synchronized void transfer

(int from, int to, int amount){

if (accounts[from] < amount) return;accounts[from] -= amount;accounts[to] += amount;num_transfers++;

}}

The transfer function is atomic (synchronized).After it begins, it cannot be preempted until it completes.

Page 59: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 5959

Synchronized Methods & ObjectsSynchronized Methods & Objects

• So, what does making a method So, what does making a method synchronizedsynchronized do? do?

public void transfer(int from, int to, int amount)

{synchronized (this) {

if (accounts[from] < amount) return;accounts[from] -= amount;accounts[to] += amount;num_transfers++;

}}

Page 60: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6060

Synchronized Methods & ObjectsSynchronized Methods & Objects

• So, what does making a method So, what does making a method synchronizedsynchronized do? do?• When a thread calls a synchronized method of an When a thread calls a synchronized method of an

object, that object becomes lockedobject, that object becomes locked There is exactly There is exactly one sharedone shared key or lock for an object key or lock for an object To enter any To enter any synchronizedsynchronized method of that object, you method of that object, you

must have the keymust have the key When you have the key, you unlock the door to the When you have the key, you unlock the door to the

synchronized method you want to enter and take the key synchronized method you want to enter and take the key with youwith you

When another thread attempts to enter a synchronized When another thread attempts to enter a synchronized method, it cannot find the key, so it blocksmethod, it cannot find the key, so it blocks• Other thread waits on the doorstep to the object for the Other thread waits on the doorstep to the object for the

key to returnkey to return

Page 61: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6161

Synchronized Methods & ObjectsSynchronized Methods & Objects When you return from your synchronized method, you When you return from your synchronized method, you

leave the object’s key behind on the doorstep.leave the object’s key behind on the doorstep. Other threads can retrieve the key and enter one of the Other threads can retrieve the key and enter one of the

object’s synchronized methodsobject’s synchronized methods

Page 62: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6262

Synchronization DiagramSynchronization Diagram

Page 63: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6363

Synchronized Methods & ObjectsSynchronized Methods & Objects• Other threads can call and execute unsynchronized Other threads can call and execute unsynchronized

(normal) methods of that object(normal) methods of that objectonlyonly the the synchronizedsynchronized methods are blocked methods are blocked

• If a thread owns the lock on an object, it can call If a thread owns the lock on an object, it can call another synchronized method of the same objectanother synchronized method of the same object The thread releases the lock only after exiting the last The thread releases the lock only after exiting the last

synchronized methodsynchronized method

• A thread can own the lock on more than one object A thread can own the lock on more than one object at a timeat a time call a synchronized method of one object from inside a call a synchronized method of one object from inside a

synchronized method of another objectsynchronized method of another object

Page 64: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6464

Waiting on our Bank AccountWaiting on our Bank Account

• What should the thread do when there are not What should the thread do when there are not enough funds?enough funds? The thread could wait until another thread has added the The thread could wait until another thread has added the

necessary fundsnecessary funds What’s the problem with this approach?What’s the problem with this approach?

public synchronized void transfer(int from, int to, int amt) {

while (accounts[from] < amount){ wait }

// then, transfer funds}

Page 65: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6565

The wait() MethodThe wait() Method

• The transfer() method is synchronized!The transfer() method is synchronized!• No other thread can run since the current No other thread can run since the current

thread is in the synchronized method and thread is in the synchronized method and waitingwaiting

• There is a feature of synchronized There is a feature of synchronized methods which takes care of this situationmethods which takes care of this situationA thread can call the object’s A thread can call the object’s waitwait() method to () method to

wait inside of a synchronized methodwait inside of a synchronized methodThe current (calling) thread blocksThe current (calling) thread blocksThe object’s lock is releasedThe object’s lock is released

Page 66: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6666

wait() and the Wait Listwait() and the Wait List

• After a thread calls wait(), it is added to a After a thread calls wait(), it is added to a wait listwait list List of threads waiting on a certain objectList of threads waiting on a certain object

• The thread scheduler ignores threads on the wait listThe thread scheduler ignores threads on the wait list Waiting thread does not continue runningWaiting thread does not continue running

• To remove the thread from the wait list, some other To remove the thread from the wait list, some other thread must call the object’s notify() or notifyAll() thread must call the object’s notify() or notifyAll() methodmethod

• notifynotify() removes one arbitrary thread from the () removes one arbitrary thread from the waiting listwaiting list

• notifyAllnotifyAll() removes all waiting threads() removes all waiting threads

Page 67: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6767

The wait()/notify() MechanismThe wait()/notify() Mechanism

• After a thread has been removed from the wait After a thread has been removed from the wait list, the scheduler will eventually run it againlist, the scheduler will eventually run it again The thread will attempt to lock the object because it The thread will attempt to lock the object because it

gave up the lock when it called gave up the lock when it called waitwait()()

• After the thread gets the lock, it reenters the After the thread gets the lock, it reenters the object and continues where it left offobject and continues where it left off after the call to wait()after the call to wait() known as known as reentryreentry

Page 68: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6868

The wait()/notify() MechanismThe wait()/notify() Mechanism

• Other threads Other threads mustmust call notify() or notifyAll() call notify() or notifyAll() Otherwise, get Otherwise, get deadlockdeadlock

• When a thread calls wait(), there is no way of When a thread calls wait(), there is no way of unblocking it unless another thread calls notify() unblocking it unless another thread calls notify()

• The waiting threads are not automatically The waiting threads are not automatically reactivated when no other thread has the object reactivated when no other thread has the object lockedlocked

• If all threads but one are blocked and the If all threads but one are blocked and the unblocked thread calls wait(), the program will unblocked thread calls wait(), the program will wait forever because there is no thread left to call wait forever because there is no thread left to call notify()notify()

Page 69: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 6969

notify() vs. notifyAll()notify() vs. notifyAll()

• It is somewhat confusing as to when a program It is somewhat confusing as to when a program should call notify()should call notify() no control about which thread will become unblockedno control about which thread will become unblocked

• Generally, call notifyAll() whenever the state of the Generally, call notifyAll() whenever the state of the object changes in such a way that might be object changes in such a way that might be advantageous to other threadsadvantageous to other threads Will need to handle that the condition that caused to Will need to handle that the condition that caused to

block still isn’t metblock still isn’t met

• If the account balance of an account changes, If the account balance of an account changes, multiple threads may be blocked based on this multiple threads may be blocked based on this event occurringevent occurring wake them all upwake them all up

Page 70: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7070

The wait()/notify() MechanismThe wait()/notify() Mechanism

• Calling notifyAll() does not immediately Calling notifyAll() does not immediately activate all of the waiting threadsactivate all of the waiting threadsIt unblocks the waiting threads and removes It unblocks the waiting threads and removes

them from the wait listthem from the wait listThe threads still must compete for entry into the The threads still must compete for entry into the

object after the current thread has exited its object after the current thread has exited its synchronized methodsynchronized method

Page 71: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7171

Wait/Notify Mechanism DiagramWait/Notify Mechanism Diagram

Page 72: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7272

Writing Multithreaded CodeWriting Multithreaded Code

• If two or more threads modify an object, If two or more threads modify an object, declare the methods that carry out these declare the methods that carry out these modifications as modifications as synchronizedsynchronized Basically, putting a lock on the codeBasically, putting a lock on the code

• If a thread must wait for the state of an object If a thread must wait for the state of an object to change, it should wait to change, it should wait insideinside the object by the object by entering the appropriate synchronized entering the appropriate synchronized method and calling wait().method and calling wait().

Page 73: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7373

Writing Multithreaded CodeWriting Multithreaded Code

• Whenever a method changes the state of an Whenever a method changes the state of an object, it should call notifyAll()object, it should call notifyAll()Gives waiting threads a chance to see if the Gives waiting threads a chance to see if the

situation that caused them to wait has changed, situation that caused them to wait has changed, allowing them to continueallowing them to continue

• wait() and notify()/notifyAll() are methods of wait() and notify()/notifyAll() are methods of the the objectobject, not the thread, not the thread

• All calls to wait() are matched up with a All calls to wait() are matched up with a notification call on the same objectnotification call on the same object

Page 74: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7474

DeadlocksDeadlocks

• DeadlockDeadlock: when threads are stuck, waiting : when threads are stuck, waiting for another to do something firstfor another to do something first A deadlock could involve a ring of threads, A deadlock could involve a ring of threads,

waiting for each other or simply one thread waiting for each other or simply one thread waiting for itselfwaiting for itself

Page 75: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7575

Example of DeadlockingExample of Deadlocking

• Suppose the bank has two accounts, account #1 Suppose the bank has two accounts, account #1 has $5000 in it and account #2 has $3750 in it. has $5000 in it and account #2 has $3750 in it.

• Thread #1 wants to transfer $4000 from account Thread #1 wants to transfer $4000 from account #2 to account #1.#2 to account #1.

• Thread #2 wants to transfer $6000 from account Thread #2 wants to transfer $6000 from account #1 to account #2.#1 to account #2.

• These two threads are deadlocked, as neither can These two threads are deadlocked, as neither can run until the other one has finished.run until the other one has finished.

Page 76: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7676

Avoiding DeadlockAvoiding Deadlock

• There is no support in Java for avoiding or There is no support in Java for avoiding or preventing deadlock situationspreventing deadlock situations

• Programmer must be very careful not to write Programmer must be very careful not to write programs in which deadlock can occurprograms in which deadlock can occur Careful synchronization: keep it simpleCareful synchronization: keep it simple

Page 77: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7777

StarvationStarvation

• One thread never gets time on the CPUOne thread never gets time on the CPU

• To avoid starvationTo avoid starvation May need higher-priority threads to call sleep or May need higher-priority threads to call sleep or

yield periodically yield periodically

Page 78: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7878

Concurrency in Java 5+Concurrency in Java 5+

public class Counter {

private long count;

public long getCount() { return count; }

public void incrementCount() { count++; }

}

Counter c = new Counter();

Thread 1

c.incrementCount()

c.getCount()

Thread 2

c.incrementCount()

c.getCount()

Page 79: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 7979

Concurrency in Java 5+Concurrency in Java 5+

public class Counter {

private volatile long count;

public long getCount() { return count; }

public void incrementCount() { count++; }

}

Counter c = new Counter();

Thread 1

c.incrementCount()

c.getCount()

Thread 2

c.incrementCount()

c.getCount()

Page 80: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 8080

Concurrency in Java 5+Concurrency in Java 5+

public class Counter {

private long count;

public long synchronized getCount() { return count; }

public void synchronized incrementCount() { count++; }

}

Counter c = new Counter();

Thread 1

c.incrementCount()

c.getCount()

Thread 2

c.incrementCount()

c.getCount()

Page 81: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 8181

Concurrency in Java 5+Concurrency in Java 5+

public class Counter {

private AtomicLong count;

public long getCount() { return count.get(); }

public void incrementCount() { count.incrementAndGet(); }

}

Counter c = new Counter();

Thread 1

c.incrementCount()

c.getCount()

Thread 2

c.incrementCount()

c.getCount()

Page 82: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 8282

How does this work?How does this work?

• Based on Compare-And-Set semanticsBased on Compare-And-Set semantics Atomically sets the value to the given updated Atomically sets the value to the given updated

value if the current value equals the expected value if the current value equals the expected value.value.

mem[0F0F] = 5

mem[0F0F] = 6

compareAndSet(mem[0F0F],5,6)

Page 83: Concurrent Programming, Threads

July 8, 2008July 8, 2008 James Atlas - CISC370James Atlas - CISC370 8383

Other Java Classes for Other Java Classes for ConcurrencyConcurrency• Package Package java.util.concurrent.locksjava.util.concurrent.locks

LockLock ConditionCondition Match more traditional OS synchronization Match more traditional OS synchronization

programmingprogramming

• Synchronized data structure classes in Synchronized data structure classes in java.util.concurrentjava.util.concurrent Hide synchronization detailsHide synchronization details

• java.util.concurrent.atomicjava.util.concurrent.atomic Allow thread-safe updates of objectsAllow thread-safe updates of objects