Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread's priority, why doesn't work?  RSS feed

 
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 /> }<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);
}
}
}
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wonda,
what is the difference between calling a threads start method and calling the threads run method directly.
example instead of calling
Thread t=new Thread()
t.start();
and saying
t.run();
It looks as if that since t.start() calls t.run() there should be no difference. However there is a signinficant difference due to which one is asked to call start not run. The difference is that calling start creates a new thread while calling run executes the run method in the current thread not another thread. Thus by executing your code there are three threads competing for the system resources.
The main method will give up its right to the processor only when it is over with its work, is put to sleep() or calls wait(). It can also be stopped by other threads of higher priority. i am not considering the deprecated methods here. Else it does not give up the processer to another thread.
In you code the number of iterations were too small to matter specifically 10 for threading concepts to work. Between a thread's call to the start method and the run method other threads run methods are being executed and that is why you will find other threads output in between and the result in my code below. I have also increased all iterations to 100 so that the priorities will matter. You will find priorities in the working below. thus the first thread is stopped in between by the higher priority thread till it completes its work. Then the normal priority thread completes its thread along with the main thread.
However Threading concepts are platform dependentent and one should not depend on this. This may not work as it does here in all platforms.
In the code below the thread with the greatest priority gains control of the processor.



Regds.
Rahul.


[This message has been edited by Rahul Mahindrakar (edited August 17, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the order in which the threads executed was dependent on the JVM implementation/underlying OS architecture. I recall reading somewhere that you can't depend on the priority of the treads for scheduling execution order.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right that thread priorities are system dependent.
Regds.
Rahul
[This message has been edited by Rahul Mahindrakar (edited August 17, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!