What is a thread and thread priority in Java: setPriority, getPriority methods

Each part of a program in a multithreaded model is known as a thread. Each of these threads follows different routes for execution. A thread can have many states like running (normal execution of a thread), suspended (stops activity), resumed (resume execution from suspended state), blocked (waiting state), or terminated (stops execution permanently). Since Java supports multithreading model, there has to be a priority for threads. However, thread priority has nothing to do with the speed of execution. It is used to switch between threads. This process of switching from one thread to another is known as context switching. There are two rules for thread priorities:

1) Threads can surrender the control by their own: A thread can give up control of execution for a high priority thread by going to sleep.

2) Threads favor preemptive multitasking: A higher priority thread is always given control of execution at any time.

There are two methods in Java for priorities of threads:

setPriority( ) method: setPriority method is used to set the priority of a thread.


final void setPriority(int level)

level can have the value:




getPriority( ) method: getPriority method is used to get the current priority of a thread.


final int getPriority( )

Leave a Reply