| Author |
setPriority not working as expected
|
Jason Anderson
Greenhorn
Joined: Apr 21, 2002
Posts: 2
|
|
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
Joined: Jan 07, 2002
Posts: 2205
|
|
|
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.
|
Rob
SCJP 1.4
|
 |
Jason Anderson
Greenhorn
Joined: Apr 21, 2002
Posts: 2
|
|
|
Thank you, Rob. I appreciate the help.
|
 |
Peter Haggar
author
Ranch Hand
Joined: Jan 03, 2001
Posts: 106
|
|
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
|
Senior Software Engineer, IBM
author of: Practical Java
|
 |
 |
|
|
subject: setPriority not working as expected
|
|
|