首先读读理论,百度有一大堆,简而言之:
- volatile:保证数据同步。
- static:保证数据唯一。
staitc相信用过的都能理解,常用于静态变量,保证多个对象共享一个变量。
volatile一般不常用,用接地气的话来讲一下它的应用场景:在多线程的并发编程时,只使用staitc一个关键字时,无法保证变量的值能够及时同步(一个线程改了变量的值,另一个线程立马读取使用,可能读到的是改变前的值,而不是改变后的值),此时想要保证同步,一般加上volatile就可以了。
想要更好的理解、掌握理论,最好的办法就是实践。
实践- 线程类代码
public class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
TestMain.number += 1;
}
System.out.println("----over----" + System.currentTimeMillis());
}
}
- 主类代码
public class TestMain {
public static int number = 0;
// public static Integer number = 0;
// public volatile static int number = 0;
// public volatile static Integer number = 0;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 3; i++) {
Thread thread = new Thread(new MyThread());
thread.start();
}
// 主线程休眠,保证其他线程的逻辑先执行
Thread.sleep(10);
System.out.println("----" + TestMain.number + "----" + System.currentTimeMillis());
}
}
变量测试
staitc
- 如上边主类代码中,number变量只使用static修饰时,运行结果如下
----over----1651722051922 ----over----1651722051922 ----over----1651722051922 ----3000----1651722051936volatile static
- number使用volatile static同时修饰时,结果显然不会有区别,因为只使用static的运行结果已经是正确的了
public volatile static int number = 0;
----over----1651722722729 ----over----1651722722729 ----over----1651722722729 ----3000----1651722722741结论
当变量是基本数据类型(如int)时,volatile作用不明显,可能是因为基本数据类型运算的较快,只使用static也能很快同步变量值,保证线程同步,如果计算逻辑较为复杂,最好还是加上volatile。
- 测试升级
为了确定volatile是有效果的,我把主类代码稍作改变,多嵌套一层for循环,代码如下:
public class TestMain {
public static int number = 0;
// public static Integer number = 0;
// public volatile static int number = 0;
// public volatile static Integer number = 0;
public static void main(String[] args) throws InterruptedException {
for (int n = 0; n < 10; n++) {
for (int i = 0; i < 3; i++) {
Thread thread = new Thread(new MyThread());
thread.start();
}
// 主线程休眠,保证其他线程的逻辑先执行
Thread.sleep(10);
System.out.println("----" + TestMain.number + "----" + System.currentTimeMillis());
}
}
}
- 运行结果
----over----1651729502396 ----over----1651729502396 ----over----1651729502396 ----3000----1651729502408 ----over----1651729502408 ----over----1651729502408 ----over----1651729502408 ----5382----1651729502423 ----over----1651729502423 ----over----1651729502423 ----over----1651729502423 ----8382----1651729502439 ----over----1651729502439 ----over----1651729502439 ----over----1651729502439 ----11382----1651729502455 ----over----1651729502455 ----over----1651729502455 ----over----1651729502455 ----14382----1651729502470 ----over----1651729502470 ----over----1651729502470 ----over----1651729502470 ----17382----1651729502486 ----over----1651729502486 ----over----1651729502486 ----over----1651729502486 ----20382----1651729502501 ----over----1651729502501 ----over----1651729502501 ----over----1651729502501 ----23382----1651729502517 ----over----1651729502517 ----over----1651729502517 ----over----1651729502517 ----26382----1651729502533 ----over----1651729502533 ----over----1651729502533 ----over----1651729502533 ----29091----1651729502549注意
上边这种升级测试结果,也只是偶尔正常出现,大多数情况变量值还是能正常同步的,也就是最终结果为30000。我估计这还是因为线程并发的不多,电脑性能完全足够才会这样,如果性能不好,可能每次都会差一些。这时候就能体现出volatile关键字的作用了。



