Yielding to Other Threads

   

You learned earlier about thread starvation. To refresh, thread starvation occurs when one or more threads are not getting enough attention from the CPU. There are several reasons this might occur:

  • Incorrect thread priorities

  • Staying too long in a sleep method

  • Other threads not yielding correctly

The first two possible reasons have already been discussed in previous sections of this chapter, so now focus on the third possibility: incorrect yielding. You can think of a thread yielding as "being a good citizen." Just because a thread has been given a high prioritydoes not mean that it should always have access to the CPU for performing its tasks . It sometimes needs to back away and let other threads have a chance. When the higher priority thread really needs to do something, it can gain control again through normal preemptive or non-preemptive means.

What this means is that as you are creating new thread classes and implementing the run method; keep in mind that you might need to insert some places where your thread manually gives up control if any other thread needs the control. You can do this by using the yield method in the Thread class. The yield method is a static method that causes the currently executing thread to temporarily pause and allow other threads of the same priority a chance to execute. If there are no other threads that are the same priority and are runnable, the original thread gains control and continues on. Again, it's up to each developer that is creating the thread to use the yield method properly and in the correct situation.

Now that you understand a little better what the yield is for, you might not have to use it all. The yield method is actually a hint to the Java Virtual Machine. It tells the JVM that if there are any threads that are runnable and the same priority, now is a good time to run them. If the operating system that you are using uses a preemptive scheduling technique, the JVM might completely ignore the yield suggestion. Remember, preemptive means that each thread has an amount of time that it can run before it is automatically interrupted . So in reality, the JVM or actually the scheduler might decide that it knows best and choose to ignore the suggestion. It's best to know how the operating system that the thread is executing on deals with threads. Just to be safe, it also a good idea to get in the habit of using the yield method. For one, Java is portable and the application might end up on an operating system that doesn't support preemptive.

Listing 11.17 shows a sample of how to incorporate the yield method into your thread class. Only the run method from a thread class is being shown in this example, not the entire thread class.

Listing 11.17 Source Code for an Example run Method
 public void run()   {     // Condition to determine when the thread should stop running     boolean keepRunning = true;     // While the condition says keep going     while( keepRunning )     {       // Some time intensive operation or calculation       if ( Math.random() < 0.1 )       {         keepRunning = false;       }       // Call yield here so that other threads of equal priority have an       // opportunity to execute       Thread.yield();     }   } 

Notice how the yield is called after the calculation to give other threads a chance to run.

   


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