use of Yield() in Java
<p>I have following classes:</p>

<pre><code>package net.adjava.multithreading;

public class MyResource {

    private int a;



    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

}
</code></pre>

<hr>

<pre><code>package net.adjava.multithreading;

public class Thread1 extends Thread {
    MyResource m;

    public Thread1(MyResource m) {
        super();
        this.m = m;
    }

    @Override
    public void run() {
        System.out.println("Current Thread name1 :"
                + Thread.currentThread().getName());

        for (int i = 0; i &lt; 10000; i++) {
            m.setA(i);
            System.out.println("Set method sets the value of a as: " + i);
            System.out.println("Current Thread name1 :"
                    + Thread.currentThread().getName());

            Thread.yield();

        }
    }
}
</code></pre>

<hr>

<pre><code>package net.adjava.multithreading;

public class Thread2 extends Thread {

    MyResource m;
    public Thread2(MyResource m) {
        super();
        this.m = m;
    }
    @Override
    public void run() {
        System.out.println("Current Thread name2 :" + Thread.currentThread().getName());

        for (int i = 0; i &lt; 100; i++) {

            System.out.println(" value of a as per getter method is :"
                    + m.getA());
            System.out.println("Current Thread name2 :" + Thread.currentThread().getName());


        }
        System.out.println("waiting for thread to end");

    }

}
</code></pre>

<hr>

<pre><code>package net.adjava.multithreading;

public class ThreadExecutionPoint {

    public static void main(String args[])
    {
        System.out.println("Current Thread name main :" + Thread.currentThread().getName());

        MyResource m = new MyResource();
        Thread1 th1 = new Thread1(m);
        Thread2 th2 = new Thread2(m);
        th1.start();
        th2.start();

    }
}
</code></pre>

<p>I am trying to understand the purpose of yield()  with the above classes. Before trying this example all i knew about yield() was that it pauses the thread which calls it. So to test it i used the yield method in Thread1 class. So basically as per my understanding thread1 should execute for loop for one time and then should have paused and waited for other threads to complete. but the output show a different result. The output shows that first thread1 gets executed and then thread2. Can someone please correct what i am missing ? or is something wrong with my understanding for yield().</p>
