<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    新的起點 新的開始

    快樂生活 !

    深入淺出多線程(4)對CachedThreadPool OutOfMemoryError問題的一些想法

        接系列3,在該系列中我們一起探討一下CachedThreadPool。
        線程池是Conncurrent包提供給我們的一個重要的禮物。使得我們沒有必要維護自個實現的心里很沒底的線程池了。但如何充分利用好這些線程池來加快我們開發與測試效率呢?當然是知己知彼。本系列就說說對CachedThreadPool使用的一下問題。
        下面是對CachedThreadPool的一個測試,程序有問題嗎?
    package net.blogjava.vincent;

    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.FutureTask;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    public class CachedThreadPoolIssue {

        
    /**
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
            
            ExecutorService es 
    = Executors.newCachedThreadPool();
            
    for(int i = 1; i<8000; i++)
                es.submit(
    new task());

        }

    }
    class task implements Runnable{

        @Override
        
    public void run() {
        
    try {
            Thread.sleep(
    4000);
        } 
    catch (InterruptedException e) {
            
    // TODO Auto-generated catch block
            e.printStackTrace();
        }
            
        }
        
    }
    如果對JVM沒有特殊的設置,并在Window平臺上,那么就會有一下異常的發生:
    Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.addIfUnderMaximumPoolSize(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.execute(Unknown Source)
        at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
        at net.blogjava.vincent.CachedThreadPoolIssue.main(CachedThreadPoolIssue.java:19)
    看看Doc對該線程池的介紹:
    Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.

    有以下幾點需要注意:
    1. 指出會重用先前的線程,不錯。
    2. 提高了短Task的吞吐量。
    3. 線程如果60s沒有使用就會移除出Cache。

    好像跟剛才的錯誤沒有關系,其實就第一句話說了問題,它會按需要創建新的線程,上面的例子一下提交8000個Task,意味著該線程池就會創建8000線程,當然,這遠遠高于JVM限制了。
    注:在JDK1.5中,默認每個線程使用1M內存,8000M !!! 可能嗎??!

    所以我感覺這應該是我遇到的第一個Concurrent不足之處,既然這么設計,那么就應該在中Doc指出,應該在使用避免大量Task提交到給CachedThreadPool.
    可能讀者不相信,那么下面的例子說明了他創建的Thread。
    在ThreadPoolExecutor提供的API中,看到它提供beforeExecute 和afterExecute兩個可以在子類中重載的方法,該方法在線程池中線程執行Task之前與之后調用。所以我們在beforeExexute中查看目前線程編號就可以確定目前的線程數目.
    package net.blogjava.vincent;

    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.FutureTask;
    import java.util.concurrent.SynchronousQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    public class CachedThreadPoolIssue {

        
    /**
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
            
            ExecutorService es 
    = new LogThreadPoolExecutor(0, Integer.MAX_VALUE,
                    
    60L, TimeUnit.SECONDS,
                    
    new SynchronousQueue<Runnable>());
            
    for(int i = 1; i<8000; i++)
                es.submit(
    new task());

        }

    }
    class task implements Runnable{

        @Override
        
    public void run() {
        
    try {
            Thread.sleep(
    600000);
        } 
    catch (InterruptedException e) {
            
    // TODO Auto-generated catch block
            e.printStackTrace();
        }
            
        }
        
    }
    class LogThreadPoolExecutor extends ThreadPoolExecutor{

        
    public LogThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
                
    long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
            
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        }
        
    protected void beforeExecute(Thread t, Runnable r) { 
            System.out.println(t.getName());
        }
        
    protected void afterExecute(Runnable r, Throwable t) {
        }
        
    }
    測試結果如圖:

    當線程數達到5592是,只有在任務管理器Kill該進程了。

    如何解決該問題呢,其實在剛才實例化時就看出來了,只需將
    new LogThreadPoolExecutor(0, Integer.MAX_VALUE,
                    60L, TimeUnit.SECONDS,
                    new SynchronousQueue<Runnable>());
    Integer.MAX_VALUE 改為合適的大小。對于該參數的含義,涉及到線程池的實現,將會在下個系列中指出。
    當然,其他的解決方案就是控制Task的提交速率,避免超過其最大限制。

    posted on 2008-09-03 21:50 advincenting 閱讀(7910) 評論(2)  編輯  收藏

    評論

    # re: 深入淺出多線程(4)對CachedThreadPool OutOfMemoryError問題的一些想法 2008-09-04 08:59 dennis

    實際應用中我想用到CachedThreadPool的機會不多,通常情況下都推薦使用FixedThreadPool以規定線程數上限,也利于調優  回復  更多評論   

    # re: 深入淺出多線程(4)對CachedThreadPool OutOfMemoryError問題的一些想法 2008-09-29 11:38 niuren

    同意, CachedThreadPool內部使用的是SynchronousQueue, 感覺這個結構本身就決定了CachedThreadPool很慢。所以一般情況下還是用FixedThreadPool或自定義的ThreadPoolExecut  回復  更多評論   


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


    網站導航:
     

    公告

    Locations of visitors to this pageBlogJava
  • 首頁
  • 新隨筆
  • 聯系
  • 聚合
  • 管理
  • <2008年9月>
    31123456
    78910111213
    14151617181920
    21222324252627
    2829301234
    567891011

    統計

    常用鏈接

    留言簿(13)

    隨筆分類(71)

    隨筆檔案(179)

    文章檔案(13)

    新聞分類

    IT人的英語學習網站

    JAVA站點

    優秀個人博客鏈接

    官網學習站點

    生活工作站點

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲黄网站wwwwww| 国产精品亚洲美女久久久| 99久久精品国产亚洲| 国产黄在线观看免费观看不卡| 女性无套免费网站在线看| 亚洲欧洲日韩国产一区二区三区| 久久久久久曰本AV免费免费| 亚洲天堂男人天堂| 久久精品一区二区免费看| 亚洲av无码成h人动漫无遮挡| 西西人体免费视频| 亚洲欧洲免费视频| 中文字幕无码免费久久99| 亚洲一卡2卡三卡4卡无卡下载| 巨胸喷奶水视频www网免费| 亚洲精品无AMM毛片| 青青青国产色视频在线观看国产亚洲欧洲国产综合 | 亚洲AV无码一区二区三区在线观看 | 亚洲国产精品综合久久网络| 手机永久免费的AV在线电影网| 国产亚洲精aa成人网站| 免费精品一区二区三区第35| 亚洲理论在线观看| 午夜电影免费观看| 一进一出60分钟免费视频| 亚洲国产成人片在线观看| 曰曰鲁夜夜免费播放视频| 亚洲av无码专区青青草原| 国产国拍精品亚洲AV片| 99视频免费播放| 亚洲综合一区国产精品| 亚洲熟伦熟女新五十路熟妇| 性无码免费一区二区三区在线| 亚洲一区二区三区免费视频| 亚洲 自拍 另类小说综合图区| 久久这里只精品热免费99| 亚洲人成人网站18禁| 亚洲日本乱码在线观看| 一二三四在线播放免费观看中文版视频| 国产亚洲精品国产福利在线观看| 国产亚洲精品国产|