猿问

多线程求解决下 谢谢大家!!!

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) {

}

当使用该过后的 回出现负数是怎么回事啊?

慕姐8946757
浏览 1419回答 3
3回答

慕的地8582982

ZVRUROKGVSVDMWHQTBKMJTQSWFBSPFBEBRZDRULVLURUIYOEHJFQQNXWZPZNQTWEBQXHWGXAVDFCLYNJSPMOLNEHXGJRNXZWLBXUDLURAOLTQZHENEVSPKTQAYAQNQTDHJAQXNPDGQGCLBRKGISCEVLAJFUJMOEHLPKHEWSBDGDTJKAYFIYOLOEVKADVXHXAWSVRAPCTJGJMIFBLBVFNQTXZVYVMFBSBDAQUXTWDSOLHXNJZDGLPVEOCPFGXHGWFIRPKHQMJQUKAFBRUWGPEOQBDAXYOEHUDGDAJNQMPSFVXAQGQZCSCNISCSVRHJNIZPXUYIZPLORHJYAQMKMPSJGPLHEBKAJMPRGCFWFVFAKHDFVLOYZCYVEEMWSOYWYHYHLUWMCTETWMVUKGWYJEOXGJJZWSBEVRNQAHQNJNDECXBYAJFVEPMDMJMQNIYBEAWFBMFARNXTJZBEORHXAJZDNUKNKNDAQGIGCYAAQGQNJHJLCYODFJMWFHRHLTWLUZCFCQASEHEUDZPTVSCHLAEOQGPLVYJACMWTDBWHQGKZWZCRTJTPSCZJMWGWYISWFIRPSQLNKNNKNWMQFIKSJWTIFCSNCMWSTWLHMJZPRMWUQMCZDAKMWNBDAXNXAQZWXXACLBFCENLBRBROXPPUJTWXSHKHXAJGWTFUKTEURTCTQQGPLISCKGKNQSCTDSWFCYBVXUFVSIYUKTLCEHLCTVLVEZBPSILCYHYUSVSQYCRIRILAJYUMIKUJTJGKZIQNDAPSVMVEAWMBJSWMOLHROKBRAWTNIZORBLUQBXUJMPSCMVRVEGWSBMVXNITQVSPYORPLGXAWLBSOROEGDGCQMCGDZBXNKLBVZCYBFBKAQKZKHDUPLCF
随时随地看视频慕课网APP

相关分类

Java
我要回答