41
UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Lecture 18 Advanced Java Concepts Advanced Java Concepts Threads and Multithreading Threads and Multithreading [ [ Java 2: The Complete Reference: Java 2: The Complete Reference: Chapter 11] Chapter 11] [ [ Deitel Deitel : Chapter 15] : Chapter 15] Fri Fri . 10/27 – Mon. 10/30 . 10/27 – Mon. 10/30

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Embed Size (px)

Citation preview

Page 1: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

UMass Lowell Computer Science 91.460

Java and Distributed Computing

Prof. Karen Daniels Fall, 2000

UMass Lowell Computer Science 91.460

Java and Distributed Computing

Prof. Karen Daniels Fall, 2000

Lecture 18Lecture 18Advanced Java ConceptsAdvanced Java Concepts

Threads and MultithreadingThreads and Multithreading[[Java 2: The Complete Reference: Java 2: The Complete Reference: Chapter 11]Chapter 11]

[[DeitelDeitel: Chapter 15]: Chapter 15]

FriFri. 10/27 – Mon. 10/30. 10/27 – Mon. 10/30

Page 2: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Homework StatusHomework Status

11 Fri, 9/8 9/15, 9/18 Fri, 9/8 9/15, 9/18 22 Fri, 9/15 Fri, 9/22 Fri, 9/15 Fri, 9/22 33 Fri, 9/22Fri, 9/22 Fri, 9/29 Fri, 9/29 44 Fri, 10/6Fri, 10/6 Fri, 10/13Fri, 10/13 55 Fri, 10/13Fri, 10/13 Fri, 10/20Fri, 10/2066 Fri, 10/20 Fri, 10/27Fri, 10/20 Fri, 10/27 77 Fri, 10/27 Fri, 11/3Fri, 10/27 Fri, 11/3

HW#HW# AssignedAssigned DueDue

GradedGraded

SubmittedSubmitted

PendingPending

Page 3: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Thread DemoThread Demo

““Nervous Text”Nervous Text”

Page 4: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Some DefinitionsSome Definitions

Multiprocessing - The simultaneous processing of Multiprocessing - The simultaneous processing of two or more portions of the same program on two two or more portions of the same program on two or more processing unitsor more processing units

Multiprogramming - The simultaneous processing Multiprogramming - The simultaneous processing of multiple programs (OS processes) on one or of multiple programs (OS processes) on one or more processing unitsmore processing units

Multitasking operating system - an OS that Multitasking operating system - an OS that supports multiprogrammingsupports multiprogramming

Multithreading ???Multithreading ???

Page 5: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

MultithreadingMultithreading

The “simultaneous” * processing of two or more The “simultaneous” * processing of two or more portions (threads) of the same program (OS portions (threads) of the same program (OS process)process) Might take advantage of multiple CPUs when presentMight take advantage of multiple CPUs when present

A thread can be thought of as a light-weight A thread can be thought of as a light-weight process that you control (rather than the OS)process that you control (rather than the OS) Requires less overhead than full-fledged processesRequires less overhead than full-fledged processes Makes sharing resources (e.g., memory) easierMakes sharing resources (e.g., memory) easier

* Logical but not necessarily truly physical concurrency* Logical but not necessarily truly physical concurrency

Page 6: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Multithreading vs. MultiprogrammingMultithreading vs. Multiprogramming

Program 1Program 1

......

Program nProgram n

CPU 1CPU 1

CPU mCPU m

......

Program 1Program 1

......

Program nProgram n

Thread 1A Thread 1A Thread 1BThread 1B

Thread nA Thread nA Thread nB Thread nB Thread nC Thread nC

CPU 1CPU 1

CPU mCPU m

......

MultiprogrammingMultiprogramming

MultithreadingMultithreading

SourceSource: : Java 2 CertificationJava 2 Certification

Page 7: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Why Use Threads?Why Use Threads?

Can provide real or apparent speed up (e.g., start Can provide real or apparent speed up (e.g., start editing a large file before it is completely loaded editing a large file before it is completely loaded in memory)in memory)

Can support arbitrary service requests easilyCan support arbitrary service requests easily Can support shared use of a common resourceCan support shared use of a common resource Is (relatively) easy to do in Java in a (mostly) Is (relatively) easy to do in Java in a (mostly)

portable wayportable way General rule of thumb - if a piece of code takes a General rule of thumb - if a piece of code takes a

long time to run and is somewhat independent of long time to run and is somewhat independent of the rest of the program, put it in a threadthe rest of the program, put it in a thread

Page 8: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Thread StatesThread States

Born - created, needs to be startedBorn - created, needs to be started Ready (runnable) - eligible to be runReady (runnable) - eligible to be run Running - actually executingRunning - actually executing Waiting - asleep, blocked (for I/O), etc.Waiting - asleep, blocked (for I/O), etc. Dead - processing completed, “that’s all she wrote”Dead - processing completed, “that’s all she wrote”

ReadyReady

WaitingWaiting

RunningRunning DeadDeadBornBorn

This is a very simple example of a state diagram. State This is a very simple example of a state diagram. State transition diagrams with transition conditions labeling the transition diagrams with transition conditions labeling the arrows are helpful aids to designing good multithreaded code.arrows are helpful aids to designing good multithreaded code.

Page 9: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Life Cycle of a Java Thread(more detail)

Life Cycle of a Java Thread(more detail)

SourceSource:: Deitel & Deitel Deitel & Deitel

Page 10: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Thread SchedulingThread Scheduling

The JVM has to select which thread(s) to The JVM has to select which thread(s) to run (move from ready to running states) at run (move from ready to running states) at what timewhat time

Two kinds of scheduling:Two kinds of scheduling: Preemptive (UNIX)Preemptive (UNIX) Time slicing (Win32, Macintosh)Time slicing (Win32, Macintosh)

Page 11: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Preemptive SchedulingPreemptive Scheduling

The highest priority thread runs until The highest priority thread runs until It diesIt dies It “puts itself to sleep”It “puts itself to sleep” It is preempted by a higher priority threadIt is preempted by a higher priority thread

A higher-priority thread comes into existenceA higher-priority thread comes into existence A previous thread’s priority changesA previous thread’s priority changes

Preemptive scheduling is predictable, but can Preemptive scheduling is predictable, but can result in low-priority threads never runningresult in low-priority threads never running

Page 12: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Time SlicingTime Slicing

Threads execute for a specific slice of timeThreads execute for a specific slice of time Scheduler then decides whether to keep Scheduler then decides whether to keep

thread running or “give the CPU to” another thread running or “give the CPU to” another threadthread

Less predictable than preemptive Less predictable than preemptive scheduling, but can handle “selfish” threadsscheduling, but can handle “selfish” threads

Page 13: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

SynchronizationSynchronization

The use of threads presents a need for The use of threads presents a need for coordinating their activitiescoordinating their activities

Synchronization is temporal coordination of Synchronization is temporal coordination of simultaneous activitiessimultaneous activities

Synchronization allows communication about Synchronization allows communication about things/events of mutual interest across threadsthings/events of mutual interest across threads

Synchronization is needed to support mutual Synchronization is needed to support mutual exclusionexclusion

Page 14: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Mutual ExclusionMutual Exclusion

Multiple threads present a potential pitfall - Multiple threads present a potential pitfall - simultaneous access to shared resourcessimultaneous access to shared resources Thread A want to read the value of a variable, Thread A want to read the value of a variable,

think about it for while, and then change itthink about it for while, and then change it After Thread A reads the value, but before it After Thread A reads the value, but before it

writes the new value, Thread B changes its writes the new value, Thread B changes its valuevalue

Thread B’s change gets lostThread B’s change gets lost

Page 15: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

SemaphoresSemaphores

Semaphores are primitives that support mutual exclusionSemaphores are primitives that support mutual exclusion Allow multiple programs to read/write shared informationAllow multiple programs to read/write shared information Identify critical regions of code that need exclusive access Identify critical regions of code that need exclusive access

to a shared resourceto a shared resource Usage:Usage:

Process Process AA wants shared resource, tells that resource’s wants shared resource, tells that resource’s semaphore to waitsemaphore to wait

System suspends Process System suspends Process AA until resource is available until resource is available System then wakes up Process System then wakes up Process AA and gives it exclusive use of and gives it exclusive use of

the resourcethe resource When Process When Process AA is finished with the resource, tells the is finished with the resource, tells the

semaphore to proceed (releases the resource)semaphore to proceed (releases the resource)

Page 16: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Semaphores (continued)Semaphores (continued)

Semaphore mechanism uses:Semaphore mechanism uses: A sequencer S: integer variableA sequencer S: integer variable Atomic Wait operation P:Atomic Wait operation P:

P(S): Wait until S>0 and then S--P(S): Wait until S>0 and then S-- Atomic Signal operation V:Atomic Signal operation V:

V(S): S++V(S): S++ Allows a process to block itself to wait for an event and then Allows a process to block itself to wait for an event and then

be awakened by another process when the event occursbe awakened by another process when the event occurs To control access to a single resource, use binary semaphoreTo control access to a single resource, use binary semaphore

SourceSource: : Structured Concurrent Programming with Structured Concurrent Programming with Operating Systems ApplicationsOperating Systems Applications

Page 17: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

The Monitor ConceptThe Monitor Concept

SourceSource:: Operating Systems: Operating Systems: Advanced ConceptsAdvanced Concepts

• Object-oriented: local data + Object-oriented: local data + “methods”“methods”• Scheduler controls order of Scheduler controls order of resource allocationresource allocation• Condition variables Condition variables determine can cause process to determine can cause process to blockblock• Guard allows only 1 process Guard allows only 1 process to execute in monitor at a timeto execute in monitor at a time• Monitor supports mutual Monitor supports mutual exclusion for each procedureexclusion for each procedure

Page 18: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Java’s Approach to ThreadsJava’s Approach to Threads

Java was designed with threads in mindJava was designed with threads in mind class class ThreadThread class class ObjectObject synchronizedsynchronized statements and methods statements and methods

uses uses MonitorMonitor concept from Operating Systems concept from Operating Systems

classclass ThreadGroup ThreadGroup

Page 19: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

java.lang.Threadjava.lang.Thread

Implements the java.lang.Runnable interfaceImplements the java.lang.Runnable interface Basic methods:Basic methods:

runrun() - the thread’s execution entry point (invoked by () - the thread’s execution entry point (invoked by the JVM thread scheduler)the JVM thread scheduler)

startstart() - put the (new) thread in the ready state, then () - put the (new) thread in the ready state, then returns immediately to the launching threadreturns immediately to the launching thread

isAliveisAlive() - the thread has been started and is not dead() - the thread has been started and is not dead getNamegetName() - returns the thread’s name (has a JVM-() - returns the thread’s name (has a JVM-

assigned default or can be specified )assigned default or can be specified ) toStringtoString() - like () - like getNamegetName(), but includes the priority (), but includes the priority

and ThreadGroup nameand ThreadGroup name

Page 20: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

java.lang.Thread (concluded)java.lang.Thread (concluded)

Basic methods (concluded):Basic methods (concluded): setPrioritysetPriority() - sets the thread’s (integer, 1 to 10) () - sets the thread’s (integer, 1 to 10)

prioritypriority currentThreadcurrentThread() - returns the current Thread (static)() - returns the current Thread (static) sleepsleep() - puts the (currentThread) thread to sleep for a () - puts the (currentThread) thread to sleep for a

specified time so that other (e.g., lower-priority) specified time so that other (e.g., lower-priority) threads can execute (static)threads can execute (static)

interruptinterrupt() - wakes up a thread() - wakes up a thread joinjoin() - the thread that invokes this on another thread () - the thread that invokes this on another thread

waits for the other thread to die before proceedingwaits for the other thread to die before proceeding

Page 21: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Thread Specification (1 of 3)Thread Specification (1 of 3)

public class Thread implements Runnable {public class Thread implements Runnable { public final static int MIN_PRIORITY = 1;public final static int MIN_PRIORITY = 1; public final static int MAX_PRIORITY = 10;public final static int MAX_PRIORITY = 10; public final static int NORM_PRIORITY = 5;public final static int NORM_PRIORITY = 5; public Thread();public Thread(); public Thread(String name);public Thread(String name); public Thread(Runnable runObject);public Thread(Runnable runObject); public Thread(Runnable runObject, String name);public Thread(Runnable runObject, String name); public Thread(ThreadGroup group, String name)public Thread(ThreadGroup group, String name) throws SecurityException, IllegalThreadStateException;throws SecurityException, IllegalThreadStateException; public Thread(ThreadGroup group, Runnable runObject)public Thread(ThreadGroup group, Runnable runObject) throws SecurityException, IllegalThreadStateException;throws SecurityException, IllegalThreadStateException; public Thread(ThreadGroup group, Runnable runObject, String name)public Thread(ThreadGroup group, Runnable runObject, String name) throws SecurityException, IllegalThreadStateException;throws SecurityException, IllegalThreadStateException;

Page 22: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Thread Specification (2 of 3)Thread Specification (2 of 3)

public String toString();public String toString(); public void checkAccess() throws SecurityException;public void checkAccess() throws SecurityException;

public void run();public void run(); public void start()public void start() throws IllegalThreadStateException;throws IllegalThreadStateException;

public final String getName();public final String getName(); public final void setName(String name)public final void setName(String name) throws SecurityException;throws SecurityException; public final ThreadGroup getThreadGroup();public final ThreadGroup getThreadGroup(); public final int getPriority();public final int getPriority(); public final void setPriority(int newPriority)public final void setPriority(int newPriority) throws SecurityException, IllegalArgumentException;throws SecurityException, IllegalArgumentException; public final boolean isDaemon();public final boolean isDaemon(); public final void setDaemon(boolean on) public final void setDaemon(boolean on)

throws SecurityException, IllegalThreadStateException;throws SecurityException, IllegalThreadStateException;

public final boolean isAlive();public final boolean isAlive();public int countStackFrames();public int countStackFrames();

Page 23: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Thread Specification (3 of 3)Thread Specification (3 of 3)

public final void join()public final void join() throws InterruptedException;throws InterruptedException; public final void join(long millis)public final void join(long millis) throws InterruptedException;throws InterruptedException; public final void join(long millis, int nanos)public final void join(long millis, int nanos) throws InterruptedException;throws InterruptedException; public void interrupt();public void interrupt(); public boolean isInterrupted();public boolean isInterrupted(); public static boolean interrupted();public static boolean interrupted(); public static Thread currentThread();public static Thread currentThread();

public static void dumpStack();public static void dumpStack(); public static void yield();public static void yield(); public static void sleep(long millis)public static void sleep(long millis) throws InterruptedException;throws InterruptedException; public static void sleep(long millis, int nanos)public static void sleep(long millis, int nanos) throws InterruptedException;throws InterruptedException; public void destroy(); }public void destroy(); }

Page 24: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Creating ThreadsCreating Threads

Option #1Option #1 Define a subclass of class Define a subclass of class ThreadThread Override the Override the runrun() method() method Create an instance of the subclassCreate an instance of the subclass Invoke the instance’s Invoke the instance’s startstart() method() method

Option #2Option #2 Define a class that implements the Define a class that implements the RunnableRunnable interface interface Implement the Implement the runrun() method() method Create an instance of the classCreate an instance of the class Pass the instance to the Thread class’ constructorPass the instance to the Thread class’ constructor Invoke the thread’s Invoke the thread’s startstart() method() method

Page 25: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Example of Creating a Thread:Option #1Example of Creating a Thread:Option #1

public class MyThread1 extends Thread {public class MyThread1 extends Thread {

public void run() { // do something useful }public void run() { // do something useful }

public static void main(String args[]) {public static void main(String args[]) {

MyThread1 myThread = new MyThread1( );MyThread1 myThread = new MyThread1( );

myThread.start();myThread.start();

} }

}} Option #1Define a subclass of class ThreadOverride the run() methodCreate an instance of the subclassInvoke the instance’s start() method

Page 26: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Example of Creating a Thread: Option #2Example of Creating a Thread: Option #2

public class MyThread2 implements Runnable {public class MyThread2 implements Runnable {

public void run( ) { // do something useful }public void run( ) { // do something useful }

public static void main(String args[]) {public static void main(String args[]) {

Thread myThread = new Thread(new MyThread2( ));Thread myThread = new Thread(new MyThread2( ));

myThread.start( );myThread.start( );

}}

}} Option #2Define a class that implements the Runnable interfaceImplement the run() methodCreate an instance of the classPass the instance to the Thread class’ constructorInvoke the thread’s start() method

Page 27: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Daemon ThreadsDaemon Threads

A utility thread for other threads (e.g., the garbage A utility thread for other threads (e.g., the garbage collector)collector)

Run in the backgroundRun in the background Their (live) existence does not prevent a program Their (live) existence does not prevent a program

from terminatingfrom terminating When the last non-daemon thread dies, the program When the last non-daemon thread dies, the program

exitsexits

Daemon status must be set before the thread startsDaemon status must be set before the thread starts

Page 28: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Running a ThreadRunning a Thread

JVM starts up, invokes some class’ main methodJVM starts up, invokes some class’ main method That class is started in a single, non-daemon threadThat class is started in a single, non-daemon thread

Eventually, a new thread gets created and its Eventually, a new thread gets created and its startstart() () method is invokedmethod is invoked Makes the thread readyMakes the thread ready

When selected by the JVM scheduler, the new thread’s When selected by the JVM scheduler, the new thread’s run method is invokedrun method is invoked Continues running until Continues running until runrun() returns, or aborts() returns, or aborts

Dead threads are eventually cleaned upDead threads are eventually cleaned up JVM continues until all non-daemon threads have JVM continues until all non-daemon threads have

stopped (died)stopped (died)

Page 29: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Making a Thread WaitMaking a Thread Wait

The currentThread can be put into a waiting state:The currentThread can be put into a waiting state: By invoking Thread.By invoking Thread.sleepsleep()() By performing an input/output requestBy performing an input/output request

The thread becomes ready when the I/O is finishedThe thread becomes ready when the I/O is finished

By invoking an Object’s By invoking an Object’s waitwait() method() method By the scheduler when a higher priority thread is By the scheduler when a higher priority thread is

selected to executeselected to execute E.g., the higher priority thread’s sleep period ends, its I/O E.g., the higher priority thread’s sleep period ends, its I/O

finishes, or it has been notified out of a wait invocationfinishes, or it has been notified out of a wait invocation

Page 30: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

InterruptsInterrupts

A waiting Thread can be interrupted by A waiting Thread can be interrupted by explicitly invoking its explicitly invoking its interruptinterrupt() method() method

The interrupted thread moves from waiting The interrupted thread moves from waiting to ready stateto ready state

When selected for running, the interrupted When selected for running, the interrupted thread “jumps to” its InterruptedException thread “jumps to” its InterruptedException handlerhandler

Page 31: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Example of Handling an InterruptExample of Handling an Interrupt

public class MyThread extends Thread {public class MyThread extends Thread {......

void foo() {void foo() {

try {try {// do something// do something

sleep(10000);sleep(10000);

} catch (InterruptException x) {} catch (InterruptException x) {System.out.println(“Interrupted”);System.out.println(“Interrupted”);

}}

}}

}}

Page 32: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Thread SynchronizationThread Synchronization

An object with a An object with a synchronizedsynchronized method is a monitor method is a monitor e.g., public e.g., public synchronizedsynchronized void foo() { // do stuff } void foo() { // do stuff }

An object has a single lockAn object has a single lock The thread executing a The thread executing a synchronizedsynchronized method has that object’s method has that object’s

locklock Only one Only one synchronizedsynchronized method in a class can be active method in a class can be active

on an object at a timeon an object at a time All other threads that want to invoke any All other threads that want to invoke any synchronizedsynchronized method method

on that object must wait (there is only one lock for the entire on that object must wait (there is only one lock for the entire object)object)

When the synchronized method returns, the lock on that When the synchronized method returns, the lock on that object is releasedobject is released

Page 33: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Thread Synchronization (concluded)Thread Synchronization (concluded)

While executing a While executing a synchronizedsynchronized method, a thread might method, a thread might decide that it can’t proceed further and voluntarily give up decide that it can’t proceed further and voluntarily give up control by invoking the object’s control by invoking the object’s waitwait() method() method

When finished executing a When finished executing a synchronizedsynchronized method, a thread method, a thread can decide to invoke the object’s can decide to invoke the object’s notifynotify or or notifyAllnotifyAll method() to let other threads know that its donemethod() to let other threads know that its done

A thread that is waiting to get a lock on an object must A thread that is waiting to get a lock on an object must either be notified or interrupted (or it will wait forever)either be notified or interrupted (or it will wait forever)

Only call Only call waitwait(), (), notifynotify(), or (), or notifyAllnotifyAll() if you have a lock () if you have a lock on the object (e.g., are in a on the object (e.g., are in a synchronizedsynchronized method) method)

Page 34: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

synchronized Static Methodssynchronized Static Methods

You can get a lock on a class (actually, the You can get a lock on a class (actually, the class’s Class object) by creating a class’s Class object) by creating a synchronizedsynchronized staticstatic method method

Only one thread can execute a (any) Only one thread can execute a (any) synchronizedsynchronized staticstatic method at a time method at a time

Page 35: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

Synchronized BlocksSynchronized Blocks

Arbitrary code blocks (marked by braces { }) can Arbitrary code blocks (marked by braces { }) can be be synchronizedsynchronized

Syntax: Syntax: 1)1) synchronized synchronized (objectReference) { statements } (objectReference) { statements } 2)2) synchronized synchronized (className) { statements } (className) { statements }

The currentThread must have a lock on the object The currentThread must have a lock on the object (or class) before it can execute the statements(or class) before it can execute the statements

Can be used with any object/class, not just the one Can be used with any object/class, not just the one for the object/class of the method you’re infor the object/class of the method you’re in

Page 36: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

class Object and Threadsclass Object and Threads

Class Class ObjectObject has three thread-related methods: has three thread-related methods: waitwait() - puts the running thread that invokes this () - puts the running thread that invokes this

method into a waiting statemethod into a waiting state notifynotify() - moves some other thread that is waiting on () - moves some other thread that is waiting on

that object into the ready statethat object into the ready state notifyAllnotifyAll() - moves all other threads that are waiting on () - moves all other threads that are waiting on

that object into the ready statethat object into the ready state

Every object has a Every object has a wait setwait set which is a list of which is a list of Threads that are waiting to get a lock on the objectThreads that are waiting to get a lock on the object

Page 37: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

wait()wait()

May specify time period (or indefinite wait if 0 or May specify time period (or indefinite wait if 0 or none specified)none specified)

Must have a lock on the objectMust have a lock on the object Causes lock to be relinquishedCauses lock to be relinquished Puts the invoking thread into wait state until:Puts the invoking thread into wait state until:

Some other thread invokes notify (and this thread Some other thread invokes notify (and this thread happens to get picked) or notifyAllhappens to get picked) or notifyAll

Some other thread interrupts itSome other thread interrupts it The specified time period has elapsedThe specified time period has elapsed

It then goes into the ready (runnable) stateIt then goes into the ready (runnable) state

Page 38: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

notify()notify()

Must have a lock on the objectMust have a lock on the object The invoking thread states its willingness to The invoking thread states its willingness to

relinquish its lock on the objectrelinquish its lock on the object Moves an arbitrary thread (at the discretion Moves an arbitrary thread (at the discretion

of the implementation) that is waiting on of the implementation) that is waiting on that lock from the waiting to the ready statethat lock from the waiting to the ready state The selected thread has no special priority in The selected thread has no special priority in

getting the lockgetting the lock

Page 39: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

notifyAll()notifyAll()

Must have a lock on the objectMust have a lock on the object The invoking thread states its willingness to The invoking thread states its willingness to

relinquish its lock on the objectrelinquish its lock on the object Moves all threads that are waiting on that Moves all threads that are waiting on that

lock from the waiting to the ready statelock from the waiting to the ready state The threads have no special priority in getting The threads have no special priority in getting

the lockthe lock

Page 40: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

java.lang.ThreadGroup java.lang.ThreadGroup

Every Thread belongs to exactly one ThreadGroupEvery Thread belongs to exactly one ThreadGroup There’s a default “system” ThreadGroupThere’s a default “system” ThreadGroup Can interrupt all Threads in a ThreadGroupCan interrupt all Threads in a ThreadGroup Can notifyAll Threads in a ThreadGroupCan notifyAll Threads in a ThreadGroup Can get/set the maxPriority of the Threads in a ThreadGroupCan get/set the maxPriority of the Threads in a ThreadGroup Can be a daemon ThreadGroup (Threads created by Can be a daemon ThreadGroup (Threads created by

referencing a daemon ThreadGrop in their constructor referencing a daemon ThreadGrop in their constructor become daemon Threads)become daemon Threads)

ThreadGroups can be hierarchical (can have one ThreadGroups can be hierarchical (can have one parent)parent) An invocation on the parent ThreadGroup is also called on An invocation on the parent ThreadGroup is also called on

all of its childrenall of its children

Page 41: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading

An Aside - the Class classAn Aside - the Class class

Objects of this class, called class Objects of this class, called class descriptors, are automatically created by the descriptors, are automatically created by the JVM when a class (or interface) is loadedJVM when a class (or interface) is loaded Accessed by an Object’s getClass methodAccessed by an Object’s getClass method Can get the class’s name, superclass, interfacesCan get the class’s name, superclass, interfaces Can create instances of the class (in lieu of Can create instances of the class (in lieu of

using the class’ constructors)using the class’ constructors) Can be locked for thread synchronization Can be locked for thread synchronization

purposespurposes