• 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

Thread.yield()

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic