Priority of threads

 

Threads are assigned priorities that the thread scheduler can use to determine how the threads will be scheduled. The thread scheduler can use thread priorities to determine which thread gets to run. The thread scheduler favors giving CPU time to the thread with the highest priority in the Ready-to-run state. This is not necessarily the thread that has been the longest time in the Ready-to-run state. Heavy reliance on thread priorities for the behavior of a program can make the program unportable across platforms, as thread scheduling is host platform-dependent.


Priorities are integer values from 1 (lowest priority given by the constantThread.MIN_PRIORITY) to 10 (highest priority given by the constantThread.MAX_PRIORITY).

The default priority is 5 (Thread.NORM_PRIORITY).

A thread inherits the priority of its parent thread. Priority of a thread can be set using thesetPriority() method and read using the getPriority() method, both of which are defined in the Thread class.

The following code sets the priority of the thread myThread to the minimum of two values: maximum priority and current priority incremented to the next level:


myThread.setPriority(Math.min( Thread.MAX_PRIORITY,myThread.getPriority()+1));.

 

Advertisements

Constructive and genuine appreciation and/or criticism most welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s