java吧 关注:1,186,911贴子:12,593,581
  • 2回复贴,共1

诡异的结果

只看楼主收藏回复

有人知道是怎么回事吗
代码:
public static void main(String[] args) {
threadConflict();
}
private static int i = 1000000;
private static void threadConflict() {
Runnable r = () -> {
while (--i >= 0) {
if (i <= 3) {
System.out.println(Thread.currentThread().getName() + ", " + i);
}
}
};
Thread[] ts = new Thread[3];
for (int j = 0; j < ts.length; j++) {
ts[j] = new Thread(r);
ts[j].start();
}
}
结果:

这个13w是怎么来的 咋就进到输出语句了?


IP属地:上海1楼2020-05-27 02:21回复
    完全没道理啊


    IP属地:上海2楼2020-05-27 03:53
    回复
      线程安全问题 你多个线程共享一个变量


      IP属地:上海来自Android客户端3楼2020-05-27 08:12
      回复