aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread's Priority Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Watch "Thread New topic
Author

Thread's Priority

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136

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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

What were you expecting, and why were you expecting that?
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
I was expecting this,
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40


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?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
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?
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Nov 23, 2011
Posts: 136
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
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jul 03, 2011
Posts: 256
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread's Priority