• Post Reply Bookmark Topic Watch Topic
  • New Topic

priority in threads  RSS feed

 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why setting priority in threads couldn't guarantee consistency in thread execution??
As written in K&B book:

Don't rely on thread priorities when designing your multithreaded application.
Because thread-scheduling priority behavior is not guaranteed, use thread
priorities as a way to improve the efficiency of your program, but just be sure
your program doesn't depend on that behavior for correctness




thanks in advance
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start, the priorities differ from one operating system to another. Avoid thread priorities if you want portable code.
 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ritchi
Here I'm witting a code which i expected to show consistent output but unfortunately it doesn't.



In this code,when all the user defined thread are started in "main" thread, 1st thread "java" should run as it has more priority,then it sleeps and
then from runnable pool which has "oracle" thread and ".net" thread "oracle" should be chosen by scheduler as running thread as it has priority more than
".net" then "oracle" goes to sleep and then the only thread in runnable state is ".net" goes into running state
then after sleep duration is complete again "java" thread should be chosen as it has more priority, then "oracle" and then ".net" .......
thus output should be
java
oracle
.net
java
oracle
.net

continues 150 times.



But output is not at all consistent.
please rectify mistake in my thinking.


thanks in advance
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't (and can't) rely on thread priorities. It's probably best not to use them.
 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Dittmer
well i'm quoting my understanding about the above program can you please check i'm correct or wrong and why?


In this code,when all the user defined thread are started in "main" thread, 1st thread "java" should run as it has more priority,then it sleeps and
then from runnable pool which has "oracle" thread and ".net" thread "oracle" should be chosen by scheduler as running thread as it has priority more than
".net" then "oracle" goes to sleep and then the only thread in runnable state is ".net" goes into running state
then after sleep duration is complete again "java" thread should be chosen as it has more priority, then "oracle" and then ".net" .......
thus output should be
java
oracle
.net
java
oracle
.net

the output continues 150 times



with regards
Roopam
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're wrong because of what I wrote in my previous post: you can't rely on thread priorities. You might (quite reasonably) assume that they work, but they don't, and certainly not reliably.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!