p345,有关volatile部分不太清楚。
package cc.openhome;html
class Variable1 { static int i = 0, j = 0; static void one() { i++; j++; } static void two() { System.out.printf("i = %d, j = %d%n", i, j); } } public class Variable1Test { public static void main(String[] args) { Thread thread1 = new Thread(() -> { while (true) { Variable1.one(); } }); Thread thread2 = new Thread(() -> { while (true) { Variable1.two(); } }); thread1.start(); thread2.start(); } }
为什么结果会有时出现j远大于i的状况?java
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 200/200 | 1/1 | 20/20 | 编写简单程序 |
第二周 | 350/550 | 1/2 | 30/30 | 用IDE调试修改程序 |
第三周 | 500/1050 | 1/3 | 35/85 | |
第四周 | 500/1550 | 1/4 | 30/115 | |
第五周 | 500/2050 | 1/5 | 30/145 | |
第六周 | 700/2750 | 2/7 | 30/175 |