Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread priority question.

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following statements about this program are correct?

a "AAA End" will always be printed before "BBB End".


b "BBB End" will always be printed before "AAA End".


c The order of "AAA End" and "BBB End" cannot be determined.


d The program will not compile.


e THe program will throw an exception at runtime.

--
ref: jqplus
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well the answer is "c" but why its not "a" in fact we are setting the priorities of threads!!
this is something really confusing
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting a thread's priority doesn't guarantee that the higher priority thread will run before the lower priority thread. You can read tutorials on thread priorities. Like this tutorial which says:
Rule of thumb: At any given time, the highest priority thread is running. However, this is not guaranteed. The thread scheduler may choose to run a lower priority thread to avoid starvation. For this reason, use priority only to affect scheduling policy for efficiency purposes. Do not rely on thread priority for algorithm correctness.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good one ankit - thanks, addition to above: following explanation given by simulator

In this case, the program is trying to manipulate the scheduling of thread using priorities which is a bad idea. Simply because operating systems behave differently about prioities. For ex. Win32 uses Time Slicing ie. it gives time to all the thread in proportion of their priorites but many unix systems do not let low priority threads run at all if a higher priority thread is running. So the output cannot be determined.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic