一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

服務器之家 - 編程語言 - Java教程 - Java 線程池框架

Java 線程池框架

2020-08-04 15:44月光下的鳳尾竹 Java教程

本文主要介紹了Java 線程池框架的相關知識。具有很好的參考價值,下面跟著小編一起來看下吧

一、線程池結構圖

Java 線程池框架

二、示例

定義線程接口

?
1
2
3
4
5
6
public class MyThread extends Thread {
 @Override
 publicvoid run() {
 System.out.println(Thread.currentThread().getName() + "正在執(zhí)行");
 }
}

1:newSingleThreadExecutor

?
1
2
3
4
5
6
7
8
9
10
ExecutorService pool = Executors. newSingleThreadExecutor();
 Thread t1 = new MyThread();
 Thread t2 = new MyThread();
 Thread t3 = new MyThread();
 //將線程放入池中進行執(zhí)行
 pool.execute(t1);
 pool.execute(t2);
 pool.execute(t3);
 //關閉線程池
 pool.shutdown();

輸入結果:

?
1
2
3
pool-1-thread-1正在執(zhí)行
pool-1-thread-1正在執(zhí)行
pool-1-thread-1正在執(zhí)行

2:newFixedThreadPool

?
1
2
3
4
5
6
7
8
9
10
11
12
13
ExecutorService pool = Executors.newFixedThreadPool(3);
Thread t1 = new MyThread();
 Thread t2 = new MyThread();
 Thread t3 = new MyThread();
 Thread t4 = new MyThread();
 Thread t5 = new MyThread();
 //將線程放入池中進行執(zhí)行
 pool.execute(t1);
 pool.execute(t2);
 pool.execute(t3);
 pool.execute(t4);
 pool.execute(t5);
pool.shutdown();

輸入結果:

?
1
2
3
4
pool-1-thread-1正在執(zhí)行
pool-1-thread-2正在執(zhí)行
pool-1-thread-1正在執(zhí)行
pool-1-thread-2正在執(zhí)行

3 :newCachedThreadPool

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ExecutorService pool = Executors.newCachedThreadPool();
 Thread t1 = new MyThread();
 Thread t2 = new MyThread();
 Thread t3 = new MyThread();
 Thread t4 = new MyThread();
 Thread t5 = new MyThread();
 //將線程放入池中進行執(zhí)行
 pool.execute(t1);
 pool.execute(t2);
 pool.execute(t3);
 pool.execute(t4);
 pool.execute(t5);
 //關閉線程池
 pool.shutdown();

輸入結果:

?
1
2
3
4
5
pool-1-thread-2正在執(zhí)行
pool-1-thread-4正在執(zhí)行
pool-1-thread-3正在執(zhí)行
pool-1-thread-1正在執(zhí)行
pool-1-thread-5正在執(zhí)行

4 :ScheduledThreadPoolExecutor

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
pool.scheduleAtFixedRate(new Runnable() {//每隔一段時間就觸發(fā)異常
  @Override
  public void run() {
   //throw new RuntimeException();
   System.out.println("================");
  }
 }, 1000, 2000, TimeUnit.MILLISECONDS);
pool.scheduleAtFixedRate(new Runnable() {//每隔一段時間打印系統(tǒng)時間,證明兩者是互不影響的
  @Override
  public void run() {
   System.out.println("+++++++++++++++++");
  }
 }, 1000, 2000, TimeUnit.MILLISECONDS);

輸入結果:

?
1
2
3
4
================
+++++++++++++++++
+++++++++++++++++
+++++++++++++++++

三、線程池核心參數(shù)

corePoolSize : 池中核心的線程數(shù)

maximumPoolSize : 池中允許的最大線程數(shù)。

keepAliveTime : 當線程數(shù)大于核心時,此為終止前多余的空閑線程等待新任務的最長時間。

unit : keepAliveTime 參數(shù)的時間單位。

workQueue : 執(zhí)行前用于保持任務的隊列。此隊列僅保持由 execute方法提交的 Runnable任務。

threadFactory : 執(zhí)行程序創(chuàng)建新線程時使用的工廠。

handler : 由于超出線程范圍和隊列容量而使執(zhí)行被阻塞時所使用的處理程序。

ThreadPoolExecutor :Executors類的底層實現(xiàn)。

3.1 任務排隊機制

SynchonousQueue: 同步隊列,隊列直接提交給線程執(zhí)行而不保持它們,此時線程池通常是無界的

LinkedBlockingQueue: 無界對列,當線程池線程數(shù)達到最大數(shù)量時,新任務就會在隊列中等待執(zhí)行,可能會造成隊列無限膨脹

ArrayBlockingQueue : 有界隊列,有助于防止資源耗盡,一旦達到上限,可能會造成新任務丟失

注意:

newSingleThreadExecutor、newFixedThreadPool使用的是LinkedBlockingQueue

newCachedThreadPool 使用的是 SynchonousQueue

newScheduledThreadPool使用的是 DelayedWorkQueue

3.2 線程執(zhí)行流程

Java 線程池框架

3.3 線程大小確定:

cpu密集型: 盡量少開線程,最佳線程數(shù) Ncpu+1

io密集型:多開線程,2Ncpu

混合型:根據(jù)情況而定,可以拆分成io密集和cou密集

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!

原文鏈接:http://www.cnblogs.com/wanwusheng/p/6376311.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 任我行视频在线观看国语 | a人片| 催眠 迷j系列小说 | 鸥美毛片 | 深夜激情网 | 秋霞一级成人欧美理论 | 久青草国产在线观看视频 | 成年人视频在线免费看 | 欧美日韩高清不卡一区二区三区 | 蜜桃在线 | 色噜噜国产精品视频一区二区 | 视频一区在线免费观看 | 亚洲人成网站在线观看青青 | 99青青青精品视频在线 | 色在线影院 | 国产美女屁股直流白浆视频无遮挡 | 成人精品亚洲 | 12一14性水蜜桃 | 亚洲精品国产综合久久一线 | 古装一级无遮挡毛片免费观看 | 俄罗斯极品h在线 | 50度灰破解版v5.7.0 | 亚洲视频一区二区在线观看 | 99久久99热久久精品免费看 | 好吊色青青青国产综合在线观看 | 亚洲a区视频 | 欧美香蕉视频 | 女性性色生活片免费观看 | 午夜精品久久久久久久99蜜桃 | tobu8在线观看免费高清 | 国产91视频网 | gogort人体的最新网站 | 91免费精品国自产拍在线不卡 | 亚洲区精品久久一区二区三区 | 欧美一级xxx | 国产成人www免费人成看片 | 69堂最新地域网名 | 69福利区 | 五月婷婷俺也去开心 | 456成人免费高清视频 | yjzz视频 |