Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Priority

 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, the code in my previous post has a bug... it should say everything else is the same though.
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic