This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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 ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic