Janki Shah wrote:
Output is ,
What I was Expecting is this,
When I run this different time, It did give the result what I was expecting but I don't understand why I am not getting the result according to the priorities everytime? why there is change in results?
Henry Wong wrote:
There could be many reasons for these results.... but a few likely possibilities are...
1. The thread is too short lived. It is entirely possible for a thread to start, and finish running, before the main thread starts the next (possibly higher priority) thread.
2. The main thread has a priority -- which many people seem to forget. It is possible to start a new thread, which will preempt the main thread -- if the priority is higher than the main thread. This means that the main thread won't start the next thread until it can get scheduled.
But the most likely reason is....
3. You are running on a machine with more than one core. Heck, even a low end laptop has four cores these days. With three threads and four cores, it really doesn't matter what priorities are. They each have a core to run on. Remember that the priority is only used if the OS needs to choose one thread over another.
Henry
Janki Shah wrote:
Output is ,
What I was Expecting is this,
When I run this different time, It did give the result what I was expecting but I don't understand why I am not getting the result according to the priorities everytime? why there is change in results?
Henry Wong wrote:Remember that the priority is only used if the OS needs to choose one thread over another.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority.
Praveen Kumar M K wrote:I just searched about this on the web and it seems jvm's thread scheduling scheme(jvm's implementation) comes into the picture too...whether its a round-robin or a time-slicing scheme. Perhaps you could look into that.
Paul Clapham wrote:
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority.
And that's all it has to say about priorities. Which would surely lead to confusion such as we see in Janki's post. I expect that documentation was written back in the days where nobody had multi-core machines, and so on, but I do think it needs one of those "but it's actually more complicated than that" qualifications.
Henry Wong wrote:The documentation isn't wrong -- "preference" implies having to choose. When the operation system can run both the high and low priority thread in parrallel -- there is no need to choose.
Henry Wong wrote:
Regardless, without actually debugging it, we won't know for sure -- however, if I have to place money on it, I say that it is a combination of reason (1) and reason (3).
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|