This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Mock Question 24

 
Matt Ghiold
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Marcus says the answer is 1, but I think 3 is also true, it may not happen, but it's still a viable reason.
Thoughts?
-Matt
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
Actually the behaviour of yield method is platfrom dependent so its unpredicatble what it does...it may allow some other thread with higher prioprity to run and itself go into runnable state or may not even do that...
Now,1 seems correct as it says that its called from currently running thread which is true. but in 3 case its not very clear which thread is calling it and it looks as if the statement is sure that the Higher priority thread will be invoked by yeidling(which is not always the case).
Hope this helps.
swapna
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the doc is not really accurate about this but the specification of yield is really to yield for same-priority threads.
 
Matt Ghiold
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I guess if it comes up in the exam, it will tell me how many correct answers there are, so I won't have to worry about it, I knew 1 was correct, but at the same time, I felt 3 was correct even though it wasn't guranteed it was still a viable option.
-Matt
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers
It has been a while since I was here, but I am checking on you guys.
I do not think yield is platform dependent. Yield is just a way to check if there are other threads with same or higher priority waiting and you want to give that other thread a chance, rather than your thread consuming the CPU cycle.
Hope this Helps.
Amish
Originally posted by swapna sivaraju:
HI
Actually the behaviour of yield method is platfrom dependent so its unpredicatble what it does...it may allow some other thread with higher prioprity to run and itself go into runnable state or may not even do that...
Now,1 seems correct as it says that its called from currently running thread which is true. but in 3 case its not very clear which thread is calling it and it looks as if the statement is sure that the Higher priority thread will be invoked by yeidling(which is not always the case).
Hope this helps.
swapna
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Java tutorial:

In addition, a given thread may, at any time, give up its right to execute by calling the yield method. Threads can only yield the CPU to other threads of the same priority--attempts to yield to a lower priority thread are ignored.
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u Amish and Valentin...got to do some more reading on this!I got the thing very wrong!!

swapna
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by swapna sivaraju:
Thank u Amish and Valentin...got to do some more reading on this!I got the thing very wrong!!

swapna

Hi
Check on Bruce Eckel's Thinking in Java for good examples about yield. He represents it nicely.
Hope this helps!
-Amish
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the question asks "Under what circumstances might you use the yield method of the Thread class"
Option 3 might be an answer to a quesiton such as
"Might a Thread of a higher priority run when the yield method is called",
but it asks under what circumstances might you use the yield method (ie you the programmer use it, rather than what might happen if you use it). Because of the platform dependent nature of Threading and the inconsistent way that priorities work. The programmers exam will question you on issues relating to the platform dependent nature of Threading. I try to keep my questions as unambiguous as possible and I know this comes close to the edge, but understanding the implications of when and where to call the yield method is important.
 
Matt Ghiold
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank's Marcus! I appreciate the insite, because I am sure it will come in very handy when I take the exam.
-Matt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic