Originally posted by Monica Moncho:
hi,
I think I read that if a thread is running and there is another one with higher priority in the pool, the one running will wait and the higher priority one will run, this done by the scheduler. But this is not for sure.
But then I have that: the guarantee that we have is that the running thread will have the same or higher priority than the ones in the pool.
Aren't the two above opposite? which one is true? will we ever have a lower priority running while higher priority wait? Is the guarantee always true only if we do yield()?.
Thank you.
What you are describing is pre-emptive sheduling, if you have a high priority thread that goes into a blocked state, then a lower priority thread will run. As soon as the high priority thread reenters the runnable state, it will run. And yes, this is done by the scheduler.
Your second paragraph is basically describing the same thing... but I would caution the
word "guarantee", it is not guaranteed by the JVM. Specifically:
- On some older JVMs, or JVMs on OS that doesn't support this type of scheduling, there can be a lag before the higher priority thread becomes the currently running thread.
- Most JVMs uses the underlying threading system... so for Windows, it will try to make sure every thread gets a time slice, including a lower priority thread when there is a higher priority thread that is runnable.
Henry