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
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.
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