今天无聊想看下我电脑一秒钟能从0加到多少,就写了这个东西
public class TestMultiThread {
static volatile long index = 0;
static boolean flag = true;
public static void main(String[] args){
new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(1000);
System.out.println(index);
Thread.sleep(1000);
System.out.println(index);
Thread.sleep(1000);
System.out.println(index);
System.exit(0);
}catch (Exception i){
i.printStackTrace();
}
}
}).start();
execute();
}
public static void execute(){
while (true){
index++;
}
}
}```
输出:
181768468
363750863
546204407;
后来一想,这个flag变量可以写成final,都说基本类型变量写成static final类型的会提高效率(编译阶段就确定值),于是输出结果变成了。。。
132285800
264612735
396760510
what the f...
直接将while循环条件写成“true”也是一样的输出。
我知道一定是在运行时有一些我不知道的细节导致了这个现象,大家说说自己的看法?
红颜莎娜
杨__羊羊
FFIVE
相关分类