0

I have just started learning threads. So i was trying to write few thread based programs.I wanted to print alphabets and numbers one after the other.

I used wait and notify for this. Now, I want to use yield for the same.

public class Y {

    public static void main(String[] args) {

        X an = new X(false);

        Thread t1 = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    an.Alpha();
                }
                catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    an.numbers();
                }
                catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t2.start();
    }
}

class X
{
    boolean flag;

    X(boolean flag)
    {
        this.flag = flag;
    }

    synchronized void Alpha() throws InterruptedException
    {
        for (char i = 'A'; i <= 'Z';i++)
        {
            while (flag == false)
            {
                System.out.println(+i);
                //notifyAll();
                flag = true;
            }
            Thread.yield();
            //wait();
        }
    }

    synchronized void numbers() throws InterruptedException
    {
        for (int i = 1;i <= 26;i++)
        {
            while (flag == true)
            {
                System.out.println(+i);
                //notifyAll();
                flag = false;
            }
            Thread.yield();
            //wait();
        }
    }
}

I'm unable to get the same output as i'm getting with the wait/notify.

I know there's something missing that i don't understand about yield.Can anybody help in clearing my concept about yield and fix the above code.

Output with wait/notify : 65 1 66 2 67 3

Output with yield : 65 1

AS Mackay
  • 2,831
  • 9
  • 19
  • 25
Ankush Dutt
  • 143
  • 8
  • Quoting the advice from *Effective Java*: "Given the difficulty of using wait and notify correctly, you should use the higher-level concurrency utilities instead". If you are learning, learn to use classes in `java.util.concurrent`; only when you understand their usage and limitations should you look to "do it yourself". – Andy Turner Oct 29 '18 at 09:40
  • Additionally, [the Javadoc of `Thread.yield()` says](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#yield--): "It is rarely appropriate to use this method. It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions." – Andy Turner Oct 29 '18 at 09:44
  • @AndyTurner Yes, I completely agree with the statement but i was trying to understand the "yield".Suppose i have two threads T1 and T2 and both are running. First, I use yield on T1 and T2 is still running.Then, I use yield on T2. Would T1 see any opportunity to resume its operations since yield gives a hint to the CPU to put another thread in the queue. – Ankush Dutt Oct 29 '18 at 09:53
  • 1
    What I am saying is: don't bother trying to understand it, not yet. You will *never* (or vanishingly close to never) need to use it in anger; and there are far more useful things to learn about instead. – Andy Turner Oct 29 '18 at 09:55
  • The documentation for `t.yield()` says that it is a _hint_. That word, "hint", means that `t.yield()` is not actually required to do anything at all. Even if `t.yield()` does something in your JVM, it probably won't do anything that you are able to notice if your computer has more than one processor. – Solomon Slow Oct 29 '18 at 15:52

0 Answers0