直接编译执行即可,执行时候注意添加输入参数 true 或 false
- Java code
public class a1 { public static void main(final String[] args) { Thread m = new Thread(new Runnable() { public void run() { int j = 1; while (true) { j++; boolean b = Boolean.parseBoolean(args[0]); if (b) { System.out.println(b); System.out.println(j); } if (j == 10) { try { System.out.println("j == 10:" + j); Thread.sleep(3000); } catch (InterruptedException e1) { System.out.println("e1"); } } } } }); m.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("e"); } System.out.println("bbbbbbb"); m.interrupt(); System.out.println("ccccccc"); }}
为什么b为true的时候跟b为false的时候结果不同呢?
b为true只是输出了sysout打印啊,为什么结果不同呢?
------解决方案--------------------
程序在b=false时,一直循环,每循环一次 j++计算一次,等到大约13秒后,4个字节的整数变成-1,(ff ff ff ff),再累加的话,就从j=0又开始了。 到j=10的时候,会有一次输出。
------解决方案--------------------
b=Math.random()>0.5d; 这时b是随机的,有时是true,有时是false. 与设置b=true不一样,(j没有全部输出,那些就是b=Math.random()>0.5d为false 时跳过了。你仔细看一下。