62,614
社区成员
发帖
与我相关
我的任务
分享
class Test{
public static void main(String[] args){
TestThread t = new TestThread();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
class TestThread implements Runnable{
private int tickets = 20;
public void run(){
while(true){
sale();
}
}
public synchronized void sale(){
if(tickets > 0){
try{
Thread.sleep(10);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()
+ " is salling ticket " + tickets--);
}
}
}
public synchronized void sale(){
if(tickets > 0){
System.out.println(Thread.currentThread().getName()
+ " is salling ticket " + tickets--);
}
}