This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread.yield() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread.yield()" Watch "Thread.yield()" New topic


Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
Given a reference called
to a class which extends Thread, which of the following will cause the currently executing Thread to give up cycles to allow another thread to execute.

1) t.yield();
2) Thread.yield();
3) yield(100); //Or some other suitable amount in milliseconds
4) yield(t);

1.I worked on the above program,that showed 1 and 2 answers are correct.But i want to verify the answer since Threads topic is new for that correct answers are 1 and 2.Please let me know.

2.Is yield() is not a guaranted?

[ February 16, 2007: Message edited by: Shiva Mohan ]
[ February 16, 2007: Message edited by: Shiva Mohan ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
There is no version of yield in Thread which allows you to specify a time or Thread instance.

The documentation for yield says

Causes the currently executing thread object to temporarily pause and allow other threads to execute.

The reason that both 1 and 2 will work is that yield is a static method in Thread.

So you can call the method using an instance of Thread, but it is irrelevant which Thread instance calls it. The currently executing thread is the thread that pauses.
[ February 16, 2007: Message edited by: Keith Lynn ]
rocket mass heater kickstarter
subject: Thread.yield()