标题: 2个线程输出奇偶数为何会死锁?我没用锁呀,求大神科普 [打印本页] 作者: yuanxianzhi 时间: 2016-9-10 00:09 标题: 2个线程输出奇偶数为何会死锁?我没用锁呀,求大神科普 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;
}
}
}