Presentation is loading. Please wait.

Presentation is loading. Please wait.

More About Threads.

Similar presentations


Presentation on theme: "More About Threads."— Presentation transcript:

1 More About Threads

2 Contents Interrupting Threads Thread States Thread Properties

3 I. Interrupting Threads

4 The interrupt Method A thread terminates when its run method returns
There is no way to force a thread to terminate. However, the interrupt method can be used to request termination of a thread When the interrupt method is called on a thread, the interrupted status of the thread is set. This is a Boolean flag that is present in every thread. Each thread should occasionally check whether it has been interrupted

5 To find out whether the interrupted status was set: while(. Thread
To find out whether the interrupted status was set: while(!Thread.currentThread().isInterrupted() && more work to do) { do more work } When a thread is blocked, it cannot check the interrupted status !!! This is where the InterruptedException comes in.

6 public void run() { try { … while(. Thread. currentThread()
public void run() { try { … while(!Thread.currentThread().isInterrupted() && more work to do) { do more work } } catch(InterruptedException e) { //Thread was interrupted during //sleep or wait } finally { } If your loop calls sleep method when the interrupted status is set, it doesn't sleep. Instead, it clears the status and throws an InterruptedException !!!

7 public void run() { try { … while(more work to do) { do more work Thread.sleep(delay); } } catch(InterruptedException e) { //Thread was interrupted during sleep } finally { }

8 II. Thread States

9 4 States New Runnable Blocked Dead

10 The New State When you create a thread with the new operator, the thread is in the new state

11 The Runnable State Once you invoke the start method, the thread is runnable A runnable thread may or may not actually be running. It is up to the operating system to give the thread time to run

12 The Blocked State A thread enters the blocked state when one of the following actions occurs: The thread goes to sleep by calling the sleep method The thread calls an operation that is blocking on input/output, that is, an operation that will not return to its caller until input and output operations are complete The thread tries to acquire a lock that is currently held by another thread The thread waits for a condition

13 The Dead State A thread is dead for one of two reasons:
It dies a natural death because the run method exits normally It dies abruptly because an uncaught exception terminates the run method

14 III. Thread Properties

15 Thread Priorities By default, a thread inherits the priority of its parent thread, that is, the thread that started it Every thread has a priority value between MIN_PRIORITY (1) and MAX_PRIORITY (10). NORM_PRIORITY is defined as 5 Thread priorities are highly system dependent. Thus, it is best to treat thread priorities only as hints to the scheduler

16 Daemon Threads A daemon is simply a thread that has no other role in life than to serve others To turn a thread into a daemon thread: t.setDaemoon(true); Examples are timer threads that send regular "timer ticks" to other threads. When only daemon threads remain, the virtual machine exists. There is no point in keeping the program running if all remaining threads are daemons.

17 Reference Core Java, Volume I – Fundamentals, Eighth Edition, Chapter 14. Cay S. Horstmann and Gary Cornell. Prentice Hall, 2008


Download ppt "More About Threads."

Similar presentations


Ads by Google