Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cancellation.

Similar presentations


Presentation on theme: "Cancellation."— Presentation transcript:

1 Cancellation

2 Why we should terminate our threads cleanly?

3

4 NeverEnds (at least) two threads are created:
first thread is main thread, executes main()  second thread is t main() thread terminates almost immediately. t never terminates - loops forever program will never terminate process only terminates when all of its threads have terminated Runnable based thread terminates when  run() does

5 t.stop()? No JAVA interface to shutting down threads from the outside
no way to invoke t.stop() from main

6 t.stop()? assume Java allows one thread to stop another
assume while T1 calls T2.stop(), T2 holds some locks and is in the middle of changing the internal state of some object protected by these locks. if locks are not released when T2 is stopped - deadlock in the future if locks are immediately released the object which T2 was working on is in inconsistent state

7 introduce a new Exception?
introduce a new exception – ThreadDeath calls to stop() throws ThreadDeath to the thread, interrupting the thread's current execution. good in theory but impractical and dangerous: ThreadDeath exception can be thrown anywhere! examine all code, take care of cleanup prior to terminating thread ThreadDeath exception can be thrown even while we are cleaning after a previous ThreadDeath exception

8 How to terminate threads cleanly?
use a variable in Thread object that indicates someone requested this thread to stop thread object checks this variable at regular intervals in run() method when it is safe to do so determine whether it accepts to stop (return from run) or not

9 How to terminate threads cleanly?
thread cancellation is a cooperative protocol: one thread asks another one to stop receiving thread can accept or reject request

10

11 How to interrupt long waits?
Threads may sometime be stopped for a long period of time. Threads may: blocked waiting to enter synchronize block sleep() wait() join() perform IO

12 interrupted() Sometimes, when we want to tell a thread T that it needs to stop T may not notice our request for some time, since T's execution is currently stopped interrupted status - internal flag to every thread : interrupt() - flag may be set by any other thread Thread.isInterrupted() - flag may be checked and cleared

13 interrupted flag If Thread T is in If thread, D calls T.interrupt():
Sleep() wait() join() If thread, D  calls T.interrupt(): InterruptedException will be thrown to T break from wait interrupted status of T is cleared

14 interrupted flag If  T is performing IO, using an interruptible channel: channel will be closed  T's interrupt status will be set ClosedByInterruptException will be thrown to T

15 interrupted flag If T is waiting for a Selector to return
T's interrupt status will be set call to the selector will return.

16 interrupted flag If none of the previous conditions holds then 's interrupt status will be set.

17 interrupt mechanism is used in conjunction with the methods mentioned above


Download ppt "Cancellation."

Similar presentations


Ads by Google