title: What are the main uses of yield(), and how does it differ from join() and interrupt()?
body: <p>I am a little bit confused about the use of <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html#yield()" rel="nofollow noreferrer"><code>Thread.yield()</code></a> method in Java, specifically in the example code below. I've also read that yield() is 'used to prevent execution of a thread'.</p>
  <p>My questions are:</p>
  <ol>
  <li><p>I believe the code below result in the same output both when using <code>yield()</code> and when not using it. Is this correct?</p>
  </li>
  <li><p>What are, in fact, the main uses of <code>yield()</code>?</p>
  </li>
  <li><p>In what ways is <code>yield()</code> different from the <code>join()</code> and <code>interrupt()</code> methods?</p>
  </li>
  </ol>
  <p>The code example:</p>
  <pre><code>public class MyRunnable implements Runnable {
  
     public static void main(String[] args) {
        Thread t = new Thread(new MyRunnable());
        t.start();
  
        for(int i=0; i&lt;5; i++) {
            System.out.println(&quot;Inside main&quot;);
        }
     }
  
     public void run() {
        for(int i=0; i&lt;5; i++) {
            System.out.println(&quot;Inside run&quot;);
            Thread.yield();
        }
     }
  }
  </code></pre>
  <p>I obtain the same output using the code above both with and without using <code>yield()</code>:</p>
  <pre><code>Inside main
  Inside main
  Inside main
  Inside main
  Inside main
  Inside run
  Inside run
  Inside run
  Inside run
  Inside run
  </code></pre>