欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java并發(fā)編程之Executors類詳解

 更新時(shí)間:2021年06月17日 15:55:57   作者:小志的博客  
今天給大家?guī)淼氖顷P(guān)于Java并發(fā)編程的相關(guān)知識(shí),文章圍繞著Java Executors類展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下

一、Executors的理解

  • Executors類屬于java.util.concurrent包;
  • 線程池的創(chuàng)建分為兩種方式:ThreadPoolExecutor 和 Executors;
  • Executors(靜態(tài)Executor工廠)用于創(chuàng)建線程池;
  • 工廠和工具方法Executor , ExecutorService , ScheduledExecutorService , ThreadFactory和Callable在此包中定義的類;

jdk1.8API中的解釋如下:

在這里插入圖片描述 

二、Executors類圖結(jié)構(gòu)

在這里插入圖片描述

三、Executors常用的方法

在這里插入圖片描述

  • public static ExecutorService newFixedThreadPool(int nThreads) 一種線程數(shù)量固定的線程池,當(dāng)線程處于空閑狀態(tài)時(shí),他們并不會(huì)被回收,除非線程池被關(guān)閉。當(dāng)所有的線程都處于活動(dòng)狀態(tài)時(shí),新的任務(wù)都會(huì)處于等待狀態(tài),直到有線程空閑出來。
  • public static ExecutorService newSingleThreadExecutor() 創(chuàng)建單個(gè)線程。它適用于需要保證順序地執(zhí)行各個(gè)任務(wù);并且在任意時(shí)間點(diǎn),不會(huì)有多個(gè)線程是活動(dòng)的應(yīng)用場(chǎng)景。
  • public static ExecutorService newCachedThreadPool() 創(chuàng)建一個(gè)根據(jù)需要?jiǎng)?chuàng)建新線程的線程池,但在可用時(shí)將重新使用以前構(gòu)造的線程, 如果沒有可用的線程,將創(chuàng)建一個(gè)新的線程并將其添加到該池中。 未使用六十秒的線程將被終止并從緩存中刪除。
  • public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 創(chuàng)建一個(gè)線程池,可以調(diào)度命令在給定的延遲之后運(yùn)行,或定期執(zhí)行, 支持執(zhí)行定時(shí)性或周期性任務(wù)。
  • public static ExecutorService newWorkStealingPool(int parallelism) 創(chuàng)建一個(gè)維護(hù)足夠的線程以支持給定的并行級(jí)別的線程池,并且可以使用多個(gè)隊(duì)列來減少爭(zhēng)用。 ( jdk1.8版本新增的方法 )

四、Executors類中常用方法示例

1、newFixedThreadPool方法示例

代碼

package com.xz.thread.executors;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description: 
 * @author: xz
 * @create: 2021-06-16 21:33
 */
public class Demo {
    public static void main(String[] args) {
        //創(chuàng)建數(shù)量固定的線程池,線程池?cái)?shù)量為3
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        for(int i=0;i<5;i++){
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                    try {
                        Thread.sleep(500);
                        System.out.println("睡眠一秒");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }
}

輸出結(jié)果如下圖

在這里插入圖片描述

結(jié)論:示例中創(chuàng)建了數(shù)量固定為3的線程,由輸出結(jié)果截圖可知,遍歷次數(shù)為5次,當(dāng)執(zhí)行一輪(3次)后,停頓一秒鐘,直到有線程空閑出來,才繼續(xù)第4次執(zhí)行。

2、newSingleThreadExecutor方法示例

代碼

package com.xz.thread.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description:
 * @author: xz
 * @create: 2021-06-15 22:33
 */
public class Demo {
    public static void main(String[] args) {
        //創(chuàng)建單個(gè)線程
        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
        for(int i=0;i<5;i++){
            singleThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000);
                        System.out.println("睡眠一秒");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }
}

輸出結(jié)果如下圖

在這里插入圖片描述

結(jié)論:示例中創(chuàng)建了創(chuàng)建單個(gè)線程,每執(zhí)行一次任務(wù)后,睡眠一秒,保證順序地執(zhí)行各個(gè)任務(wù)。

3、newCachedThreadPool方法 代碼

package com.xz.thread.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description:
 * @author: xz
 * @create: 2021-06-15 22:33
 */
public class Demo {
    public static void main(String[] args) {
        //創(chuàng)建帶有緩存功能的線程池
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for(int i=0;i<5;i++){
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000);
                        System.out.println("睡眠一秒");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }
}

輸出結(jié)果如下圖

在這里插入圖片描述

結(jié)論:示例中根據(jù)需要?jiǎng)?chuàng)建帶有緩存線程的線程池,并在可用時(shí)將重新使用以前構(gòu)造的線程。

4、newScheduledThreadPool方法示例

代碼

package com.xz.thread.executor;

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: xz
 * @create: 2021-06-15 22:33
 */
public class Demo {
    public static void main(String[] args) {
        //創(chuàng)建執(zhí)行周期性任務(wù)的線程池
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
        /**
         * schedule(Runnable command,long delay, TimeUnit unit)方法參數(shù)解析
         * command 表示執(zhí)行任務(wù)命令
         * delay 表示從現(xiàn)在開始延遲執(zhí)行的時(shí)間
         * unit  延時(shí)參數(shù)的時(shí)間單位
         */
        scheduledThreadPool.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("scheduledThreadPool:"+LocalDateTime.now());
            }
        },1L, TimeUnit.MINUTES);
        System.out.println("當(dāng)前時(shí)間:"+LocalDateTime.now());
    }
}

輸出結(jié)果如下圖

在這里插入圖片描述

結(jié)論:示例中創(chuàng)建執(zhí)行周期性或定時(shí)性任務(wù)的線程池,由輸出結(jié)果可知,設(shè)置的1分鐘后執(zhí)行任務(wù)已經(jīng)生效。

五、Executors創(chuàng)建線程池原理

1、無論是創(chuàng)建何種類型線程池(newFixedThreadPool、newSingleThreadExecutor、newCachedThreadPool等等),均會(huì)調(diào)用ThreadPoolExecutor構(gòu)造函數(shù)。

在這里插入圖片描述

在這里插入圖片描述
在這里插入圖片描述

2、 ThreadPoolExecutor構(gòu)造函數(shù)中的參數(shù)解析

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

到此這篇關(guān)于Java并發(fā)編程之Executors類詳解的文章就介紹到這了,更多相關(guān)Java Executors類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論