File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread priority 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" Watch "Thread priority" New topic
Author

Thread priority

Thamarai kannan
Greenhorn

Joined: May 05, 2009
Posts: 28
what is the use of set priority method..?
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
If two treads are runnable with different priority, JVM will pick thread with high priority to run.


SCJP 1.5(97%) My Blog
Thamarai kannan
Greenhorn

Joined: May 05, 2009
Posts: 28
thnx....but i tried with the following code.....i am not able to know its usage...


Main(String name,int a)
{
this.name=name;
this.a=a;
start();
}
public void run()
{
for(int i=0;i<=6;i++)
{try
{
sleep(1000);
System.out.println(name);
System.out.println(currentThread());
System.out.println(getPriority());
setPriority(a);
System.out.println(getPriority());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO code application logic here
new Main("start",10);
new Main("stop",5);
}
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Thread control is OS specific, the operating system chooses which Thread should get CPU cycles when. By setting the priority you give the OS a hint that Threads with higher priority should get more CPU cycles than Threads with lower priority. It is not something you can easily test with short-running Threads that don't hog CPU cycles. For example, in your code your Threads don't use a lot of CPU time, they go to sleep, which doesn't use the CPU, so there will be no Thread contention and the OS doesn't have to choose which Thread to run. In another point, the priority really only comes into play when Thread context needs to switch, so you will NOT see the affect of Priority when the number of Threads is less than or equal to the number of CPUs.

So to properly test Priority you would need a test suite which has more Threads than your computer has CPUs. And each Thread would need to be processor intensive, like by doing a lot of calculations and no waiting/blocking.


Steve
Thamarai kannan
Greenhorn

Joined: May 05, 2009
Posts: 28
thanks..if i didn't use sleep method.....at first thread with priority 5 is executing first........
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Thamarai kannan wrote:thanks..if i didn't use sleep method.....at first thread with priority 5 is executing first........


The Priority doesn't force anything, it is a hint. It doesn't guarantee who starts first, but suggests which should get more CPU time. A better test would be to set an infinite loop in the Threads that do some calculation and tracks the number of cycles performed. Then peek at the results on occasion and see which thread is doing more calculations. Over time, the Thread with the higher priority should get farther than the one with lower priority. The effect is only seen over time, and does not force the OS to choose one Thread over another.
shaiksha vali
Greenhorn

Joined: Jul 12, 2009
Posts: 11
Actually We use the setPriority( ) to set the priority to the threads.
In java we hava different types of thread priorities.This is used for which thread we have to give the highest priority
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

i think if you set the priority first and then start the thread, you will notice that the scheduler will pick the higher priority thread. Always the higher priority thread will be running.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread priority