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)

          搜索

          •  

          積分與排名

          • 積分 - 451362
          • 排名 - 119

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 金川县| 虞城县| 乐至县| 许昌市| 白河县| 扎兰屯市| 诸城市| 阆中市| 永昌县| 崇信县| 清水县| 乳山市| 襄城县| 宁武县| 额尔古纳市| 古交市| 神农架林区| 兴文县| 扎赉特旗| 永春县| 东丽区| 剑河县| 平谷区| 南澳县| 芒康县| 麻阳| 哈尔滨市| 光泽县| 大冶市| 精河县| 博客| 内丘县| 英吉沙县| 绥芬河市| 万州区| 安陆市| 石城县| 安泽县| 育儿| 永寿县| 施甸县|