What is Thread priority?

Threads always run with some priority, usually represented as a number between 1
and 10.
In most JVMs, however, the scheduler does use thread priorities in one important
way: If a thread enters the runnable state, and it has a higher priority than any of
the threads in the pool and a higher priority than the currently running thread,
the lower-priority running thread usually will be bumped back to runnable and the
highest-priority thread will be chosen to run. In other words, at any given time the
currently running thread usually will not have a priority that is lower than any of
the threads in the pool. In most cases, the running thread will be of equal or greater
priority than the highest priority threads in the pool.
Setting a Thread's Priority
FooRunnable r = new FooRunnable();
Thread t = new Thread(r);
t.setPriority(8);
t.start();
A thread’s default prority is 5.
Thread class has the three following
constants (static final variables) that define the range of thread priorities:
Thread.MIN_PRIORITY (1)
Thread.NORM_PRIORITY (5)
Thread.MAX_PRIORITY (10)
If priority not explicitly set, a thread's priority will have the same priority as the
priority of the thread that created it.

No comments:

Post a Comment