激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java ThreadPoolExecutor的參數深入理解

Java ThreadPoolExecutor的參數深入理解

2020-08-31 14:44Lubby Java教程

這篇文章主要介紹了Java ThreadPoolExecutor的參數深入理解的相關資料,需要的朋友可以參考下

Java ThreadPoolExecutor參數深入理解

一、使用Executors創建線程池   

        之前創建線程的時候都是用的Executors的newFixedThreadPool(),newSingleThreadExecutor(),newCachedThreadPool()這三個方法。當然Executors也是用不同的參數去new ThreadPoolExecutor

    1. newFixedThreadPool()

    創建線程數固定大小的線程池。 由于使用了LinkedBlockingQueue所以maximumPoolSize 沒用,當corePoolSize滿了之后就加入到LinkedBlockingQueue隊列中。每當某個線程執行完成之后就從LinkedBlockingQueue隊列中取一個。所以這個是創建固定大小的線程池。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public static ExecutorService newFixedThreadPool(int nThreads) {
   return new ThreadPoolExecutor(nThreads, nThreads,
                  0L, TimeUnit.MILLISECONDS,
                  new LinkedBlockingQueue<Runnable>());
 }
public ThreadPoolExecutor(int corePoolSize,
              int maximumPoolSize,
              long keepAliveTime,
              TimeUnit unit,
              BlockingQueue<Runnable> workQueue) {
  this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
      Executors.defaultThreadFactory(), defaultHandler);
 }

   2.newSingleThreadPool()

    創建線程數為1的線程池,由于使用了LinkedBlockingQueue所以maximumPoolSize 沒用,corePoolSize為1表示線程數大小為1,滿了就放入隊列中,執行完了就從隊列取一個。

?
1
2
3
4
5
6
public static ExecutorService newSingleThreadExecutor() {
   return new FinalizableDelegatedExecutorService
     (new ThreadPoolExecutor(1, 1,
                 0L, TimeUnit.MILLISECONDS,
                 new LinkedBlockingQueue<Runnable>()));
 }

    3.newCachedThreadPool()

    創建可緩沖的線程池。沒有大小限制。由于corePoolSize為0所以任務會放入SynchronousQueue隊列中,SynchronousQueue只能存放大小為1,所以會立刻新起線程,由于maxumumPoolSize為Integer.MAX_VALUE所以可以認為大小為2147483647。受內存大小限制。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                   60L, TimeUnit.SECONDS,
                   new SynchronousQueue<Runnable>());
  }
public ThreadPoolExecutor(int corePoolSize,
             int maximumPoolSize,
             long keepAliveTime,
             TimeUnit unit,
             BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
       Executors.defaultThreadFactory(), defaultHandler);
  }
 

二、使用ThreadPoolExecutor創建線程池

ThreadPoolExecutor的構造函數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public ThreadPoolExecutor(int corePoolSize,
              int maximumPoolSize,
              long keepAliveTime,
              TimeUnit unit,
              BlockingQueue<Runnable> workQueue,
              ThreadFactory threadFactory,
              RejectedExecutionHandler handler) {
   if (corePoolSize < 0 ||
     maximumPoolSize <= 0 ||
     maximumPoolSize < corePoolSize ||
     keepAliveTime < 0)
     throw new IllegalArgumentException();
   if (workQueue == null || threadFactory == null || handler == null)
     throw new NullPointerException();
   this.corePoolSize = corePoolSize;
   this.maximumPoolSize = maximumPoolSize;
   this.workQueue = workQueue;
   this.keepAliveTime = unit.toNanos(keepAliveTime);
   this.threadFactory = threadFactory;
   this.handler = handler;
 }

參數:

        1、corePoolSize核心線程數大小,當線程數<corePoolSize ,會創建線程執行runnable

        2、maximumPoolSize 最大線程數, 當線程數 >= corePoolSize的時候,會把runnable放入workQueue中

        3、keepAliveTime  保持存活時間,當線程數大于corePoolSize的空閑線程能保持的最大時間。

        4、unit 時間單位

        5、workQueue 保存任務的阻塞隊列

        6、threadFactory 創建線程的工廠

        7、handler 拒絕策略

任務執行順序:

        1、當線程數小于corePoolSize時,創建線程執行任務。

        2、當線程數大于等于corePoolSize并且workQueue沒有滿時,放入workQueue中

        3、線程數大于等于corePoolSize并且當workQueue滿時,新任務新建線程運行,線程總數要小于maximumPoolSize

        4、當線程總數等于maximumPoolSize并且workQueue滿了的時候執行handler的rejectedExecution。也就是拒絕策略。

ThreadPoolExecutor默認有四個拒絕策略:

        1、ThreadPoolExecutor.AbortPolicy()   直接拋出異常RejectedExecutionException

        2、ThreadPoolExecutor.CallerRunsPolicy()    直接調用run方法并且阻塞執行

        3、ThreadPoolExecutor.DiscardPolicy()   直接丟棄后來的任務

        4、ThreadPoolExecutor.DiscardOldestPolicy()  丟棄在隊列中隊首的任務

當然可以自己繼承RejectedExecutionHandler來寫拒絕策略.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int corePoolSize = 1;
 int maximumPoolSize = 2;
 int keepAliveTime = 10;
// BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
 BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(5);
 ThreadFactory threadFactory = Executors.defaultThreadFactory();
 //線程池和隊列滿了之后的處理方式
 //1.跑出異常
 RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
 RejectedExecutionHandler handler2 = new ThreadPoolExecutor.CallerRunsPolicy();
 RejectedExecutionHandler handler3 = new ThreadPoolExecutor.DiscardPolicy();
 RejectedExecutionHandler handler4 = new ThreadPoolExecutor.DiscardOldestPolicy();
 
 
 ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, handler2);
 
 
 for (int j = 1; j < 15; j++) {
  threadPoolExecutor.execute(new Runnable() {
  
  public void run() {
   
   try {
   System.out.println(Thread.currentThread().getName());
   TimeUnit.SECONDS.sleep(1);
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
   
   
  }
  });
 }
 
 System.out.println(threadPoolExecutor);
 
 }

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:https://my.oschina.net/u/2250599/blog/498787

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 日韩精品中文字幕在线观看 | 欧美性受ⅹ╳╳╳黑人a性爽 | 国产成人自拍视频在线观看 | 一级成人欧美一区在线观看 | 日韩av在线资源 | 日本娇小18xxxⅹhd | 成人偷拍片视频在线观看 | 国产二三区 | 亚洲精品一区二区三区免 | av手机免费在线观看 | 91短视频在线视频 | 久久精品国产一区二区电影 | 久草成人在线观看 | 涩涩伊人 | 欧美黄成人免费网站大全 | 国产免费一区二区三区在线能观看 | 欧美特一级片 | 国产小视频一区 | 久久久久久久久久久国产精品 | 国产精品中文在线 | 久久成人视屏 | 国产xxxx免费 | 一区二区三区欧美在线 | 免费看成年人视频在线 | 欧美精品久久久久久久多人混战 | 国产三级国产精品国产普男人 | 欧美雌雄另类xxxxx | 久久精品国产清自在天天线 | 网站激情 | 私库av在线免费观看 | 一区在线免费视频 | 国产精品免费成人 | 最新国产毛片 | 久久国产91 | 骚av在线 | 国产乱淫a∨片免费视频 | 夜夜看| 黄色一级毛片免费看 | av在线免费看片 | 国产系列 视频二区 | 免费a级网站 |