Open source are the greatest wealth---WANGPENG
          posts - 46, comments - 11, trackbacks - 0, articles - 0
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          [轉(zhuǎn)載]多線程售票代碼

          Posted on 2009-07-18 06:53 WANGPENG 閱讀(184) 評論(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 
          主站蜘蛛池模板: 和顺县| 霍山县| 四会市| 蓝田县| 新龙县| 盐山县| 沅江市| 镇安县| 武乡县| 南召县| 朔州市| 蚌埠市| 丹棱县| 中江县| 剑河县| 灵山县| 资中县| 化州市| 通河县| 兴化市| 马公市| 密云县| 武宣县| 鱼台县| 公主岭市| 武邑县| 西乡县| 分宜县| 保定市| 垫江县| 阿合奇县| 赫章县| 兴化市| 十堰市| 两当县| 盘山县| 邹城市| 北流市| 怀化市| 治县。| 罗平县|