• 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

Issue with Thread question in Marcus Green

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the question:
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 is 1 only, because, as the answer key wrote, (3) "but there is no guarantee that the thread that grabs the cpu time will be of a higher priority." But perhaps it is just bad wording of the question then. Because doesn't answer (1) entail that a yield() may be called to allow a thread of higher priority to start, even if that won't always happen. What do you guys think?
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think yield() is called to allow waiting threads of same priority to run. Any thread of higher priority than currently running thread will automatically preempt the currently running thread. I think option 1 is not completely right....
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yield() means to move a thread to the 'ready' state. After that, it is up to the scheduler to decide what thread to run next.
The scheduler would decide based on priority. If there is a thread of higher priority than the yielded thread, then it will run that. Otherwise, it may run that same thread, or other thread of same priority.
So I think, (1) is the correct answer.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Answer 3 should be a valid answer. yield() method has no gauranteed behaviour. It can be implemented differently by different vendors so there is no gaurantee whether a higher, same or lower priority thread may be allowed.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupam Sinha:
..there is no gaurantee whether a higher, same or lower priority thread may be allowed.


But in general, the higher priority thread will be executed.
The 'Thread' api doc says that


Threads with higher priority are executed in preference to threads with lower priority.


I think what makes (1) correct is that it is more complete with its statement.
What happens if there are no threads with higher priority? Will it execute just any thread? No. If there are other threads with same priority then it may execute any one of them, including the yielded thread. Otherwise, the yielded thread will be executed again.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton

What happens if there are no threads with higher priority? Will it execute just any thread? No.


What you have said is to quite true to quite an extent but not the whole truth. It can if the thread scheduler wishes execute any thread of any priority totaly disregarding the priority of the currently executing thread. But that generally doesn't happens. In the question it says
Under what circumstances might you use the yield method of the Thread class
Now the word here is might so it means that you might want to do the following things. Whats not written is that there is no gaurantee that they will happen. Secondly as the number of correct answers are not mentioned both should be considered as correct. You might have find answer 1 more complete but answer 3 is not wrong and it never says select only one answer.

The scheduler would decide based on priority. If there is a thread of higher priority than the yielded thread, then it will run that.


Alton this is what exactly the option no. 3 says.
Actually there is no correct answer because as mentoned before a thread of lower priority may be allowed to run. But now that you have to select an answer I would go with 1 and 3.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic