Changing Thread Priorities

   

As mentioned previously, multiple threads compete for CPU time. There are various algorithms used by the operating system and Java VM when figuring out which thread should be executed next . Thread scheduling can be divided into two main types: preemptive and non-preemptive. Java native threads usually support the preemptive model. This means that the schedule interrupts the current thread when its time for the processor runs out. With Java green threads, the non-preemptive model is normally used. This model allows the thread to continue until the thread yields control. However, even with the non-preemptive model, if a higher priority thread is ready to execute, the current thread has to give up the CPU.

The discussion of these different algorithms is beyond this book, but it's safe to say that a thread with a higher priority will get more attention than a thread with a lower priority. This can cause problems sometimes if a developer is not careful. If there is a thread in an application doing some background work occasionally and there are a bunch of higher priority threads doing work all the time, there is a possibility that the lower priority thread will never get the attention of the processor. This is known as thread starvation .

There are no hard and fast rules for how to set priorities for threads in your application. Each application has different constraints placed on it. However, most Java developers will agree that for threads doing mundane tasks that need to happen every so often, it's best to set their priorities low so they will execute when all the other threads have finished or are not running. For tasks in which something depends on the thread's completion, it's better to set the priorities of these threads high so they can execute and finish their tasks . An example of this might be a thread that checks a directory for a file and reads in the file if it exists. These types of threads should have a higher priority because another part of the application is probably depending on that data from the file.

To set the priority of a thread, use the setPriority method defined in the Thread class. The parameter to the setPriority method is an integer value that you want the new priority to be. The Thread class defines a minimum and maximum value for a thread priority. If the new priority is not between those ranges, an IllegalArgumentException will be thrown. Also, if the setPriority method was called by an outside thread instance and that thread did not have permission to change the priority, a SecurityException will be thrown.

Note

If you are creating Applets and the Applet attempts to change the priority of a Thread, a SecurityException will be thrown.


To determine what the current priority of a thread is, use the getPriority method defined on the Thread class. It returns an int value for the priority of that thread. The Thread class defines three constant's priority levels: minimum priority, normal priority, and maximum priority. Your applications can use them like this:

 setPriority(Thread.MIN_PRIORITY); 

Note

Most applications should always use one of the predefined constants. Using these constants ensures that you never set the priority outside the valid ranges.


Go back to the SimpleThread example from Listing 11.4 and change the priority of the last thread that gets started and see how the output is affected. Set the priority of the last thread to the maximum and all the other threads to the minimum. Hopefully you are imagining the outcome. All you have to do is create a new test main class and reuse the same SimpleThread class. Listing 11.9 shows the new class used to test setting the priorities of threads. Only the test class had to change. You can use the SimpleThread class as before.

Listing 11.9 Source Code for TestMainPriority.java
 public class TestMainPriority {   public static final int NUMBER_OF_THREADS = 5;   public static void main(String[] args)   {     SimpleThread[] threads = new SimpleThread[NUMBER_OF_THREADS];     for(int i = 0; i < NUMBER_OF_THREADS; i++)     {       String name = "Thread: " + i;       SimpleThread newThread = new SimpleThread(name);       threads[i] = newThread;       // If this is the last thread, set it's priority to the maximum       // otherwise set the priority of the thread to the minimum       if (i == NUMBER_OF_THREADS)       {         newThread.setPriority(Thread.MAX_PRIORITY);       }       else       {         newThread.setPriority(Thread.MIN_PRIORITY);       }     }     for (int i = 0; i < NUMBER_OF_THREADS; i++)     {       threads[i].start();     }   } } 

All you are doing in this class is just checking for the last thread defined by the constant NUMBER_OF_THREADS and when it is hit in the loop, you set the priority to the maximum constant defined in the Thread class. All the other threads get set to the minimum. Here's the output:

 C:\jdk1.3se_book\classes>java TestMainPriority Thread-0 entered run method Thread-0 going to sleep Thread-1 entered run method Thread-2 entered run method Thread-1 going to sleep Thread-3 entered run method Thread-4 entered run method Thread-2 going to sleep Thread-3 going to sleep Thread-4 going to sleep Thread-0  woke up from sleep Thread-1  woke up from sleep Thread-2  woke up from sleep Thread-3  woke up from sleep Thread-4  woke up from sleep C:\jdk1.3se_book\classes> 

Notice that after all the threads have been created, the last thread enters the run method first and then the rest of the threads enter the run method in a sort of random order. This is because the last thread has the maximum priority, so it will get the attention of the processor the most. So the results agree with what should have happened . Play around with changing some of the other priorities to different values and inspect the different results.

Tip

Remember that the priority value must be between the minimum and maximum constants defined in the Thread class. It's a good idea to try to use these values when setting priorities to prevent the IllegalArgumentException from being thrown.


   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net