夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          1.實現(xiàn)Runnable接口
          package hasreturn;

          public class MyThread implements Runnable
          {   
              
          private String name;
              
              
          public MyThread(String name)
              
          {
                  
          this.name = name;
              }

              
              
          public void run()
              
          {
                  
          try
                  
          {
                      Thread.sleep(
          1000*3);
                  }

                  
          catch(Exception ex)
                  
          {}
                  System.out.println(
          this.name + "正在執(zhí)行..");
              }

          }

          2. 線程池
          package hasreturn;

          import java.util.concurrent.Executors;

          import java.util.concurrent.ExecutorService;

          public class ThreadPool
          {
              
              
          public static void main(String[] args)
              
          {
                  
          // 創(chuàng)建一個可重用固定線程數(shù)的線程池
                  ExecutorService pool = Executors.newFixedThreadPool(1);
                  
                  
          // 創(chuàng)建實現(xiàn)了Runnable接口對象,Thread對象當(dāng)然也實現(xiàn)了Runnable接口
                  
                  MyThread t1 
          = new MyThread("A");
                  MyThread t2 
          = new MyThread("B");
                  MyThread t3 
          = new MyThread("C");
                  MyThread t4 
          = new MyThread("D");
                  MyThread t5 
          = new MyThread("E");
                  
                  
          // 將線程放入池中進(jìn)行執(zhí)行
                  pool.execute(t1);
                  pool.execute(t2);
                  pool.execute(t3);
                  pool.execute(t4);
                  pool.execute(t5);
                  
                  
          // 關(guān)閉線程池
                  pool.shutdown();
                  
              }

          }


          posted on 2011-08-16 21:35 HUIKK 閱讀(317) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 盐池县| 乌兰察布市| 松桃| 湾仔区| 阜平县| 沂源县| 崇礼县| 广丰县| 玉树县| 东台市| 遵义市| 舒兰市| 临湘市| 毕节市| 南和县| 遵化市| 潞城市| 长岭县| 三门县| 榆社县| 和静县| 三台县| 墨竹工卡县| 嵩明县| 旺苍县| 库伦旗| 资讯 | 永川市| 英超| 金昌市| 潢川县| 静安区| 那曲县| 台山市| 大城县| 济阳县| 治县。| 托里县| 天祝| 建平县| 漳平市|