Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread.yield()

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given a reference called
t
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 me.is 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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic