Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading."— Presentation transcript:

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

2 Homework Status 1 Fri, 9/8 9/15, 9/18 2Fri, 9/15 Fri, 9/22 3Fri, 9/22 Fri, 9/29 4Fri, 10/6Fri, 10/13 5Fri, 10/13Fri, 10/20 6Fri, 10/20 Fri, 10/27 7Fri, 10/27 Fri, 11/3 HW# Assigned Due Graded Submitted Pending

3 Thread Demo “Nervous Text”

4 Some Definitions ä Multiprocessing - The simultaneous processing of two or more portions of the same program on two or more processing units ä Multiprogramming - The simultaneous processing of multiple programs (OS processes) on one or more processing units ä Multitasking operating system - an OS that supports multiprogramming ä Multithreading ???

5 Multithreading ä The “simultaneous” * processing of two or more portions (threads) of the same program (OS process) ä Might take advantage of multiple CPUs when present ä A thread can be thought of as a light-weight process that you control (rather than the OS) ä Requires less overhead than full-fledged processes ä Makes sharing resources (e.g., memory) easier * Logical but not necessarily truly physical concurrency

6 Multithreading vs. Multiprogramming Program 1... Program n CPU 1 CPU m... Program 1... Program n Thread 1A Thread 1B Thread nA Thread nB Thread nC CPU 1 CPU m... Multiprogramming Multithreading Source: Java 2 Certification

7 Why Use Threads? ä Can provide real or apparent speed up (e.g., start editing a large file before it is completely loaded in memory) ä Can support arbitrary service requests easily ä Can support shared use of a common resource ä Is (relatively) easy to do in Java in a (mostly) portable way ä General rule of thumb - if a piece of code takes a long time to run and is somewhat independent of the rest of the program, put it in a thread

8 Thread States ä Born - created, needs to be started ä Ready (runnable) - eligible to be run ä Running - actually executing ä Waiting - asleep, blocked (for I/O), etc. ä Dead - processing completed, “that’s all she wrote” Ready Waiting RunningDeadBorn This is a very simple example of a state diagram. State transition diagrams with transition conditions labeling the arrows are helpful aids to designing good multithreaded code. This is a very simple example of a state diagram. State transition diagrams with transition conditions labeling the arrows are helpful aids to designing good multithreaded code.

9 Life Cycle of a Java Thread (more detail) Source: Deitel & Deitel

10 Thread Scheduling ä The JVM has to select which thread(s) to run (move from ready to running states) at what time ä Two kinds of scheduling: ä Preemptive (UNIX) ä Time slicing (Win32, Macintosh)

11 Preemptive Scheduling ä The highest priority thread runs until ä It dies ä It “puts itself to sleep” ä It is preempted by a higher priority thread ä A higher-priority thread comes into existence ä A previous thread’s priority changes ä Preemptive scheduling is predictable, but can result in low-priority threads never running

12 Time Slicing ä Threads execute for a specific slice of time ä Scheduler then decides whether to keep thread running or “give the CPU to” another thread ä Less predictable than preemptive scheduling, but can handle “selfish” threads

13 Synchronization ä The use of threads presents a need for coordinating their activities ä Synchronization is temporal coordination of simultaneous activities ä Synchronization allows communication about things/events of mutual interest across threads ä Synchronization is needed to support mutual exclusion

14 Mutual Exclusion ä Multiple threads present a potential pitfall - simultaneous access to shared resources ä Thread A want to read the value of a variable, think about it for while, and then change it ä After Thread A reads the value, but before it writes the new value, Thread B changes its value ä Thread B’s change gets lost

15 Semaphores ä Semaphores are primitives that support mutual exclusion ä Allow multiple programs to read/write shared information ä Identify critical regions of code that need exclusive access to a shared resource ä Usage: ä Process A wants shared resource, tells that resource’s semaphore to wait ä System suspends Process A until resource is available ä System then wakes up Process A and gives it exclusive use of the resource ä When Process A is finished with the resource, tells the semaphore to proceed (releases the resource)

16 Semaphores (continued) ä Semaphore mechanism uses: ä A sequencer S: integer variable ä Atomic Wait operation P: ä P(S): Wait until S>0 and then S-- ä Atomic Signal operation V: ä V(S): S++ ä Allows a process to block itself to wait for an event and then be awakened by another process when the event occurs ä To control access to a single resource, use binary semaphore Source: Structured Concurrent Programming with Operating Systems Applications

17 The Monitor Concept Source: Operating Systems: Advanced Concepts Object-oriented: local data + “methods” Object-oriented: local data + “methods” Scheduler controls order of resource allocation Scheduler controls order of resource allocation Condition variables determine can cause process to block Condition variables determine can cause process to block Guard allows only 1 process to execute in monitor at a time Guard allows only 1 process to execute in monitor at a time Monitor supports mutual exclusion for each procedure Monitor supports mutual exclusion for each procedure

18 Java’s Approach to Threads ä Java was designed with threads in mind ä class Thread ä class Object ä synchronized statements and methods ä uses Monitor concept from Operating Systems ä class ThreadGroup

19 java.lang.Thread ä Implements the java.lang.Runnable interface ä Basic methods: ä run() - the thread’s execution entry point (invoked by the JVM thread scheduler) ä start() - put the (new) thread in the ready state, then returns immediately to the launching thread ä isAlive() - the thread has been started and is not dead ä getName() - returns the thread’s name (has a JVM- assigned default or can be specified ) ä toString() - like getName(), but includes the priority and ThreadGroup name

20 java.lang.Thread (concluded) ä Basic methods (concluded): ä setPriority() - sets the thread’s (integer, 1 to 10) priority ä currentThread() - returns the current Thread (static) ä sleep() - puts the (currentThread) thread to sleep for a specified time so that other (e.g., lower-priority) threads can execute (static) ä interrupt() - wakes up a thread ä join() - the thread that invokes this on another thread waits for the other thread to die before proceeding

21 Thread Specification (1 of 3) 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;

22 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 start() public void start() throws IllegalThreadStateException; throws IllegalThreadStateException; 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; public final boolean isAlive(); public final boolean isAlive(); public int countStackFrames();

23 Thread Specification (3 of 3) 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 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(); }

24 Creating Threads ä Option #1 ä Define a subclass of class Thread ä Override the run() method ä Create an instance of the subclass ä Invoke the instance’s start() method ä Option #2 ä Define a class that implements the Runnable interface ä Implement the run() method ä Create an instance of the class ä Pass the instance to the Thread class’ constructor ä Invoke the thread’s start() method

25 Example of Creating a Thread: Option #1 public class MyThread1 extends Thread { 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 #1 Define a subclass of class Thread Override the run() method Create an instance of the subclass Invoke the instance’s start() method

26 Example of Creating a Thread: Option #2 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 #2 Define a class that implements the Runnable interface Implement the run() method Create an instance of the class Pass the instance to the Thread class’ constructor Invoke the thread’s start() method

27 Daemon Threads ä A utility thread for other threads (e.g., the garbage collector) ä Run in the background ä Their (live) existence does not prevent a program from terminating ä When the last non-daemon thread dies, the program exits ä Daemon status must be set before the thread starts

28 Running a Thread ä JVM starts up, invokes some class’ main method ä That class is started in a single, non-daemon thread ä Eventually, a new thread gets created and its start() method is invoked ä Makes the thread ready ä When selected by the JVM scheduler, the new thread’s run method is invoked ä Continues running until run() returns, or aborts ä Dead threads are eventually cleaned up ä JVM continues until all non-daemon threads have stopped (died)

29 Making a Thread Wait ä The currentThread can be put into a waiting state: ä By invoking Thread.sleep() ä By performing an input/output request ä The thread becomes ready when the I/O is finished ä By invoking an Object’s wait() method ä By the scheduler when a higher priority thread is selected to execute ä E.g., the higher priority thread’s sleep period ends, its I/O finishes, or it has been notified out of a wait invocation

30 Interrupts ä A waiting Thread can be interrupted by explicitly invoking its interrupt() method ä The interrupted thread moves from waiting to ready state ä When selected for running, the interrupted thread “jumps to” its InterruptedException handler

31 Example of Handling an Interrupt public class MyThread extends Thread {... void foo() { try { try { // do something sleep(10000); } catch (InterruptException x) { System.out.println(“Interrupted”);}}}

32 Thread Synchronization ä An object with a synchronized method is a monitor ä e.g., public synchronized void foo() { // do stuff } ä An object has a single lock ä The thread executing a synchronized method has that object’s lock ä Only one synchronized method in a class can be active on an object at a time ä All other threads that want to invoke any synchronized method on that object must wait (there is only one lock for the entire object) ä When the synchronized method returns, the lock on that object is released

33 Thread Synchronization (concluded) ä While executing a synchronized method, a thread might decide that it can’t proceed further and voluntarily give up control by invoking the object’s wait() method ä When finished executing a synchronized method, a thread can decide to invoke the object’s notify or notifyAll method() to let other threads know that its done ä A thread that is waiting to get a lock on an object must either be notified or interrupted (or it will wait forever) ä Only call wait(), notify(), or notifyAll() if you have a lock on the object (e.g., are in a synchronized method)

34 synchronized Static Methods ä You can get a lock on a class (actually, the class’s Class object) by creating a synchronized static method ä Only one thread can execute a (any) synchronized static method at a time

35 Synchronized Blocks ä Arbitrary code blocks (marked by braces { }) can be synchronized ä Syntax: ä 1) synchronized (objectReference) { statements } ä 2) synchronized (className) { statements } ä The currentThread must have a lock on the object (or class) before it can execute the statements ä Can be used with any object/class, not just the one for the object/class of the method you’re in

36 class Object and Threads ä Class Object has three thread-related methods: ä wait() - puts the running thread that invokes this method into a waiting state ä notify() - moves some other thread that is waiting on that object into the ready state ä notifyAll() - moves all other threads that are waiting on that object into the ready state ä Every object has a wait set which is a list of Threads that are waiting to get a lock on the object

37 wait() ä May specify time period (or indefinite wait if 0 or none specified) ä Must have a lock on the object ä Causes lock to be relinquished ä Puts the invoking thread into wait state until: ä Some other thread invokes notify (and this thread happens to get picked) or notifyAll ä Some other thread interrupts it ä The specified time period has elapsed ä It then goes into the ready (runnable) state

38 notify() ä Must have a lock on the object ä The invoking thread states its willingness to relinquish its lock on the object ä Moves an arbitrary thread (at the discretion of the implementation) that is waiting on that lock from the waiting to the ready state ä The selected thread has no special priority in getting the lock

39 notifyAll() ä Must have a lock on the object ä The invoking thread states its willingness to relinquish its lock on the object ä Moves all threads that are waiting on that lock from the waiting to the ready state ä The threads have no special priority in getting the lock

40 java.lang.ThreadGroup ä Every Thread belongs to exactly one ThreadGroup ä There’s a default “system” ThreadGroup ä Can interrupt all Threads in a ThreadGroup ä Can notifyAll Threads in a ThreadGroup ä Can get/set the maxPriority of the Threads in a ThreadGroup ä Can be a daemon ThreadGroup (Threads created by referencing a daemon ThreadGrop in their constructor become daemon Threads) ä ThreadGroups can be hierarchical (can have one parent) ä An invocation on the parent ThreadGroup is also called on all of its children

41 An Aside - the Class class ä Objects of this class, called class descriptors, are automatically created by the JVM when a class (or interface) is loaded ä Accessed by an Object’s getClass method ä Can get the class’s name, superclass, interfaces ä Can create instances of the class (in lieu of using the class’ constructors) ä Can be locked for thread synchronization purposes


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

Similar presentations


Ads by Google