Java& Threads
Threads are essentially subprocesses1. Informally,
you can think of them as tasks that belong to a program and that can run
"simultaneously". Depending on the number of CPUs available and the
number of competing threads, some of those threads actually will run in
parallel on different CPUs, whilst in other cases the illusion of simultaneous
execution will be achieved by "juggling" threads in and out of the
available CPUs. A part of the OS called the thread scheduler takes care of
deciding which threads to allocate CPU time to (on which CPUs) and when.
Recognise conditions that might prevent a thread
from executing.
The expression "prevent a thread from
executing" is slightly ambiguous, does it mean a thread that has been
deliberately paused, or does it also include threads that have died?. A thread
that is prevented from executing is said to be blocked.
Reasons a thread may be blocked
A thread may be blocked because
1) It has been put to sleep for a set amount of time
2) It is suspended with a call to suspend() and will
be blocked until a resume() message
3) The thread is suspended by call to wait(), and
will become runnable on a notify or notifyAll message.
For the purposes of the exam sleep(), and
wait/notify are probably the most important of the situations where a thread
can be blocked.
The sleep method is static and pauses execution for
a set number of milliseconds. There is a version that is supposed to pause for
a set number of nanoseconds, though I find it hard to believe many people will
work on a machine or Java implementation that will work to that level of
accuracy. Here is an example of putting a Thread to sleep, note how the sleep
method throws InterruptedException. The thread
public class TSleep extends Thread{
public static void main(String argv[]){
TSleep
t = new TSleep();
t.start();
}
public
void run(){
try{
while(true){
this.sleep(1000);
System.out.println("looping while");
}
}catch(InterruptedException ie){}
}
}
With the release of the Java2 platform the Thread
methods stop, suspend and resume have been deprecated (no longer recommended
for use, and will produce a warning at compile time). The JDK notes have the
contain the following notice
//Quote
Deprecated. This method has been deprecated, as it
is inherently deadlock-prone. If the target thread holds a lock on the monitor
protecting a critical system resource when it is suspended, no thread can
access this resource until the target thread is resumed. If the thread that
would resume the target thread attempts to lock this monitor prior to calling
resume, deadlock results. Such deadlocks typically manifest themselves as
"frozen" processes. For more information see Why are Thread.stop,
Thread.suspend and Thread.resume Deprecated?.
//End Quote
Thread blocking via the wait/notify protocol is
covered in the next topic 7.3
Because of the platform dependent nature of Java
threading you cannot be certain if a thread will ever give up its use of CPU
resources to other threads. On some operating systems the threading algorithm
may automatically give different threads a share of the CPU time, on others one
thread might simply hog processor resources. For this reason the Java Thread
class has a static method called yield which causes the currently running
thread to yield its hold on CPU cycles. This thread returns to the "ready
to run" state and the thread scheduling system has a chance to give other
threads the attention of the CPU. If no other threads are in a "ready to
run state" the thread that was executing may restart running again.
It is hard to demonstrate the benefits of using the
yield method without being able to run some sample code on two different
implementations on Java with different thread scheduling systems. Assuming that
option is not available to you it is worth describing two main different
scheduling systems used in operating systems.
Each thread gets a set amount of CPU time for
executing. Once it has used up its time with the CPU, it is removed from
accessing the CPU and any other waiting Threads get a chance at CPU time. When
each thread has had its chance with the CPU the cycle starts again.The beauty
of this approach is that you can be confident that each thread will get at
least some time executing.
A priority system is used to decide which thread
will run. A thread with the highest priority gets time with the CPU. A program
under this system needs to be created in such a way that it
"voluntarily" yield access to the CPU.
The Java Programmers exam does not expect you to
understand or know about the system for setting Thread priorities. However it
is worthwhile knowing about it, and its limitations so you can understand the
importance of using the yield method of the Thread class. The priority of a
thread can be set using the Thread.setPriority class and you can find out the
priority by using the getPriority method. A newly created Thread gets set to
Thread.NORM_PRIORITY.
No comments:
Post a Comment