• 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

Marcus Green's Exam 3 #44 - yield()

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I found this question in one mock exam.
Which is the correct form of using the yield().
1.yield();
2.t.yield(); //assuming that t is the instance of the thred.
3.yield(500);
4.Thread.yield();
5.try
{
yield()
}
catch(InterruptedException e)
{}
The answers given are 1,4. But i think 2 is also correct.
Can someone please clarify.
Thanks.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think yeild() method cannot be called using an instance of thread.
 
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
can you give me some explanation please.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uma- you are right that t.yield() is correct. Since yield()is static you don't need an instance, but you can use one. (Try it raja, it works.) The actual object t is never really used - the compiler just looks up the declared type of the reference, and uses that as the class. So t.yield() is exactly equivalent to Thread.yield(), and it should be an answer.
On the other hand, I don't think (1) yield() should be an answer, unless the question states that it's called from a class which extends Thread. In general, you need to specify the class (directly or using an instance) to invoke a static method, as in "Thread.yield()" or "t.yield()". "yield()" by itself just causes the compiler to look in the current class for a method yield(), and it will complain if it doesn't find it.
What mock exam is this from? Let us know and we can move this into Mock Exam Errata.
 
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
Yes, The question is from marcus mock exam3. I changed the question. I couldn't remember the question exactly. Now i copied the question from marus site and posting it again. Take a look.
Given a reference called
t to a class which extends Thread, which of the following will cause it to give up cycles to
allow another thread to execute.
1) t.yield();
2) yeild()
3) yield(100) //Or some other suitable amount in milliseconds
4) yield(t);

The answer given is only 2. But i think 1 is also correct.
Any suggestions?
Thanks.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As discussed above - answer (1) is correct; answer (2) is not (unless the question is reworded slightly). Note that Marcus' Exam 3 is not "officially" released yet because Marcus is well aware there are errors he hasn't gotten around to fixing yet. But since Exam 3 is out anyway "unofficially", I'll go ahead and move this to Mock Exam Errata so people can find it.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have updated that question now. Because I did specify that the class extended Thread the t.yield() would compile (though it I think it is a slightly misleading example to give). And yield() on its own would work (though I had mis spelt it in my question).
But I was wondering what yield() on its own really means, is it the equivalent of this.yield()?
Threads, doncha love em. Thanks for pointing this out (and for Jim for emailing me). All and any corrections are greatfully received.
Marcus
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus- I can't yet see the modified question on your site, so I don't know if you have reworded the question, or just changed the answer. But if you haven't reworded the question, then yield() is not correct. You have stated that t is a reference to a class which extends Thread, but you haven't said that the code we're writing is within such a class. I.e. the following code fits your problem statement:
<code><pre>class MyThread extends Thread {}
class Test {
public static void main(String[] args) {
Thread t = new MyThread();
yield();
}
}</pre></code>
And this won't compile, because yield() is interpreted as this.yield() - the compiler looks for a yield() method in class Test, and can't find one, because Test does not extend Thread. t.yield() would compile, as would Thread.yield().
Actually, looking again at the original question, I'm now convinced there is no possible answer to the question as worded. The question asks what code will cause "it" to give up cycles, where "it" refers to the thread referenced by t. The problem is, there's no reason to think that t is the current thread - and the current thread is the only one that we can ever cause to yield(). The methods Thread.yield() and t.yield() may compile, but they don't necessarily cause t to yield. Nothing can, unless t is the curent thread.
Hmmmm... how about this:
Given
Given a reference called t to the current thread, which of the following will cause the current thread to give up cycles to allow another thread to execute? Assume this code is within a class which extends Thread.
1) t.yield();
2) yield()
3) yield(100) //Or some other suitable amount in milliseconds
4) yield(t);
Correct answers would be 1 & 2.
Or maybe this question would be better with some sample code instead?
Heh, writing a good question is harder than I realized.
reply
    Bookmark Topic Watch Topic
  • New Topic