国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Java并發編程之Executor接口的使用

瀏覽:6日期:2022-08-10 10:00:48
一、Executor接口的理解 Executor屬于java.util.concurrent包下; Executor是任務執行機制的核心接口;二、Executor接口的類圖結構

Java并發編程之Executor接口的使用

由類圖結構可知:

ThreadPoolExecutor 繼承了AbstractExecutorService接口; AbstractExecutorService接口實現了ExecutorService接口; ExecutorService繼承了Executor接口; 因此以下部分主要講解ThreadPoolExecutor類。三、Executor接口中常用的方法

void execute(Runnable command) 在將來的某個時間執行給定的命令。 該命令可以在一個新線程,一個合并的線程中或在調用線程中執行,由Executor實現。

四、線程池的創建分為兩種方式(主要介紹通過ThreadPoolExecutor方式)

注:通過Executors類的方式創建線程池,參考lz此博文鏈接https://www.jb51.net/article/215163.htm

1.ThreadPoolExecutor類中的構造方法

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue,defaultHandler)

2、 ThreadPoolExecutor類中構造函數的參數解析

corePoolSize 核心線程最大數量,通俗點來講就是,線程池中常駐線程的最大數量 maximumPoolSize 線程池中運行最大線程數(包括核心線程和非核心線程) keepAliveTime線程池中空閑線程(僅適用于非核心線程)所能存活的最長時間 unit 存活時間單位,與keepAliveTime搭配使用 workQueue 存放任務的阻塞隊列 handler 線程池飽和策略

3、ThreadPoolExecutor類創建線程池示例

代碼

package com.xz.thread.executor;import java.util.concurrent.*;/** * @description: * @author: xz * @create: 2021-06-16 22:16 */public class Demo { public static void main(String[] args) {ThreadPoolExecutor pool = new ThreadPoolExecutor(3,3,1L, TimeUnit.MINUTES,new LinkedBlockingDeque<>());for(int i=1;i<=5;i++){ pool.execute(new Runnable() {@Overridepublic void run() { System.out.println(Thread.currentThread().getName()); try {Thread.sleep(1000);System.out.println('睡眠一秒鐘'); } catch (InterruptedException e) {e.printStackTrace(); }} });} }}

輸出結果如下圖

Java并發編程之Executor接口的使用

結論:無論是創建何種類型線程池(newFixedThreadPool、newSingleThreadExecutor、newCachedThreadPool等等),均會調用ThreadPoolExecutor構造函數。

Java并發編程之Executor接口的使用Java并發編程之Executor接口的使用

到此這篇關于Java并發編程之Executor接口的使用的文章就介紹到這了,更多相關Java Executor接口內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 炎陵县| 盐源县| 都匀市| 根河市| 望城县| 富顺县| 衡阳市| 仙桃市| 运城市| 兴安县| 太白县| 甘肃省| 民乐县| 昔阳县| 苏尼特左旗| 潞西市| 呼图壁县| 昌图县| 嵊泗县| 河曲县| 大庆市| 柳林县| 西藏| 共和县| 潼关县| 昔阳县| 大田县| 河曲县| 龙川县| 涿鹿县| 樟树市| 古丈县| 洪湖市| 莱西市| 永泰县| 都安| 湘潭市| 嘉兴市| 日喀则市| 新巴尔虎左旗| 鱼台县|