class Ticket implements Runnable{
private int count = 5;
@Override
public void run(){
for(int i=1; i<=100; i++){
sale();
}
}
private synchronized void sale(){
if(count >0){
try{
Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("票有" + count--);
}
}
}
public class T{
public static void main(String[] args){
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
t1.start();
t2.start();
t3.start();
}
}
线程同步
最新推荐文章于 2025-04-21 07:00:00 发布