jQuery in Action, 2nd edition*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Priority" Watch "Thread Priority" New topic
Author

Thread Priority

Ra Carter
Ranch Hand

Joined: Jul 30, 2005
Posts: 96
i have a question about how threads and setting their priority works. I am playing with this code now on sun jdk 1.5 on linux:



I won't put all of the output here but just the first few lines go like this:


so it looks like even after t3 (highest priority thread) is started, it continues to process t2 (lower priority of 5). and actually it will process t2 for quite some time before coming to t3 in my latest run but sometimes it will return to t1 and finish t3 last. i thought the MAX_PRIORITY means it should go first?
Ra Carter
Ranch Hand

Joined: Jul 30, 2005
Posts: 96
sorry, the code in my previous post has a bug... it should say everything else is the same though.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

so it looks like even after t3 (highest priority thread) is started, it continues to process t2 (lower priority of 5). and actually it will process t2 for quite some time before coming to t3 in my latest run but sometimes it will return to t1 and finish t3 last. i thought the MAX_PRIORITY means it should go first?


Basically, when you have two (or more) threads in a runnable state, the one would the higher priority should run more often. Unfortunately, you don't have any threads that are in a run state too often. Your threads spent most of its time in the blocked state, as it is trying to write IO to the screen.

Anyway, try this... I slightly modified your runner to be more cpu bound, instead of IO bound.



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: Thread Priority