• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Marcus Green mock exam - 2 #24

 
Ranch Hand
Posts: 108
  • 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 or higher 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

The answer mentioned was (1).Is (1) fine ???.
Suppose when yield() is executed ,there is only one "lower priority thread" that is in the ready state . In such a case won'nt that thread run,irrespective of what priority it holds .
Pls give ur opinion .
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that's the case, the thread that issued the yield call will immediately continue executing. This might even depend on the thread scheduler.
Bosun
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also think #3 is a correct answer, any comments?
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Savio Mascarenhas:
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 or higher 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

The answer mentioned was (1).Is (1) fine ???.
Suppose when yield() is executed ,there is only one "lower priority thread" that is in the ready state . In such a case won'nt that thread run,irrespective of what priority it holds .
Pls give ur opinion .


The answer is obviously 1
1. whenever you call yield, you call it for the currently executing thread only. Once this thread yields, it will quit the running state and reach the ready state. So any thread that is in the ready state which has the same priority or higher priority will take over the CPU.
2. false. the yield() method does not take any parameters. So, it cannot call any other thread like yield(t). Also, if the thread t1 is running, you cannot have a statement t2.yield() within the run of t1. You can just have yield() which means, that thread whose run is being executed(t1 in this case) has decided to yield on its own ( some kind-heartedness that )
3. false. When a thread yields, the new thread that takes over
need not always have a higher priority. It may be an equal priority thread also.
4. false -- as given in explanation of option 2
Regarding your final question, i think what you say is true. If the waiting thread is of a lower priority, if it is the only thread -- the process should be the current thread yields, goes to the ready state and then comes back to the running state.
Guess somebody more knowledgable regarding threads can ascertain that. Or probably we can try it with a program.
Hope this helps.
------------------
Regards,
Shree
[This message has been edited by shree vijay (edited December 09, 2000).]
 
Skool. Stay in. Smartness. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic