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

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

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

服務器之家 - 編程語言 - Java教程 - Java四種線程池的使用詳解

Java四種線程池的使用詳解

2020-12-23 13:30cuisuqiang Java教程

本篇文章主要介紹了Java四種線程池的使用詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

java通過executors提供四種線程池,分別為:

newcachedthreadpool創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。

newfixedthreadpool 創(chuàng)建一個定長線程池,可控制線程最大并發(fā)數,超出的線程會在隊列中等待。

newscheduledthreadpool 創(chuàng)建一個定長線程池,支持定時及周期性任務執(zhí)行。

newsinglethreadexecutor 創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務,保證所有任務按照指定順序(fifo, lifo, 優(yōu)先級)執(zhí)行。

(1) newcachedthreadpool

創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。示例代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package test;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
public class threadpoolexecutortest {
 public static void main(string[] args) {
 executorservice cachedthreadpool = executors.newcachedthreadpool();
 for (int i = 0; i < 10; i++) {
  final int index = i;
  try {
  thread.sleep(index * 1000);
  } catch (interruptedexception e) {
  e.printstacktrace();
  }
  cachedthreadpool.execute(new runnable() {
  public void run() {
   system.out.println(index);
  }
  });
 }
 }
}

線程池為無限大,當執(zhí)行第二個任務時第一個任務已經完成,會復用執(zhí)行第一個任務的線程,而不用每次新建線程。

(2) newfixedthreadpool

創(chuàng)建一個定長線程池,可控制線程最大并發(fā)數,超出的線程會在隊列中等待。示例代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package test;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
public class threadpoolexecutortest {
 public static void main(string[] args) {
 executorservice fixedthreadpool = executors.newfixedthreadpool(3);
 for (int i = 0; i < 10; i++) {
  final int index = i;
  fixedthreadpool.execute(new runnable() {
  public void run() {
   try {
   system.out.println(index);
   thread.sleep(2000);
   } catch (interruptedexception e) {
   e.printstacktrace();
   }
  }
  });
 }
 }
}

因為線程池大小為3,每個任務輸出index后sleep 2秒,所以每兩秒打印3個數字。

定長線程池的大小最好根據系統資源進行設置。如runtime.getruntime().availableprocessors()

(3)  newscheduledthreadpool

創(chuàng)建一個定長線程池,支持定時及周期性任務執(zhí)行。延遲執(zhí)行示例代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package test;
import java.util.concurrent.executors;
import java.util.concurrent.scheduledexecutorservice;
import java.util.concurrent.timeunit;
public class threadpoolexecutortest {
 public static void main(string[] args) {
 scheduledexecutorservice scheduledthreadpool = executors.newscheduledthreadpool(5);
 scheduledthreadpool.schedule(new runnable() {
  public void run() {
  system.out.println("delay 3 seconds");
  }
 }, 3, timeunit.seconds);
 }
}

表示延遲3秒執(zhí)行。

定期執(zhí)行示例代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package test;
import java.util.concurrent.executors;
import java.util.concurrent.scheduledexecutorservice;
import java.util.concurrent.timeunit;
public class threadpoolexecutortest {
 public static void main(string[] args) {
 scheduledexecutorservice scheduledthreadpool = executors.newscheduledthreadpool(5);
 scheduledthreadpool.scheduleatfixedrate(new runnable() {
  public void run() {
  system.out.println("delay 1 seconds, and excute every 3 seconds");
  }
 }, 1, 3, timeunit.seconds);
 }
}

表示延遲1秒后每3秒執(zhí)行一次。

(4) newsinglethreadexecutor

創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務,保證所有任務按照指定順序(fifo, lifo, 優(yōu)先級)執(zhí)行。示例代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package test;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
public class threadpoolexecutortest {
 public static void main(string[] args) {
 executorservice singlethreadexecutor = executors.newsinglethreadexecutor();
 for (int i = 0; i < 10; i++) {
  final int index = i;
  singlethreadexecutor.execute(new runnable() {
  public void run() {
   try {
   system.out.println(index);
   thread.sleep(2000);
   } catch (interruptedexception e) {
   e.printstacktrace();
   }
  }
  });
 }
 }
}

結果依次輸出,相當于順序執(zhí)行各個任務。

你可以使用jdk自帶的監(jiān)控工具來監(jiān)控我們創(chuàng)建的線程數量,運行一個不終止的線程,創(chuàng)建指定量的線程,來觀察:

工具目錄:c:\program files\java\jdk1.6.0_06\bin\jconsole.exe

運行程序做稍微修改:

?
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
package test;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
public class threadpoolexecutortest {
 public static void main(string[] args) {
 executorservice singlethreadexecutor = executors.newcachedthreadpool();
 for (int i = 0; i < 100; i++) {
  final int index = i;
  singlethreadexecutor.execute(new runnable() {
  public void run() {
   try {
   while(true) {
    system.out.println(index);
    thread.sleep(10 * 1000);
   }
   } catch (interruptedexception e) {
   e.printstacktrace();
   }
  }
  });
  try {
  thread.sleep(500);
  } catch (interruptedexception e) {
  e.printstacktrace();
  }
 }
 }
}

效果如下:

選擇我們運行的程序:

Java四種線程池的使用詳解

監(jiān)控運行狀態(tài)

Java四種線程池的使用詳解

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://cuisuqiang.iteye.com/blog/2019372

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩在线二区全免费 | 亚洲是图你懂的 | 欧美色影视 | 农村老妇1乱69系列小说 | 牧教师| 免费国产高清视频 | 俄罗斯freeⅹ性欧美 | 天天做天天爽天天谢 | 我的妹妹最近有点怪在线观看 | 无码人妻99久久密AV | 男女一级特黄a大片 | 国产成人免费片在线观看 | 免费欧美一级片 | 日本丰满大乳乳奶 | 奇米影视久久 | 久久综合给会久久狠狠狠 | 手机看片福利 | 成人欧美一区二区三区黑人 | 天天视频国产精品 | 2020年国产精品午夜福利在线观看 | 千金肉奴隶在线观看 | yy111111影院理论大片 | 亚洲精品成人456在线播放 | 牛人国产偷窥女洗浴在线观看 | 日日夜夜撸影院 | 日本免费一区二区三区 | 国产精品亚洲片在线va | 男gaygays免费网站多人 | 四虎在线精品观看免费 | 草莓丝瓜芭乐樱桃榴莲色多黄 | 91看片淫黄大片.在线天堂 | 暖暖中国免费观看高清完整版 | 国产精品怡红院在线观看 | 精品日本三级在线观看视频 | 好大夫在线个人空间 | 日本人护士免费xxxx视频 | 国产精品久久久精品视频 | 亚洲欧美日韩另类在线一 | darkside动漫在线观看 | 国产成人成人一区二区 | 男人操女人动图 |