Thread Priority Constants

Chapter 6 - Thread Prioritization

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Thread-Scheduling Scenarios
There are many thread-scheduling algorithms that might be used on a particular platform. If you simultaneously consider the three following scenarios, you should end up with a good design that will work well on a wide variety of VM implementations .
Scenario I: One High-Priority Thread Hogs Processor
The highest-priority thread that is ready-to-run gets to run until it blocks, it yields the processor, or an even higher-priority thread becomes ready-to-run. In this case, the thread scheduler does not force the thread to move off the processor, but it waits for some condition to change. In this scenario, the following events can happen:
  Lower-priority threads may never get a chance to run and may become starved for processor time.
  Threads of equal priority dont get a chance to run until the running thread yields or blocks.
  If a thread of higher priority becomes ready-to-run, it will pre-empt the currently running thread.
Scenario II: All High-Priority Threads Hog Processor
This is a slight variation on Scenario I where the thread scheduler swaps threads of equal priority with each other. Now the running thread will continue to run until it blocks, it yields the processor, or a thread of higher or equal priority becomes ready-to-run:
  Lower-priority threads may never get a chance to run and may become starved for processor time.
  Threads of equal priority each get a chance to run.
  If a thread of higher priority becomes ready-to-run, it will pre-empt the currently running thread.
Scenario III: All Threads Get Some Time on the Processor
In this case, the thread scheduler makes sure that all threads get some time on the processor regardless of their priority. However, higher-priority threads will be scheduled for more processor time than lower-priority threads. This is the behavior observed in Listing 6.6 :
  Lower-priority threads will get some processor time, although it will be less than higher-priority threads get.
  Threads of equal priority each get a chance to run.
  When higher-priority threads become ready-to-run, they will generally pre-empt a thread that is running at a lower priority. But, because all threads get some time on the processor, there might be short periods of time when a high-priority thread is ready-to-run, but is held back briefly while a lower-priority thread gets a small slice of time.
This last scenario is really the ideal case, but you will need to code with the other two in mind to be sure that each thread gets some time on the processorregardless of the particular VM that the code is running on.
  Tip When assigning priorities to the threads in your application, use the higher priorities only for threads that block frequently (sleeping, waiting, I/O). CPU- intensive calculations should be done with a medium- to low-priority thread to ensure that the processor is not hogged. Avoid setting a priority to Thread.MAX_PRIORITY unless the thread spends nearly all of its time blocked or is very short-lived.

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

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