• Post Reply Bookmark Topic Watch Topic
  • New Topic

Theads priority

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code, setting two different priority, one is 6 the other is 9, why the one with 6 is executed before the one with 9? It seems the order is decided by the order I call start(). Pls. give me some help if you know the reason.
public class CounterThread extends Thread{
public static void main(String[] args){
CounterThread ct=new CounterThread();
DownCounter dc=new DownCounter();
dc.setPriority(Thread.NORM_PRIORITY+1);
ct.setPriority(Thread.MAX_PRIORITY-1);
System.out.println(ct.getPriority() + " " + dc.getPriority());
dc.start();
ct.start();

}
public void run(){
System.out.println("ct running");
for(int i=0;i<10;i++){<br /> System.out.println(getName() + " ii= " + i);<br /> }<br /> }<br /> }<br /> class DownCounter extends Thread{<br /> public void run(){<br /> System.out.println("Down running");<br /> for(int i=10;i>0;i--){
System.out.println(getName() + "i = " + i);
}
}
}
Pls. donot close this topic, before tell me what is the answer. Thanks
 
Lancy Mendonca
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a similar code to your except for a few changes. If you run this program for some time you will see the Thread2 running slighly ahead of Thread1
After running the program for 15 secs. The o/p I had was
T1115
T2120
* The drawbacks that I could see in your code were, the loop of 10 is too small to demonstrate priority and that too without a sleep
I might be wrong and would like to be corrected.
import java.util.*;
class T1 extends Thread
{
int i;
public void run()
{
for (;
{
i++;
System.out.println("T1"+i);
try
{
sleep(1000);
}
catch(InterruptedException ie){}
}
}
}
class T2 extends Thread
{
int i;
public void run()
{
for (;
{
i++;
System.out.println("T2"+i);

try
{
sleep(1000);
}
catch(InterruptedException ie){}
}
}
}
public class One
{
public static void main(String[] args){
T1 ct=new T1();
T2 dc=new T2();
ct.setPriority(Thread.MIN_PRIORITY);
dc.setPriority(Thread.MAX_PRIORITY-1);
System.out.println(ct.getPriority() + " " + dc.getPriority());
ct.start();
dc.start();
}
}
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

Its well described in RHE (complete java 2 certification study guide)
That is - The scheduler generally chooses the highest-priority waiting thread. If there is more than one waiting thread, the scheduler chooses one of them. There is no guarantee that the thread chosen will be the one that has been waiting the longest.
So you can't be sure which priority thread will execute first.
Hope it helps you
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I think the waiting thread with the highest priority should be selected, this should be garanteed, but why it's not like this when running? The thread with high priority runs after the thread with lower priority.
Thank you for your answer.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scheduling is platform dependent and it is all upto the thread scheduler. Nothing is guaranteed. If the scheduler implements priority-based scheduling, then it will honor the priorities and run the ones with the highest priority. If the scheduler implements preemptive-scheduling, it will not honor priority and every thread will get the same amount of time to run.
Hope this helps.
Ajith
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I should move this discussion to Threads forum, unless someone has an objection.
Ajith
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!