public class NumberDemo {
static Object o1 = new Object();
static int i = 9990;
public static void main(String[] args) {
Deom d = new Deom();
JISHU ji = new JISHU(d);
OUSHU ou = new OUSHU(d);
Thread t1 = new Thread(ji);
Thread t2 = new Thread(ou);
t1.start();
t2.start();
}
}
class Deom {
int num = 10000;
boolean flag = false;
}
class JISHU implements Runnable {
private Deom d;
public JISHU(Deom d) {
this.d = d;
}
@Override
public void run() {
while (d.num > 0) {
if (d.flag) {
System.out.println("jishu--" + d.num);
d.num--;
d.flag = false;
}
}
}
}
class OUSHU implements Runnable {
private Deom d;
public OUSHU(Deom d) {
this.d = d;
}
@Override
public void run() {
while (d.num > 0) {
if (!d.flag) {
System.out.println("偶数--" + d.num);
d.num--;
d.flag = true;//
}
}
}
}
代码如上,输出到一部分就不输出了。。想了1个小时实在想不出来。不知道是死锁了还是其他怎么了。。。。求告知卡死的原理
|
|