aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes yield(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "yield();" Watch "yield();" New topic
Author

yield();

manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
Given a reference called t to a class which extends Thread,
which of the following will cause it to give up CPU to allow another thread to execute.
a) t.yield();
b) yield();
c) yield(t);
d) yield(t);
e) Thread.yield()
I think , the answer is a) and b) and e)
But, the answer says, its just b) and e).
Cany plz explain, why not a) is the correct option.
I thought a) and e) are almost same.
thanks!


MT
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
from the api for yield

public static void yield()
Causes the currently executing thread object to temporarily pause and allow other threads to execute.

1) The question seems to deal only with the way of calling to the yield method. Whether the currently executing thread effectively yields or not, depends on the priority of the others threads in the ready state.
Only a, b and e are proper ways of calling yield. And b only if the implicit this is of type Thread.
2) t will only stop execution if it is the currently executing thread. If another thread is executing t.yield() that thread will be possible stopped. This a trick possible thanks to yield being static and it can be called on any instance.


SCJP2. Please Indent your code using UBB Code
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
The question is faulty!
Calling yield() doesn't DEFINITELY stop a thread.
I saw a similar question :
What MIGHT cause a thread to stop ?
1.wait(), 2.stop(). 3.yield().....
yield() is one of the correct answer. It may or
may not cause a thread to stop. Whether calling
yield() will stop a thread or not depends on the
OS.
Mandar Patki
Greenhorn

Joined: Jun 27, 2002
Posts: 16
This is what I read from Java Certification Guide(-Heller)
A thread that has yielded goes to Ready state. There are 2 possible scenarios. If any other threads are in Ready state, then the thread that just yielded might have to wait a while before it gets to execute again.
However,if there are no other threads waiting, then the thread that just yielded will get to continue executing immidiately. MOST SCHEDULERS DO NOT STOP THE YIELDING THREAD FROM RUNNING IN FAVOR OF A THREAD OF LOWER PRIORITY.
-Mandar Patki
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
I think the answer should only be e) Thread.yield() because yield() could be overridden, such as this example:

In the above example, calling t.yield() or yield() will do nothing. Calling Thread.yield() is the only way to guarantee the desired behaviour.
[ July 01, 2002: Message edited by: Tybon Wu ]

SCJP2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: yield();
 
Similar Threads
Marcus Green #3, Q44
Threads...
Threads Execution
Marcus Green #3 Question 44
Marcus Mock # 3 Help!!