|
|
|
|
|
|
|
|
|
|
|||||||||||||
|
Threaded environments like Java allows threads to put locks on shared resources so that while one thread is using a shared resource, the others cannot use it. The others should wait for the lock to be released. Threads are generally
used for calculation tasks which may take long time to complete. In addition,
we use threads to divide the computer's power among the different tasks.
While one thread is updating the display, the other may be computing the
next display.
There are three main controls exist to control threads:
public void run() public final void stop() You never call run() explicitly. It is called automatically by the runtime as necessary once you've called start(). There are also methods to supend and resume threads, to put threads to sleep and wake them up, to yield control to other threads, and many more. I'll discuss these later. The Runnable interface allows you to add threading to a class which, for one reason or another, cannot conveniently extend Thread. It declares a single method, run():
Thread priorities are defined as integers between 1 and 10. Ten is the highest priority. One is the lowest. The normal priority is five. Higher priority threads get more CPU time. Warning: This is exactly opposite to the normal UNIX way of prioritizing processes where the higher the priority number of a process, the less CPU time the process gets. We can use Thread.MAX_PRIORITY, Thread.MIN_PRIORITY and Thread.NORM_PRIORITY constants if we don't want to bother with numbers.
|
|
|
|
|
|
|