Open source are the greatest wealth---WANGPENG
posts - 46, comments - 11, trackbacks - 0, articles - 0
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

[转载]多线程售票代码

Posted on 2009-07-18 06:53 WANGPENG 阅读(165) 评论(0)  编辑  收藏 所属分类: Java

 

 1 package com.ztejc.wp ;
 2 

 3 public class SellTicket implements Runnable {
 4     private static int    ticketNum    = 100
 ;
 5 
    
 6     public void
 run() {
 7         while (ticketNum > 1
) {
 8             synchronized (this
) {
 9                 ticketNum--
 ;
10 
            }
11             System.out.println(Thread.currentThread().getName() + " : " +
 ticketNum) ;
12 
        }
13 
    }
14 
    
15     public static void
 main(String[] args) {
16         SellTicket st = new
 SellTicket() ;
17 
        
18         Thread thread1 = new Thread(st , "线程1"
) ;
19         Thread thread2 = new Thread(st , "线程2"
) ;
20         Thread thread3 = new Thread(st , "线程3"
) ;
21 
        
22 
        thread1.start() ;
23 
        thread2.start() ;
24 
        thread3.start() ;
25 
    }
26 
}
27 

只有注册用户登录后才能发表评论。


网站导航: