When multiple threads are ready to start the execution jvm chooses the runnable thread with highest priority for execution. If this thread is stop/yield/or becomes not runnable for some other reason then the low priority thread is executed.
In your code thread t2 has highest priority and thread t1 has lowest priority. So jvm starts thread t2 first. When the thread t2 is stopped then only the thread t1 is executed. So your code out put is like below
T2 done at xx T1 done at xx
Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
hi ragu what i thought is that the thread priority level will come only when the both threads are in runnable state. there is a chance the first thread t1 will get the execution of run method. When one thread running i doubt the thread with high priority will access the sheduler since thread 1 is running.
Hi, I think that whenever a thread with a higher priority come in the Runnable state, usually, the JVM will stop the thread he is running to do the higher priority one. Thats why in this case, T2 will finish first even if T1 is started first.
First, there are more than two threads, don't forget the main thread that is starting these two threads. In this scenario...
When the main thread starts the low priority thread nothing should happen, as the main thread has a higher priority than the newly started thread. When the main thread starts the high priority thread, that should cause a prempt to the newly created thread. Once the high priority thread finishes, that should cause a switch back to the main thread. Once the main thread finishes, that should cause a switch to the low priority thread.
Second, threading is susceptible to the underlying threading model. This means that for windows, the priorities are actually adjusted based on the current time slice, last time slice, etc. The high and low priority threads will actually round robin, in the long run -- albeit, it shouldn't be distributed evenly.
Originally posted by Maxime Duval: If the JVM use Round Robin, how is a high priority thread advantaged compared to a low one? Will it still get more cpu time or will all the thread get equal time slice?
Under Windows, a higher priority thread should get more CPU time than a lower priority thread. The main reason for this is to prevent thread starvation.
Joined: Apr 27, 2005
Thanks Henry ! Do you know of any major OS who dont use Round Robin to select witch task should run?
Originally posted by Maxime Duval: Thanks Henry ! Do you know of any major OS who dont use Round Robin to select witch task should run?
Most modern OSes today will time slice. However, Windows is not the norm. Most Unix variants (Solaris and Linux) will tend to time slice only within the same priority -- hence, it is possible for a high priority thread to starve a low priority thread.