/**
* 原子性:指一个操作是不可中断的,即使在多个线程一起执行的时候,一个操作一旦开始,就不会被其他线程干扰
* long型数据在32位的系统中的读写不是原子性的(long有64位)
* 注意:在32位虚拟机下执行,会输出信息
*/
public class MultiTheadLong {
public static long t = 0;
public static class ChangeT implements Runnable{
private long to;
public ChangeT(long to) {
this.to = to;
}
@Override
public void run() {
while (true){
MultiTheadLong.t = to;
Thread.yield();
}
}
}
public static class ReadT implements Runnable{
@Override
public void run() {
while (true){
long tmp = MultiTheadLong.t;
if (tmp!=111L&&tmp!=-999L&&tmp!=333L&&tmp!=444L){
System.out.println(tmp);
Thread.yield();
}
}
}
}
public static void main(String[] args){
new Thread(new ChangeT(111L)).start();
new Thread(new ChangeT(-999L)).start();
new Thread(new ChangeT(333L)).start();
new Thread(new ChangeT(444L)).start();
new Thread(new ReadT()).start();
}
}