1 package Test;
 2 
 3 public class Demo9 {
 4 public static void main(String[] args) {
 5     TestSync2 tt = new TestSync2();
 6         
 7     //
 8     new Thread(tt).start();
 9     new Thread(tt).start();
10     new Thread(tt).start();
11     new Thread(tt).start();
12 }
13 
14 }
15 class TestSync2 implements Runnable{
16     private int ticket = 20;
17     public void run(){
18         while(true){
19             if(ticket>0){
20                 try{
21                     Thread.sleep(1000);
22                 }catch(Exception e){}
23                 System.out.println(Thread.currentThread().getName()+" 出售票: "+ticket--);
24             }
25         }
26     }
27 }
View Code

相关文章: