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 Priorities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Priorities" Watch "Thread Priorities" New topic

Thread Priorities

Jon Kidd

Joined: Oct 11, 2002
Posts: 29
Hi all,
Failed my 1.4 exam recently but am confident that I'll be ok when I retake it
One of the questions was over thread priority ie setting one thread to max and another to minimum. Obviously I dont want to write the exact question out here so I'll try and explain it as best i can.
The answers ranged from the priorites having no affect to having total affect on how the threads acted.
When i run the code at home the thread priorities do have total affect on the outcome of the results but I noticed this statement on Suns website:
"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. "
So what gives, they are saying that you can't guarantee thread priorities but is that the answer they would be looking for in the exam ?
Thanks in advance
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Take a look here at this thread. Maybe that will answer some of your questions.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
API documentation says:

Causes the currently executing thread object to temporarily pause and allow other threads to execute.

JLS says:

Every thread has a priority. When there is competition for processing resources, threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running, and thread priorities cannot be used to reliably implement mutual exclusion.

Kathy says "No Guarantees"
What does the one that made the exam question say?? No one has an idea and none of the resources gives a "guaranteed" answer to what should the one that put the exam thought. I mean should we believe the API or the JLS? That's as far as I found about yield(). Even the long thread mentioned above didn't give a final word on what should one choose in the exam. We all came to an understanding that nothing about yield() and priorities is guaranteed but what should one answer in the exam, there was no agreement, and even if there was an agreement, who knows that this agreement goes with what the one that put the exam thinks, maybe he doesn't agree. Sun resources on that specific thing is vague, so No guarantees on the answer, I suppose.

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Jon Kidd

Joined: Oct 11, 2002
Posts: 29
Hi Barry,
Thanks for the reply. I think one of the answers was also something along the lines of 'insufficient information is provided here' ie if the snippet of code is part of a larger picture and there is no clue as to what system the code is running on then you cannot predict the result.
Do you think that is more likely the answer ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Jon, the "Gosling et. al" book says:
Exactly when preemption can occur depends on the virtual machine you have. There are no guarantees, only a general expectation that preference is typically given to running higher priority threads. Use priority only to effect scheduling policy for efficiency purposes. Do not rely on thread priority for algoritm correctness."

Now, knowing that the exam writers take Mr Gosling et. al as one of their guiding lights, I would look for the answer corresponding nearest to that quote.
Jon Kidd

Joined: Oct 11, 2002
Posts: 29
Thanks Barry, much appreciated.
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
I'd listen to Barry if I were taking the test
And if there *is* a conflict between API and JLS (rare, but it happens), go with Gosling. You are absolutely being tested on whether you understand the portability issues with respect to guaranteed behavior. So do not rely solely on what you see on your own system -- use the JLS.

Co-Author of Head First Design Patterns
Just a Jini girl living in a J2EE world.
I agree. Here's the link:
subject: Thread Priorities
It's not a secret anymore!