posts - 39,  comments - 263,  trackbacks - 0

          線程池的體驗,這里是簡單的例子,不解釋了,只能意會不能言傳。

           

          //第一個類
          package thread;
          import java.util.LinkedList;


          public class ThreadPool {
            private LinkedList tasks = new LinkedList();

            public ThreadPool(int size) {
              for (int i = 0; i < size; i++) {
               System.out.println("new");
                Thread thread = new ThreadTask(this);
                thread.start();
              }
            }

            public void run(Runnable task) {
             System.out.println("run0");
              synchronized (tasks) {
                tasks.addLast(task);
                tasks.notify();
              }
            }

            public Runnable getNext() {
              Runnable returnVal = null;
              synchronized (tasks) {
                while (tasks.isEmpty()) {
                  try {
                   System.out.println("waiting");
                   Thread.sleep(1000);
                    tasks.wait();
                    System.out.println("waited");
                  } catch (InterruptedException ex) {
                    System.err.println("Interrupted");
                  }
                }
                returnVal = (Runnable) tasks.removeFirst();
              }
              return returnVal;
            }

            public static void main(String args[]) {
              final String message[] = { "zgw", "nake", "sunny", "piao" };
              ThreadPool pool = new ThreadPool(message.length / 1);
              for (int i = 0, n = message.length; i < n; i++) {
                final int innerI = i;
                Runnable  runner = new Runnable() {
                  public void run() {
                    for (int j = 0; j < 25; j++) {
                      System.out.println("j: " + j + ": " + message[innerI]+"   "+innerI);
                    }
                  }
                };
               
               pool.run(runner);
              }
            }
          }
          //---------------------

          第二個類
          package thread;
          import java.util.LinkedList;

          class ThreadTask extends Thread {
            private ThreadPool pool;

            public ThreadTask(ThreadPool thePool) {
              pool = thePool;
            }

            public void run() {
              while (true) {
               System.out.println("gogogo");
                // blocks until job
                Runnable job = pool.getNext();
                try {
                  job.run();
                } catch (Exception e) {

                  System.err.println("Job exception: " + e);
                }
              }
            }
          }

          posted on 2006-01-08 22:58 nake 閱讀(289) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2006年1月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(18)

          我參與的團隊

          隨筆檔案(39)

          收藏夾(1)

          搜索

          •  

          積分與排名

          • 積分 - 451355
          • 排名 - 119

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 凌源市| 四会市| 龙海市| 七台河市| 克什克腾旗| 永清县| 安阳县| 丰城市| 洛阳市| 防城港市| 正镶白旗| 扎鲁特旗| 依安县| 达尔| 通海县| 北海市| 武宁县| 巫山县| 武川县| 嘉义县| 莲花县| 哈巴河县| 惠安县| 梁山县| 惠来县| 那曲县| 荥经县| 通河县| 汤阴县| 石门县| 治多县| 泰顺县| 伊通| 阜平县| 杨浦区| 五莲县| 射阳县| 徐州市| 塔河县| 岚皋县| 雷波县|