線程池(2)Executors.newFixedThreadPool

來源:互聯網
上載者:User

標籤:for   imu   timeout   exce   except   fixed   stat   支援   link   

例子:

ExecutorService es = Executors.newFixedThreadPool(5);        try {            for (int i = 0; i < 20; i++) {                Runnable syncRunnable = new Runnable() {                    @Override                    public void run() {                        log.info(Thread.currentThread().getName());                        try {                            Thread.sleep(2000);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                };                es.execute(syncRunnable);            }        } finally {            es.shutdown();        }
運行結果:         *  10:20:14.630 pool-1-thread-5            10:20:14.630 pool-1-thread-1            10:20:14.632 pool-1-thread-4            10:20:14.630 pool-1-thread-2            10:20:14.630 pool-1-thread-3            10:20:16.635 pool-1-thread-4            10:20:16.635 pool-1-thread-5            10:20:16.635 pool-1-thread-1            10:20:16.636 pool-1-thread-2            10:20:16.637 pool-1-thread-3            10:20:18.637 pool-1-thread-3            10:20:18.638 pool-1-thread-4            10:20:18.641 pool-1-thread-5            10:20:18.641 pool-1-thread-1            10:20:18.642 pool-1-thread-2            10:20:20.638 pool-1-thread-3            10:20:20.639 pool-1-thread-4            10:20:20.641 pool-1-thread-5            10:20:20.642 pool-1-thread-1            10:20:20.642 pool-1-thread-2

調用的ThreadPoolExecutor:

public static ExecutorService newFixedThreadPool(int nThreads) {        return new ThreadPoolExecutor(nThreads, nThreads,                                      0L, TimeUnit.MILLISECONDS,                                      new LinkedBlockingQueue<Runnable>());    }

 

corePoolSize=maximumPoolSize=5

keepAliveTime=0

allowCoreThreadTimeout=false(預設)

因此,

  • 線程池中的線程數永遠是5,永久存活。
  • 對於新任務,當隊列未滿時,插入隊列;當隊列已滿時,預設執行AbortPolicy,即拋出異常。
  • 支援線程reuse

 

線程池(2)Executors.newFixedThreadPool

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.