可见性代表:volatile
保证输出最大值肯定是100,原子性没有保证,禁止重排序
public class Count implements java.lang.Runnable{
String threadName;
volatile int a;
public Count(String threadName,int a){
this.threadName = threadName;
this.a = a;
}
@Override
public void run() {
while(true){
if(a==100)break;
a++;
System.out.println(this.threadName+" "+a);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
int a = 0;
new Thread(new test().new Count("1",a)).start();
new Thread(new test().new Count("2",a)).start();
new Thread(new test().new Count("3",a)).start();
}