• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic