posts - 38, comments - 2, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          (轉(zhuǎn))隊(duì)列和線程池簡單示例

          Posted on 2009-08-03 18:18 AntiquMan 閱讀(212) 評(píng)論(0)  編輯  收藏 所屬分類: Thread

          基本演示了線程池和隊(duì)列的應(yīng)用

            public class WorkQueue { 
             

             private final int nThreads;//線程池的大小 
             private final PoolWorker[] threads;//用數(shù)組實(shí)現(xiàn)線程池 
             private final LinkedList queue;//任務(wù)隊(duì)列 

            public WorkQueue(int nThreads){ 
               this.nThreads = nThreads; 
               queue = new LinkedList(); 
               threads = new PoolWorker[nThreads]; 

                for (int i=0; i<nThreads;i++){

                   threads[i] = new PoolWorker(); 
                   threads[i].start();//啟動(dòng)所有工作線程 
                } 
            } 

            public void execute(Runnable r) {//
          任務(wù) 
              synchronized(queue) { 
                      queue.addLast(r); 
                      queue.notify(); 
              } 
            } 

            private class PoolWorker extends Thread {//工作線程類 
                  public void run() { 
                         Runnable r; 
                         while (true) { 
                              synchronized(queue) { 
                                while (queue.isEmpty()) {//如果任務(wù)隊(duì)列中沒有任務(wù),等待 
                                  try{ 
                                    queue.wait(); 
                                  }catch (InterruptedException ignored){} 
                                }    
                                 r = (Runnable) queue.removeFirst();//有任務(wù)時(shí),取出任務(wù) 
                             } 
                             try { 
                                 r.run();//執(zhí)行任務(wù) 
                             }catch (RuntimeException e) { 
                                // You might want to log something here 
                            } 
                        } 
                } 
             } 


           public static void main(String args[]){ 
                WorkQueue wq=new WorkQueue(10);//10個(gè)工作線程 
                Mytask r[]=new Mytask[20];//20個(gè)任務(wù) 
             
                for(int i=0;i<20;i++){ 
                     r[i]=new Mytask(); 
                     wq.execute(r[i]); 
                }       
           } 

          class Mytask implements Runnable{//任務(wù)接口 
                   public void run(){ 
                        String name=Thread.currentThread().getName(); 
                        try{ 
                            Thread.sleep(100);//模擬任務(wù)執(zhí)行的時(shí)間 
                        }catch(InterruptedException e){} 
                        System.out.println(name+" executed OK"); 
                   } 
            } 


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 客服| 阿鲁科尔沁旗| 杭锦后旗| 盐津县| 田东县| 五大连池市| 北川| 福州市| 忻城县| 汉源县| 邯郸县| 蛟河市| 杨浦区| 嘉荫县| 忻州市| 镇巴县| 镇坪县| 永顺县| 永嘉县| 鸡泽县| 广灵县| 江津市| 松江区| 普定县| 太仆寺旗| 芦山县| 务川| 绥中县| 保靖县| 津南区| 东阿县| 读书| 东丰县| 舞阳县| 阳高县| 梅河口市| 东兰县| 平塘县| 宜君县| 清镇市| 保山市|