File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Thread priority and scheduling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread priority and scheduling" Watch "Thread priority and scheduling" New topic

Thread priority and scheduling

deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi all,
I've a question.
suppose there are a number of threads in a program. each having different priority.
now if i run this program on a m/c that uses time slicing as the scheduling algo. then will the priority of threads matter?
In my opinion the answer is -- no
I feel that all the threads will get equal CPU time irrespective of their priority and even a lower priority thread may be started by th OS even by preemping the higher priority thread if the turn of lower priority thread comes.
please comment


Paul Caudle
Ranch Hand

Joined: Jun 08, 2000
Posts: 64
Hi deekasha,
With time-sliced thread scheduling, there is no definitive way to allocate CPU time to a specific thread, but the priorities used are one of the deciding factors in the scheduler's decision to choose a specific one.
As an example, create an application that has three threads all running on different priorities. Have them print out different things in a loop perhaps . You'll see that the thread with the highest priority will finish it's job first almost every time (assuming they start at similar times) and the thread with the lowest priority will finish last almost every time.

You are right that time-sliced will not accurately utilize the priority of each thread like we as programmers may want, but it does allocate more time to the higher priority threads in the long run. I guess it just increases the odds that the higher priority threads will get CPU time.
I hope that helps somewhat.
Sameer Rao

Joined: Oct 12, 2000
Posts: 20
Can OS allot more time to a Higher priority Thread in Time-Slice Scheduling OS ?
[This message has been edited by Sameer Rao (edited October 19, 2000).]

deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi Sameer,
Earlier I was confused about thread scheduling but while going through JLS my doubt got clear.

JLS clearly states (last para of 17.12):
Every Thread has a priority. When there is competition for processing resourcees, threads with higher priority are generally executed inprference to threads with lower priority.
Such preference is not however , a guarntee that the highest priority will always be running.

Note the generally word in above quote .
as far as your question goes then the straight forward answer is scheduler is free to schedule threads in accordance to the scheduling algo it follows.It may respect the priority it may not.
so once u say thread.start() the control is out of your hands and scheduler may run them in any order.


I agree. Here's the link:
subject: Thread priority and scheduling
It's not a secret anymore!