67,513
社区成员
发帖
与我相关
我的任务
分享
private int num=100;
private int i=0;
private synchronized void sale(){
if(num>0){
num--;
i++;
System.out.println(Thread.currentThread().getName()+"卖出了第"+i+"张票,还剩余"+num+"票!");
try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
@Override
public void run() {
while(num>0){
sale();
}
}
@Test
public void test() {
Seller st = new Seller();
for(int i=1; i<=5; i++) {
new Thread(st, "售票点" + i).start();
}
}
public static void main(String[] args){
Seller st = new Seller();
for(int i=1; i<=5; i++) {
new Thread(st, "售票点" + i).start();
}
}
以下是junit运行结果,运行结果明显不正确