0

maybe what I hope to express isn't so clearly, the first case is a sample about when and how to use volatile, And to make the program run successfully, We need to add the volatile.

the second is in the hope to express that, even without the volatile, the program sitll run successfully. And I hope to know why this will happen without ‘volatile’

In the first sample, a typical sample of using volatile

public static int num=1;

public static class MyThread extends Thread {
    // flag
    private boolean flag = false ;
    public boolean isFlag() { return flag;}
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) { e.printStackTrace();
        }
// change flag to true
        this.flag = true ;
        System.out.println("flag=" + flag);
        this.flag=true;
    }
}
// main
static void testWithOutVolatile(){
    MyThread t=new MyThread();
    t.start();
    while(true) {
        boolean is=t.flag;
        if (is) {
            System.out.println("run======");
        }
    }
}

After started, the main thread won't find the change of flag unless using volatile

However, in the sample, unexpectedly, thread2 got the change of flag, why this happen?

static int amb=0;
static void testSimple(){
    Thread t1=new Thread(()->{
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        amb++;
    });
    Thread t2=new Thread(()->{while(true) {
        if (amb == 0) {
            System.out.println("no");
        }
        if (amb != 0) {
            System.out.println("SUc");
            break;
        }
    }});
    t2.start();
    t1.start();
}

And After a try, I find if I remove the code

if (amb == 0) {
            System.out.println("no");
        }

it will run as I thought, thread2 can't get the change.

Thanks for your answer, QwQ

Jirath Liu
  • 23
  • 4
  • Honestly I dont understand your question, but let me clarify a few things. In order for a variable to share its state across multiple threads in Java(in real time), it needs to be Volatile. Using Static variables: They are shared between threads, but the changes made in one thread may not be visible to another thread immediately, making it seem like there are two copies of the variable. https://stackoverflow.com/questions/13582395/sharing-a-variable-between-multiple-different-threads https://www.xspdf.com/resolution/52087767.html – JCompetence Dec 26 '20 at 10:16
  • sorry, maybe what I hope to express isn't so clearly, the first case is a sample about when and how to use `volatile`, And to make the program run successfully, We need to add the volatile. the second is in the hope to express that, even without the volatile, the program sitll run successfully. And I hope to know why this will happen without ‘volatile’ – Jirath Liu Dec 26 '20 at 10:24
  • Actually, @SusanMustafa - that is not strictly true that it *needs to be* `volatile`. There are other ways. For example, by using `synchronized` appropriately, or by using an `AtomicXxx` type, or by using `final` field. – Stephen C Dec 26 '20 at 11:03
  • @StephenC Yes Stephen, perhaps my comment might put the impression that it is the only way, however I did give an example stackoverflow for more clarification. – JCompetence Dec 26 '20 at 11:05
  • @StephenC hhh, thanks, get the reasons feels good :P – Jirath Liu Dec 26 '20 at 11:12

1 Answers1

0

May Be, in the second case, the io statement refresh the buffer zone of Thread

Thread t2=new Thread(()->{while(true) {
        System.out.println("no");
        if (amb != 0) {
            System.out.println("SUc");
            break;
        }
    }});

if I use an io statement(Sout), it will work successfully. results of using io

In this way, I keep finding reasons in println And I find the true reason

 public void println(String x) {
    synchronized (this) {
        print(x);
        newLine();
    }
}

synchronized caused the switch of thread

and with the switch, the buffer zone of the thread was clear,

so thread2 read a new value

Jirath Liu
  • 23
  • 4