class ThreadTest01 implements Runnable { int num = 100; // 2、覆蓋接口中的run方法。。 @Override public void run() { while (true) { /* * 格式: synchronized(鎖對象){ //需要同步的代碼 } */ synchronized (Demo1.class) { if (num<=0) { break; } try { Thread.sleep(1); System.out.println( Thread.currentThread().getName() + "出售了" + (1000 - num + 1) + "張票,還剩余:" + (--num) + "張"); /* * this.notify(); this.wait(); */ } catch (InterruptedException e) { e.printStackTrace(); } }請問以上代碼塊 ?if (num<=0) {break;}為什么不能改成if (num>0) {}當使用該過后的 回出現負數是怎么回事啊?
3 回答

添加回答
舉報
0/150
提交
取消