wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q #24, Marcus Green ..... yield() method of threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q #24, Marcus Green ..... yield() method of threads" Watch "Q #24, Marcus Green ..... yield() method of threads" New topic
Author

Q #24, Marcus Green ..... yield() method of threads

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Question 24)
Under what circumstances might you use the yield method of the Thread class
1) To call from the currently running thread to allow another thread of the same priority to run
2) To call on a waiting thread to allow it to run
3) To allow a thread of higher priority to run
4) To call from the currently running thread with a parameter designating which thread should be allowed to run
I think the answer should be both 1 and 3, however the answer is only 1..can some one explai ..why not it also allows the threads of higher priority to run???
Wali
Ankur Gupta
Ranch Hand

Joined: Jun 13, 2000
Posts: 66
WaliM,
yield() is a static method. So, it affects only the currently executing thread. The answer "3" is partly wrong. When yield() is called it makes the currently executing thread to yield for some other thread to execute. The next thread that executes could be of the same or higher priority bit not necessarily higher, it could even be the same thread which had called yield(). In retrospect, answer "1" should have been worded a bit differently. IMHO, it should have been "same or higher", to remove any ambiguity.
Please, feel free to correct me.
regards,
Ankur
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for your repluy Ankur
I was also thinking the same way that it should be "same or higher priority threads"...
Wali
Savithri Devaraj
Ranch Hand

Joined: Jun 26, 2000
Posts: 103
Originally posted by Ankur:
WaliM,
yield() is a static method. So, it affects only the currently executing thread. The answer "3" is partly wrong. When yield() is called it makes the currently executing thread to yield for some other thread to execute. The next thread that executes could be of the same or higher priority bit not necessarily higher, it could even be the same thread which had called yield(). In retrospect, answer "1" should have been worded a bit differently. IMHO, it should have been "same or higher", to remove any ambiguity.
Please, feel free to correct me.
regards,
Ankur

I am not so sure. This again depends a lot on the scheduling algorithms on the target environment. I think the autor said
"to allow another thread of the same priority to run" on the assumption that if there were a thread of priority higher than this, then this thread would have been interrupted for the thread of higher priority anyway..
Just my 2 cents.
Savithri
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Q #24, Marcus Green ..... yield() method of threads