File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread priority question. 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 priority question." Watch "thread priority question." New topic

thread priority question.

Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

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

SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

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

Joined: Aug 03, 2008
Posts: 9462

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.

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

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.
I agree. Here's the link:
subject: thread priority question.
jQuery in Action, 3rd edition