Interview Q&A · Java · 7 questions in this topic
Thread Priorities, yield() and Control Methods — Java Interview Q&A
How thread priorities and yield work, and why they are only hints to the scheduler. Part of Threads & Concurrency · 294 questions in the Java bank.
What are thread priorities?
Answer
Thread priorities are integer hints from 1 to 10 that suggest how urgently a thread should be scheduled. A new thread inherits the priority of the thread that created it, and the main thread normally runs at NORM_PRIORITY, which is 5. The scheduler may favour higher priority threads, but it is not required to do so. Correct code therefore treats priorities as tuning hints, never as guarantees of order.
What priority constants exist?
Answer
The Thread class defines three named constants for the allowed range. MIN_PRIORITY is 1 for the lowest priority, NORM_PRIORITY is 5 for the default priority, and MAX_PRIORITY is 10 for the highest priority. Custom priorities must fall between MIN_PRIORITY and MAX_PRIORITY inclusive. Using the named constants is clearer than hard-coding numbers.
Key points
- 1MIN_PRIORITY with value 1 for background work.
- 2NORM_PRIORITY with value 5, the default for new threads.
- 3MAX_PRIORITY with value 10 for urgent work.
How is a thread priority changed?
Answer
You read it with getPriority and change it with setPriority before or after the thread starts. The argument must be between MIN_PRIORITY and MAX_PRIORITY, otherwise setPriority throws IllegalArgumentException. Changing the priority only nudges the scheduler and does not guarantee execution order. Portable programs therefore use priorities only for coarse tuning, not for correctness.
Example
Thread t = new Thread(() -> System.out.println("work")); t.setPriority(Thread.MAX_PRIORITY); System.out.println("priority: " + t.getPriority());
Two equal-priority threads - which runs first?
Answer
The order is unspecified and depends on the JVM and operating system scheduler. Either thread may run first, and execution may interleave in any way. Common policies include round-robin time slicing, but Java does not promise any particular policy. You must not write code whose correctness depends on which equal-priority thread runs first.
Which methods affect thread execution?
Answer
The classic methods are sleep, join, and yield, plus the Object methods wait, notify, and notifyAll for coordination. The interrupt mechanism and the java.util.concurrent utilities also affect execution by signalling cancellation or parking threads. Modern code often prefers executors, futures, and LockSupport park and unpark over direct use of the low level methods. Interviews still expect you to know the classic list and what each one does.
Key points
- 1sleep pauses the current thread for a timed period without releasing locks.
- 2join waits for another thread to finish, with optional timeout.
- 3yield hints that the current thread is willing to let another thread run.
- 4wait, notify, and notifyAll coordinate threads on a shared monitor.
- 5interrupt requests cancellation, and LockSupport park blocks at a lower level.
What does yield() do?
Answer
Yield is a hint that the current thread is willing to pause briefly so another ready thread can run. It typically favours threads of the same priority, but the scheduler may ignore the hint entirely. Critically, yield has no timeout parameter and it never releases any lock the thread holds. The source claim that yield takes a timeout is wrong, so state clearly that it is only a best-effort scheduling hint.
Can a yielded thread run again?
Answer
Yes, a thread that calls yield stays in the RUNNABLE state. The scheduler may immediately reschedule it, especially if no other thread is ready. Yield never blocks the thread or moves it to a waiting state. It is therefore useless for coordination and should not be used to fix race conditions.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission