Can you tell me what Java people think are thread priorities?

Prev      Next

Answer: Sure - here is what people think threads do

Thread Priorities

The Java runtime supports a very simple, deterministic scheduling algorithm known as fixed priority scheduling. This algorithm schedules threads based on their priority relative to other "Runnable" threads.

When a Java thread is created, it inherits its priority from the thread that created it. You can also modify a thread's priority at any time after its creation using the setPriority() method. Thread priorities are integers ranging between MIN_PRIORITY and MAX_PRIORITY (constants defined in the Thread class). The higher the integer, the higher the priority. At any given time, when multiple threads are ready to be executed, the runtime system chooses the "Runnable" thread with the highest priority for execution. Only when that thread stops, yields, or becomes "Not Runnable" for some reason will a lower priority thread start executing. If two threads of the same priority are waiting for the CPU, the scheduler chooses one of them to run in a round-robin fashion. The chosen thread will run until one of the following conditions is true:

  1. a higher priority thread becomes "Runnable"
  2. it yields, or its run() method exits
  3. on systems that support time-slicing, its time allotment has expired

Then the second thread is given a chance to run, and so on, until the interpreter exits.

The Java runtime system's thread scheduling algorithm is also preemptive. If at any time a thread with a higher priority than all other "Runnable" threads becomes "Runnable", the runtime system chooses the new higher priority thread for execution. The new higher priority thread is said to preempt the other threads.

Rule of thumb: At any given time, the highest priority thread is running. However, this is not guaranteed. The thread scheduler may choose to run a lower priority thread to avoid starvation. For this reason, use priority only to affect scheduling policy for efficiency purposes. Do not rely on thread priority for algorithm correctness.

When a thread calls the wait() method:

  1. The thread releases the lock for the object.
  2. The state of the thread is set to blocked.
  3. The Thread is placed in the wait set for the object

When a thread calls the notify() method:

  1. An arbitrary thread is picked from the list of threads in the wait set.
  2. Moves the selected thread from the wait set to the entry set.
  3. Sets the state of the selected thread from blocked to runnable.


Every object in Java has a single lock associated with it. This lock is not used ordinarily.

When a method is declared as SYNCHRONIZED, calling the method requires acquiring the lock for the object.

When the lock is owned by a different thread the thread blocks and is put into the entry set of the object\ufffds lock.

A different thread is invoked to run in case one of the following events occur:

  1. The currently running thread exits the Runnable state ie either blocks or terminates.
  2. A thread with a higher priority than the thread currently running enters the Runnable state. The lower priority thread is preempted and the higher priority thread is scheduled to run.

Time Slicing is dependent on the implementation.

A thread can voluntarily yield control through the yield() method. Whenever a thread yields control of the CPU another thread of the same priority is scheduled to run.