package com.sinoservices.chainwork.srm.tender.service.impl;
import java.util.concurrent.TimeUnit;
class Mydata{
volatile int number = 0;
public void addTo60() {
this.number = 60;
}
}
public class VolatileDemo {
public static void main(String[] args) {
Mydata mydata = new Mydata();
new Thread(()->{
System.out.println(Thread.currentThread().getName() + “\t come in”);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
mydata.addTo60();
System.out.println(Thread.currentThread().getName() + "\t update number to " + mydata.number);
},“AA”).start();
while (mydata.number == 0) {
//main线程一直在循环,直到number值不等于0
}
System.out.println(Thread.currentThread().getName() + "\t mission is over number value is" + mydata.number);
}
}