File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread yield method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Thread yield method" Watch "Thread yield method" New topic
Author

Thread yield method

Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
Under what circumstances might you use the
yield method of the Thread class
A. To call from the currently running thread to allow
another thread of the same priority to run
B. To call on a waiting thread to allow it to run
C. To allow a thread of higher priority to run
D. To call from the currently running thread with a
parameter designating which thread should be allowed
to run.
The answer given was A. But I think the answer should be A and C since in the API doc yield is describe as
"Causes the currently executing thread object to temporarily pause and allow other threads to execute"
Other threads could mean thread of the same priority and higher priority. At least, that's what I think.

Thanks in advance....
Just For A Moment
Greenhorn

Joined: Apr 01, 2000
Posts: 4
I think the yield method is to allow another thread of same / lesser priority to run. The thread with the higher priority will anyway be taken care by the JVM.
Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
Thanx for your reply Just For A Moment...
But in Roberts and Heller p. 208, it says "Note that most schedulers do not stop yielding thread from running in favor of a thread of lower priority." So generally speaking, yield does not allow lower priority thread to run. In my opinion, a yielding thread would give thread of the same priority and higher priority the chance to run. I know what you mean by thread with the higher priority will be taken care by the JVM since the JVM will EVENTUALLY (but not immediately) allow the thread of higher priority to run. In my opinion, the yielding thread will make the higher priority to run much earlier than it is scheduled. In Thinking in Java, it defines yield() give control to other threads and some other books just define it just allowing other runnable thread to run.
Now, I'm left with this question in my mind. Should I include the answer
"To allow a thread of higher priority to run" and
"To allow a thread of lower priority to run" in my answer if ever this question is asked.
Need confirmation. I'm going to take the test soon.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Jerson
This topic came up long back. The static yield() method in Thread class is for giving a chance for other threads of SAME PRIORITY.
To allow a thread of higher priority to run.
Allowing a higher priority thread to run is, not the goal/job of yield method. It is JVM's. Simillarly for lower priority threads.
When a thread calls the yield() method it just gives the chance for SAME PRIORITY thread-mates. If none exists this again continues to run. Only ans a) is correct.
Refer to these 2 discussions here and here.
regds
maha anna

[This message has been edited by maha anna (edited April 01, 2000).]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Thread yield method
 
Similar Threads
Marcus Mock Question 24
Yield()
yield
Q #24, Marcus Green ..... yield() method of threads
Question from k&B's masterexam about yield()???????