直接编译执行即可,执行时候注意添加输入参数 true 或 falsepublic 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打印啊,为什么结果不同呢?
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打印啊,为什么结果不同呢?
把 if (b) 删除了
为什么设置b = true,则会每隔13秒循环输出j == 10:10呢?
boolean b = Boolean.parseBoolean(args[0]);
分别修改为
boolean b = true;
boolean b = false;
boolean b = Math.random() > 0.5d;然后编译执行,3个执行结果有的过程是怎样的???????????????????
说错了不好意思!
是b = false的时候,会输出:
j == 10:10
bbbbbbb
ccccccc
e1
j == 10:10
之后每隔13秒输出一个
j == 10:10
我的标题起的的确会引起歧义,真的不好意思
我只是想知道,b = true的时候输出的值跟b = false时输出的值是不同的,但是内部的逻辑是怎么走的我没明白
还有一个,如果设置b = Math.random() > 0.5d;这个输出的值为什么会跟b = true的值一样的呢?难道Math.random的值每次都会大于0.5么?
原来是加到临界点又加回去了,非常感谢!
是的,与机器有关。