File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Threads - priority Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Threads - priority" Watch "Threads - priority" New topic
Author

Threads - priority

bolisetti sriharsha
Greenhorn

Joined: Sep 02, 2011
Posts: 8
I am learning threads and got stuck here

The output I am getting is
5
5
10
This is from main
This is a new thread!

My question is Shouldn't MyThread be executed first as it has highest priority ?

Please Explain

Thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
Calling Thread.start does not mean that the thread will start executing right at that moment; the JVM must first decide to switch away from the current thread. I'm not sure what all the factors are that go into that decision, but the length of the remaining byte code (very short in this case) may well be a factor.

In general, you should not rely on setting thread priorities for trying to force scheduling decisions; that may not work the way you expect, or not work at all.


Ping & DNS - updated with new look and Ping home screen widget
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

bolisetti sriharsha wrote:I am learning threads and got stuck here

The output I am getting is
5
5
10
This is from main
This is a new thread!

My question is Shouldn't MyThread be executed first as it has highest priority ?

Please Explain

Thanks in advance



Hint: What happens when the OS has to choose between a high priority thread that isn't runnable (or started yet) and a lower priority thread that is runnable?

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads - priority
 
Similar Threads
notifyAll()
Thread Related Problem
Thread------ getPriority.
Understanding Thread Priorities
setPriority() is not working fine.