Explain about Thread priority in Java?


 In Java, each thread is assigned as priority which affects the order in which it is schedule for running.

 The Threads of the same priority are given equal treatment by the Java scheduler.

 Java permits us to set the priority of a thread using the setPriority ( )

ThreadName.setPriority ( int number );

The number is an integer value to which the threads priority is set. The Thread class defined several priority constants

MID- PRIORITY=1

NORM-PRIORITY=5

MAX =PRIORITY =10

The int number may assume one of these constants or any value between 1 and 10

 Default setting is NORM – PRIORITY.

Most user level processes should be use NORM-PPIORITY, plus or minus 1.

Whenever multiple threads are ready for execution the java system chooses the highest priority thread and executes it. For a thread of lower priority to gain control, one of the following things should happen:

     1.      It stops running at the end of run().
     2.      It is made to sleep using sleep().
     3.      It is told to wait using wait().

However, if another thread of a higher priority comes along, the currently running thread will be preempted by the incoming thread thus forcing the current thread to move to the runnable state.

Remember that the highest priority thread always preempts any lower priority threads.



Related

Java 2908686711970232429

Post a Comment

emo-but-icon

item