Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6224117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:36:55+00:00 2026-05-24T08:36:55+00:00

I am a little bit confused about the use of Thread.yield() method in Java,

  • 0

I am a little bit confused about the use of Thread.yield() method in Java, specifically in the example code below. I’ve also read that yield() is ‘used to prevent execution of a thread’.

My questions are:

  1. I believe the code below result in the same output both when using yield() and when not using it. Is this correct?

  2. What are, in fact, the main uses of yield()?

  3. In what ways is yield() different from the join() and interrupt() methods?

The code example:

public class MyRunnable implements Runnable {

   public static void main(String[] args) {
      Thread t = new Thread(new MyRunnable());
      t.start();

      for(int i=0; i<5; i++) {
          System.out.println("Inside main");
      }
   }

   public void run() {
      for(int i=0; i<5; i++) {
          System.out.println("Inside run");
          Thread.yield();
      }
   }
}

I obtain the same output using the code above both with and without using yield():

Inside main
Inside main
Inside main
Inside main
Inside main
Inside run
Inside run
Inside run
Inside run
Inside run
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:36:56+00:00Added an answer on May 24, 2026 at 8:36 am

    Source: http://www.javamex.com/tutorials/threads/yield.shtml

    Windows

    In the Hotspot implementation, the way that Thread.yield() works has
    changed between Java 5 and Java 6.

    In Java 5, Thread.yield() calls the Windows API call Sleep(0). This
    has the special effect of clearing the current thread’s quantum and
    putting it to the end of the queue for its priority level. In other
    words, all runnable threads of the same priority (and those of greater
    priority) will get a chance to run before the yielded thread is next
    given CPU time. When it is eventually re-scheduled, it will come back
    with a full full quantum, but doesn’t “carry over” any of the
    remaining quantum from the time of yielding. This behaviour is a
    little different from a non-zero sleep where the sleeping thread
    generally loses 1 quantum value (in effect, 1/3 of a 10 or 15ms tick).

    In Java 6, this behaviour was changed. The Hotspot VM now implements
    Thread.yield() using the Windows SwitchToThread() API call. This call
    makes the current thread give up its current timeslice, but not its
    entire quantum. This means that depending on the priorities of other
    threads, the yielding thread can be scheduled back in one interrupt
    period later
    . (See the section on thread scheduling for more
    information on timeslices.)

    Linux

    Under Linux, Hotspot simply calls sched_yield(). The consequences of
    this call are a little different, and possibly more severe than under
    Windows:

    • a yielded thread will not get another slice of CPU until all other threads have had a slice of CPU;
    • (at least in kernel 2.6.8 onwards), the fact that the thread has yielded is implicitly taken into account by the scheduler’s heuristics
      on its recent CPU allocation— thus, implicitly, a thread that has
      yielded could be given more CPU when scheduled in the future.

    (See the section on thread scheduling for more details on priorities
    and scheduling algorithms.)

    When to use yield()?

    I would say practically never. Its behaviour isn’t standardly defined
    and there are generally better ways to perform the tasks that you
    might want to perform with yield():

    • if you’re trying to use only a portion of the CPU, you can do this in a more controllable way by estimating how much CPU the thread
      has used in its last chunk of processing, then sleeping for some
      amount of time to compensate: see the sleep() method;
    • if you’re waiting for a process or resource to complete or become available, there are more efficient ways to accomplish this,
      such as by using join() to wait for another thread to complete, using
      the wait/notify mechanism to allow one thread to signal to another
      that a task is complete, or ideally by using one of the Java 5
      concurrency constructs such as a Semaphore or blocking queue.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little bit confused about which method to use : 1- using Jquery
I am little bit confused about use of destructor in c#. In my knowledge
I am a little bit confused about 2 things. Firstly when I create an
I´m a little bit confused by reading all the posts and tutorials about starting
I'm using SQLite as my database, and I'm a little bit confused about how
I'm a little bit confused about Git. When I'm looking through the manual it
I'm a little bit confused about how to approach this. I had never used
Ok. Im little bit confused about those permissions in linux so please people help
I am little bit confused about Zend authentication session storage system. I will try
I'm a little bit confused now after I've seen a code snippet for iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.