File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic