/**
*
* 类简要描述
*
* <p>
* 类详细描述
* </p>
*
* @author think
*
*/
public class VolatileThread implements Runnable {
private volatile int a = 0;
@Override
public void run() {
// TODO Auto-generated method stub
// synchronized (this) {
a = a + 1;
System.out.println(Thread.currentThread().getName() + ":----" + a);
try {
Thread.sleep(100);
a = a + 2;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":----" + a);
// }
}
}
/**
*
* 类简要描述
*
* <p>
* 类详细描述
* </p>
*
* @author think
*
*/
public class VolatileMain {
public static void main(String[] args) {
VolatileThread s = new VolatileThread();
Thread t1 = new Thread(s);
Thread t2 = new Thread(s);
Thread t3 = new Thread(s);
Thread t4 = new Thread(s);
t1.start();
t2.start();
t3.start();
t4.start();
/* 同步的结果
Thread-2:----1
Thread-2:----3
Thread-0:----4
Thread-0:----6
Thread-3:----7
Thread-3:----9
Thread-1:----10
Thread-1:----12*/
/*
去掉同步的结果
Thread-0:----1
Thread-1:----2
Thread-2:----3
Thread-3:----4
Thread-0:----8
Thread-3:----10
Thread-1:----10
Thread-2:----12*/
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有