title: In Java 6, why doesn't higher priority thread not run even if lower priority thread yields?
body: <p>In the code below, I am trying to get the higher priority thread to run by yielding the lower priority thread. But it doesn't seem to work, the higher priority thread seems to run after lower priority thread finishes. Can anyone explain what I am doing wrong?</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  public class TestThreadPriority extends Thread {
  
      static List&lt;String&gt; messages = new ArrayList&lt;String&gt;();
  
      public void run() {
          Thread t = Thread.currentThread();
          int priority = t.getPriority();
          String name = t.getName();
          messages.add(name + ":" + priority);        
          Thread.yield();
          messages.add(name + ":" + priority);
      }
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          Thread t = Thread.currentThread();
          t.setPriority(MIN_PRIORITY);
          int priority = t.getPriority();
          String name = t.getName();
          messages.add(name + ":" + priority);
  
          Thread tp1 = new TestThreadPriority();
          tp1.setPriority(MIN_PRIORITY);      
          Thread tp2 = new TestThreadPriority();
          tp2.setPriority(MAX_PRIORITY);      
  
          tp1.start();
          tp2.start();
  
          Thread.yield();
          messages.add(name + ":" + priority);
  
          for(int i = 0; i &lt; messages.size(); i++) {
              System.out.println((i+1) + ". " + messages.get(i));
          }
      }
  }
  </code></pre>
  
  <p>Output is:</p>
  
  <blockquote>
    <ol>
    <li>main:1</li>
    <li>Thread-0:1</li>
    <li>Thread-0:1</li>
    <li>main:1</li>
    <li>Thread-1:10</li>
    <li>Thread-1:10</li>
    </ol>
  </blockquote>
  
  <p>Any help is greatly appreciated.</p>
  
  <p>Thanks,
  Quadir</p>