All ITKnowledge Source Code Search Tips Advanced Search |

| Mastering Java Threads by Marc Adler and David Herst DDC Publishing, Inc. ISBN: 1562438425 Pub Date: 05/01/99

Search this book:

Previous | Table of Contents | Next

## Chapter 7
Thread Priority

Lesson Topics

  • What is Thread Priority?
  • How the Scheduler Works
  • Time Slicing
  • Starvation

### What is Thread Priority

The Java Scheduler is in charge of allocating a single CPU among the multiple threads of a Java application. In general, you need only be concerned with scheduling when the application has one or more CPU-intensive threads. In this case, it helps to know what is going on behind the scenes in the Java VM so that you can optimize the execution of your application and avoid problems.

#### Priority Values

Every thread has a priority between MIN_PRIORITY and MAX_PRIORITY. As the priority value increases, so does the priority of the thread. Thus, a thread with a priority of 6 has greater priority than a thread with a priority of 4.

| When a thread is created, it is born with the same priority as the thread that created it.

The Java application can change the priority of a thread at runtime using the setPriority() method. The application can also query the thread for its priority level using the getPriority() method.

Maximum Priority

Threads within an Applet can have a maximum priority of 6. However, threads within a Java application can have the same maximum priority as the system, which is MAX-PRIORITY (10).

### How the Scheduler Works

The Java Scheduler uses a fixed priority system. This is in contrast to a variable priority system, in which a Scheduler can dynamically adjust the priority of processes in order to avoid starvation.

Below is a list that provides a behind-the-scenes view of how the Scheduler chooses which thread to run:

  • The highest priority thread is chosen to run.
  • If a higher priority thread becomes Runnable, the scheduler will run it. This is called preemptive scheduling.
  • If a thread enters the 1) Not Runnable state, 2) stops, or 3) yields, the scheduler will choose the next highest priority thread to run.
  • On systems that support time slicing, a thread will become Not Runnable after its CPU time allotment has expired.

| Never rely on the fact that the underlying operating system performs time slicing in your application. This makes your Java program nonportable.

#### Time Slices & the Queue

Internally, the Java VM has a separate queue for each priority level. When a thread (assume it is called T) finishes its time slice—or goes from the blocked to the unblocked state—it is placed at the back on the queue. As threads of the same priority finish their turn, Thread T moves toward the front of the queue. Eventually, Thread T will reach the front of the queue and get a chance to be picked by the Java Scheduler to run.

### Time Slicing

The implementation of threads largely depends on the threading mechanism of the underlying operating system on which the VM is running. Windows 95/98 and Windows NT support preemptive threading. Thus, Java threads running on Windows will be allocated a certain time slice and will be preempted when that period of time expires.

#### Sun Solaris = No Time Slicing

Contrary to Windows, the Solaris version of Java (from Sun Microsystems) uses a simple threading package that does not perform any type of time slicing. Therefore, threads under the Solaris version of Java will not be interrupted preemptively. Threads under Solaris must be preempted by the Java application.

#### Solaris vs. Windows

Consider a situation in which there is a two-threaded Java application, each thread having the same priority. If one thread runs continuously without ever explicitly yielding to the other thread, then—under Solaris—the second thread will never run. This second thread would be starved (described in the following section).

Unlike Solaris, however, under Windows the first and second thread would both get to run because the Windows-based VM will time slice between the two threads.

| Differences between operating systems in the underlying Java threading mechanism introduces some system-dependent behavior to Java.

### Starvation

If a high priority thread runs continuously without relinquishing control of the CPU (by either sleeping or yielding), then equal and lower priority threads might never have a chance to execute. This is commonly known as starvation. You should write your applications to make attempts to allow all threads a chance to run.

| If you have a CPU-intensive thread running in a Java Applet, you should use setPriority() to lower the priority of this thread, especially if this thread was created at the same priority level as the Applet itself. The Applet should never be starved.

| You should write your applications to make attempts to allow all threads a chance to run.

Previous | Table of Contents | Next

Copyright © DDC Publishing

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.