Threading Model   «Prev  Next»


Lesson 9Preventing a thread from executing
ObjectiveDescribe those conditions that may prevent a thread from executing.

Preventing a Thread from executing in Java

The certification exam requires you to be able to identify those conditions that may prevent a thread from executing. You have already covered these conditions in previous lessons, but I will list them here so you can study them more easily.
  1. A thread has entered the dead state as the result of returning from its run() method.
  2. A thread's sleep() method has been invoked and it is waiting for the timeout to expire.
  3. A thread is blocking on an I/O operation.
  4. A thread is waiting to acquire a lock on an object or class.
  5. A thread has invoked an object's wait() method and is waiting for another thread to invoke the object's notify() or notifyAll() method.
  6. A thread is in the New state, but has not been started.
  7. Another thread is executing. (This usually means a higher priority thread, but depending on a system's scheduling algorithm, it could mean any thread.)

For each bullet in the above list, try to think of what must occur (if anything) to enable the thread to execute again (if possible). By doing so, you will be reviewing many of the major points that were covered in previous lessons.

Methods that will not stop the current thread from executing

Which of the following calls made by the current thread will NOT stop the current thread from executing?
(Assume that the calls are made from a subclass of Thread class.)
[Select 3 options: ]
  1. start()
  2. notify()
  3. wait()
  4. interrupt()
  5. t.join(); //here t is a reference to some other thread.


Answer: a, b, d
Explanation:
a. There are two possibilities:
1. The current thread is the same object as the subclass of Thread referred to in the question. In this case, calling start() amounts to starting the same thread again. Since the thread is already started, it will throw an exception. However, the thread will not stop running.
2. The current thread is a different object than the given subclass of Thread referred to in the question.
In this case, a new thread will be started and the current thread will keep running. If the other thread is already started, this call will get an exception. However, that will still not stop the current thread.
d. The interrupt() method does not stop the thread. This method merely sets the interrupted status of this thread to true.
e. Current thread will remain stopped as long as thread referred to by t is running.
It will stop the current thread from executing until the thread referenced by t ends.

Modern Way of Suspending, Resuming, and Stopping Threads

While the suspend( ), resume( ), and stop( ) methods defined by Thread seem to be a reasonable and convenient approach to managing the execution of threads, they must not be used for new Java programs for the following reasons.
The suspend( ) method of the Thread class was deprecated by Java 2 several years ago. This was done because suspend( ) can sometimes cause serious system failures. Assume that a thread has obtained locks on critical data structures. If that thread is suspended at that point, those locks are not relinquished. Other threads that may be waiting for those resources can be deadlocked.
The resume( ) method is also deprecated and it does not cause problems, but cannot be used without the suspend( ) method as its counterpart.
The stop( ) method of the Thread class was also deprecated by Java 2. This was done because this method can sometimes cause serious system failures. Assume that a thread is writing to a critically important data structure and has completed only part of its changes. If that thread is stopped at that point, that data structure might be left in a corrupted state. The trouble is that stop( ) causes any lock the "calling thread" holds to be released. Thus, the corrupted data might be used by another thread that is waiting on the same lock.
Because you cannot now use the suspend( ), resume( ), or stop( ) methods to control a thread, you might be thinking that no way exists to pause, restart, or terminate a thread.
However, this is not true. Instead, a thread must be designed so that the run( ) method periodically checks to determine whether that thread should
  1. suspend,
  2. resume, or
  3. stop
its own execution. Typically, this is accomplished by establishing a flag, variable that indicates the execution state of the thread.
As long as this flag is set to "running", the run( ) method must continue to let the thread execute. If this variable is set to "suspend", the thread must pause. If it is set to "stop", the thread must terminate. Of course, a variety of ways exist in which to write such code, but the central theme will be the same for all programs.