Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
What is thread priority in Java?
Thread priority is an integer hint, ranging from Thread.MIN_PRIORITY (1) to Thread.MAX_PRIORITY (10), with Thread.NORM_PRIORITY (5) as the default, that suggests to the thread scheduler how important a thread's execution is relative to others.
You set it with thread.setPriority(int) and read it with getPriority(). Higher-priority threads are more likely to be scheduled sooner or more often, but this is only a hint.
The actual scheduling behavior is delegated to the underlying operating system, so the effect of priority varies across platforms and JVM implementations, and it's never a substitute for correct synchronization. Relying on priority to guarantee ordering or exclusivity between threads is considered unreliable and is discouraged in production code.
More Related questions...