This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Thread's Priority

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 25823
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What were you expecting, and why were you expecting that?
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was expecting this,
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?

 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry, I have one question. The main() is running, which is right but after main() executes start() method for all three threads then why JVM is not taking the MAX_Priority thread first and then the NORM_Priority and so on?

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?

 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 25823
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:Remember that the priority is only used if the OS needs to choose one thread over another.



However, playing devil's advocate: The API documentation for Thread says

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.
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry, Praveen and Paul for all the answers.
But i still can not find the conclusion here.
Does it mean that it is unpredictable now a days because we have more that one core machines?
And what should be the answer for this kind of questions in exam?
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.



I thought of that too -- which is why my previous response was based on "likely" possibilites. Some unlikely ones are ...

4. It is possible that two priorities are the same priority due to lack of a large enough range in the underlying threading system. This is unlikely because the example uses priorities that are far apart.

5. It is possible that the priorities are being overridden because it is under a thread group. I am going to assume that this is a small test program, so this is not possible.

6. It could be affected by the thread system mechanism to prevent thread starvation -- which for short running threads, is very unlikely.


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

Henry
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.



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
 
Paul Clapham
Marshal
Posts: 25823
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.



I didn't mean to suggest that it was wrong, just that it was overly simple.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



I put in a 10000L loop inside run(), but still didn't get the expected output, so am inclined more towards your reason (3).


100th post, can I be a Sheriff now?
    Bookmark Topic Watch Topic
  • New Topic