Quiz: Thread Priority and the Thread Scheduler in Java

Total: 6 questions

1. 

What is thread priority in Java, and what is the default priority?

Thread priority is an integer from 1 to 10: the higher it is, the more willing the scheduler should be to give the thread the CPU. The bounds have constants: Thread.MIN_PRIORITY (1), Thread.NORM_PRIORITY (5) and Thread.MAX_PRIORITY (10). You set a priority with setPriority() and read it with getPriority().

Strictly speaking, there is no "default priority": a new thread gets the priority of the thread that created it. main runs at 5, so almost every thread starts at 5. A value outside 1-10 throws IllegalArgumentException, and a value above ThreadGroup.getMaxPriority() is silently cut down to the group's maximum. A priority is only a hint to the OS scheduler, so program logic must never depend on it.

2. 

Who decides which Java thread runs: the JVM or the operating system?

For regular (platform) threads in HotSpot, it's the operating system's scheduler: every Java thread is an OS thread, and the JVM merely passes its priority on to the OS - and not always. The line "the scheduler is part of the JVM" dates back to green threads (Java 1.1 on Solaris).

  • Linux: by default (-XX:ThreadPriorityPolicy=0) the priority isn't passed to the OS, all threads get the normal priority.
  • Windows: the 10 Java levels are mapped onto 5 Windows levels, so neighboring values (say, 5 and 6) are the same to the OS.
  • Virtual threads: scheduled by the JVM itself (ForkJoinPool), priority is ignored.

To check whether a priority reached the OS, take a thread dump (jstack or jcmd <pid> Thread.print): prio is the Java priority, os_prio is what the OS received.

3. 

What is a context switch, and when does it happen?

A context switch is the moment a CPU core stops running one thread and starts another: the first thread's state (registers, program counter, stack pointer) is saved, and the second one's is loaded. This is where priority is used. The textbook model "the thread goes to the end of its priority queue, a thread from the highest-priority queue is loaded" is simplified: Linux, for example, keeps no per-priority queues and hands out time in proportion to a thread's weight (nice).

A switch happens when:

  • the time slice has run out;
  • the thread gave up the CPU voluntarily (Thread.yield(), sleep(), wait(), join()) or blocked on I/O or a monitor;
  • the thread was preempted by a higher-priority one - if the OS honors priorities;
  • the thread finished.

Switching isn't free: the new thread starts with a "cold" CPU cache.

4. 

What does Java guarantee about the order of thread execution, and what doesn't it?

Almost nothing ("When it comes to threads, very little is guaranteed"). Not guaranteed: that threads run in the order start() was called; that a higher-priority thread starts first; that a thread, once started, finishes without interruption; that the result is the same on every run; that a thread gets the CPU quickly (starvation is possible, and daemon threads are cut off when the last non-daemon thread ends or System.exit() is called).

Guaranteed: the order of actions within one thread - from that thread's own point of view; without synchronization, another thread may see them in a different order. If order matters, set it explicitly: join(), synchronized, wait()/notify() or java.util.concurrent classes. Priorities are not on that list.

5. 

How are thread priorities related to starvation, and what is priority inversion?

Starvation: if the OS strictly honors priorities and the CPU is busy, a low-priority thread may go without CPU time for a long while. That's why high priorities for "important" threads sometimes break a program - a background thread that frees resources can no longer keep up. Operating systems fight this (Linux shares time in proportion to weight, Windows temporarily boosts threads that have waited long), but these are OS mechanisms, not Java ones.

Priority inversion: a thread with MIN_PRIORITY holds a synchronized monitor, and a thread with MAX_PRIORITY wants to enter the same block. The high-priority thread sits in BLOCKED, waiting for the low-priority one, which gets the CPU least often of all. The monitor doesn't pass priority along, so here a high priority works against you.

6. 

What priority do virtual threads have in Java 21?

Always NORM_PRIORITY: getPriority() returns 5, and setPriority() ignores the new value. Virtual threads aren't scheduled by the OS but by the JVM itself - a built-in scheduler based on ForkJoinPool - and it doesn't take priorities into account.

The 1-10 range is still checked first, so setPriority(11) throws IllegalArgumentException on a virtual thread too. The Thread.ofVirtual() builder has no priority() method - only Thread.ofPlatform() does. You can't raise the priority of carrier threads either: the JVM manages them. If some tasks need a guaranteed share of the CPU, give them a separate, bounded pool of platform threads.

Page 1 of 1