• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setPriority not working as expected

 
Jason Anderson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to teach myself about threads for the certification exam. I wrote the following sample code:

I would expect this to output:
Thread5
Thread4
Thread3
Thread2
Thread1
However, it prints:
Thread5
Thread3
Thread4
Thread1
Thread2
Can anyone tell me why this isn't running the threads in order of priority?
Thanks,
Jason
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread scheduling is always up to the specific underlying implementation. You don't really have control in java to affect this. You can make *suggestions* to the Thread scheduler, such as calling Thread.yield() or setting a thread's priority, but there's no guarantee that these calls will be used for anything.
 
Jason Anderson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Rob. I appreciate the help.
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This creates problems when you want to control the order of execution of waiting threads. You can't rely on priority, or on which thread has been waiting the longest, even on the same JVM. To control the order of waiting threads you can use the specific notification pattern. Details and programming examples are here.
Peter Haggar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic