my dog learned polymorphism*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Priorities" Watch "Thread Priorities" New topic
Author

Thread Priorities

Jon Kidd
Greenhorn

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
Jon
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
Greenhorn

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 ?
Jonathan
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.
-Barry
Jon Kidd
Greenhorn

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

Joined: Oct 10, 2002
Posts: 1572
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.
cheers,
Kathy


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
 
 
subject: Thread Priorities
 
Similar Threads
Executor runnable priority
Thread's Priority
yield() on synchronized code
Please give me reply.
Thread Priority Question