Java – Thread Priority

Java – Thread Priority 2017-08-09T13:47:46+00:00

Java Thread Priority:

Each java thread has its own priority which decides the order of thread to be schedule.

The threads of equal priority will be given same treatment by java scheduler. And they will follow the FCFS (First Come First Serve) algorithm.

User can also set the priority of thread by using the setPriority() method as follow:

ThreadName.setPriority(int Number);

Here the number is integer value between 1 to 10, Here 1 is minimum priority 10 is maximum priority.

The Thread class defines few priority constants:




In any Thread the default priority is NORM_PRIORITY

In multithreading by assigning priority we can answer an input  as quickly as we want.

Whenever more than one threads are ready to run java system select the highest priority thread and execute it

If another thread of higher priority comes the running thread will be preempted by the incoming thread and current thread will move to runnable state.




Start Thread A

Start Thread B

End of main Thread

ThreadB strated

From ThreadB j = 1

From ThreadB j = 2

From ThreadB j = 3

From ThreadB j = 4

From ThreadB j = 5

Exit from B

ThreadA strated

From ThreadA i = 1

From ThreadA i = 2

From ThreadA i = 3

From ThreadA i = 4

From ThreadA i = 5

Exit from A

Here You can see  that we start ThreadA first but than also ThreadB completed its task first because of higher priority.

But again its multithreading so we all know that output may be vary each time you run the program.

Prev Next