国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。

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

newscheduledthreadpool 創建一個定長線程池,支持定時及周期性任務執行。

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

(1) newcachedthreadpool

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

?
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);
  }
  });
 }
 }
}

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

(2) newfixedthreadpool

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

?
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

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

?
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秒執行。

定期執行示例代碼如下:

?
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秒執行一次。

(4) newsinglethreadexecutor

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

?
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();
   }
  }
  });
 }
 }
}

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

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

工具目錄: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四種線程池的使用詳解

監控運行狀態

Java四種線程池的使用詳解

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品久久中文字幕 | 国产一区二区三区在线视频 | 国产一区二 | 亚洲激情综合 | 成人在线 | 欧美3区 | 国产毛片精品 | 亚洲天堂一区 | 久久精品国产v日韩v亚洲 | 午夜tv | 在线观看五码 | 高清国产一区二区三区 | 男人天堂网址 | 欧美大逼| av国产精品 | 精品成人免费一区二区在线播放 | 国产韩国精品一区二区三区 | 欧美综合一区二区三区 | 欧美视频在线观看免费 | 草草影院ccyycom | 精品少妇一区二区三区在线播放 | 亚洲成人久久久 | 夜夜摸夜夜操 | 美女久久久久 | 国内精品一区二区 | 亚洲欧洲自拍 | 精品伊人久久 | 午夜在线电影 | 精品96久久久久久中文字幕无 | 免费成人在线视频网站 | 日日操视频| 免费在线黄色电影 | 综合精品久久久 | 国产一区二区亚洲 | 黄色高清网站 | 中文在线一区二区 | 亚洲成人观看 | 中文字幕一区日韩精品欧美 | 国产精品亚洲精品 | 欧美狠狠操 | 亚洲精品www久久久久久广东 |