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.
- A thread has entered the dead state as the result of returning from its
- A thread's
sleep() method has been invoked and it is waiting for the timeout to expire.
- A thread is blocking on an I/O operation.
- A thread is waiting to acquire a lock on an object or class.
- A thread has invoked an object's
wait() method and is waiting for another thread to invoke the object's
- A thread is in the New state, but has not been started.
- 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.
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: ]
- t.join(); //here t is a reference to some other thread.
: a, b, d
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
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
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.
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.
method of the Thread class was deprecated by Java 2 several years ago.
This was done because
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.
method is also deprecated and it does not cause problems, but cannot be used without the
method as its counterpart.
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
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
- resume, or
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
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.