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

Java創(chuàng)建線程池的幾種方式代碼示例

 更新時(shí)間:2025年01月26日 08:29:54   作者:動(dòng)物園首領(lǐng)  
這篇文章主要介紹了Java中創(chuàng)建線程池的四種方式,包括使用Executors類、ThreadPoolExecutor類、Future和Callable接口以及Spring的ThreadPoolTaskExecutor,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

一、創(chuàng)建線程池四種方式

  • 使用 Executors 類,Executors 類是 Java 中用于創(chuàng)建線程池的工廠類,它提供了多種靜態(tài)方法來創(chuàng)建不同類型的線程池
  • 使用 ThreadPoolExecutor 類,ThreadPoolExecutor 是 Java 中線程池的一個(gè)核心類,它提供了更細(xì)粒度的控制來創(chuàng)建和管理線程池
  • 使用 Future 和 Callable,F(xiàn)uture 和 Callable 是并發(fā)編程中非常重要的兩個(gè)接口,它們通常與 ExecutorService 一起使用來執(zhí)行異步任務(wù)。
  • 使用 Spring 的 ThreadPooltaskExecutor,ThreadPoolTaskExecutor 是一個(gè)基于 java.util.concurrent.ThreadPoolExecutor 的擴(kuò)展,提供了更豐富的配置選項(xiàng)和與Spring集成的特性

二、線程池重要參數(shù)

  • corePoolSize (int): 線程池的基本大小,即在沒有任務(wù)執(zhí)行時(shí)線程池的大小。當(dāng)新任務(wù)提交時(shí),線程池會(huì)優(yōu)先使用已有的空閑線程。
  • maximumPoolSize (int): 線程池能夠容納同時(shí)執(zhí)行的最大線程數(shù)。這個(gè)參數(shù)用于控制線程池的最大規(guī)模,防止因任務(wù)過多而導(dǎo)致資源耗盡。
  • keepAliveTime (long): 當(dāng)線程池中的線程數(shù)量超過 corePoolSize 時(shí),多余的空閑線程能等待新任務(wù)的最長(zhǎng)時(shí)間。超過這個(gè)時(shí)間后,多余的線程將被終止。
  • unit (TimeUnit): keepAliveTime 參數(shù)的時(shí)間單位,常見的時(shí)間單位有 TimeUnit.SECONDS、TimeUnit.MINUTES 等。
  • workQueue (BlockingQueue): 一個(gè)阻塞隊(duì)列,用于存儲(chǔ)等待執(zhí)行的任務(wù)。常用的阻塞隊(duì)列有 LinkedBlockingQueue、ArrayBlockingQueue 和 SynchronousQueue 等。
  • threadFactory (ThreadFactory): 用于創(chuàng)建新線程的工廠??梢酝ㄟ^實(shí)現(xiàn) ThreadFactory 接口來自定義線程的創(chuàng)建過程。
  • handler (RejectedExecutionHandler): 當(dāng)任務(wù)太多而線程池?zé)o法處理時(shí),用于定義拒絕任務(wù)的策略。常見的拒絕策略有 ThreadPoolExecutor.AbortPolicy、ThreadPoolExecutor.CallerRunsPolicy 和 ThreadPoolExecutor.DiscardPolicy 等。
package com.demo.threadPool;

import java.util.concurrent.*;

public class MainDemo1 {
    public static void main(String[] args) {
        int corePoolSize = 5; // 核心線程數(shù)
        int maximumPoolSize = 10; // 最大線程數(shù)
        long keepAliveTime = 1; // 非核心線程空閑存活時(shí)間
        /**
         * 存活時(shí)間單位
         * TimeUnit.DAYS:天
         * TimeUnit.HOURS:小時(shí)
         * TimeUnit.MINUTES:分
         * TimeUnit.SECONDS:秒
         * TimeUnit.MILLISECONDS:毫秒
         * TimeUnit.MICROSECONDS:微妙
         * TimeUnit.NANOSECONDS:納秒
         */
        TimeUnit unit = TimeUnit.MINUTES;
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(); // 工作隊(duì)列
        ThreadFactory threadFactory = Executors.defaultThreadFactory(); // 線程工廠
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy(); // 拒絕策略
        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,threadFactory,handler);
    }
}

三、線程池5種狀態(tài)

  • RUNNING:正常運(yùn)行狀態(tài),可接收新任務(wù),可處理阻塞隊(duì)列中的任務(wù)
  • SHUTDOWN:不會(huì)接收新任務(wù),但會(huì)處理阻塞隊(duì)列剩余任務(wù)
  • STOP:會(huì)中斷正在執(zhí)行的任務(wù),并拋棄阻塞隊(duì)列任務(wù)
  • TIDYING:任務(wù)全執(zhí)行完畢,活動(dòng)線程為 0,即將進(jìn)入終結(jié)
  • TERMINATED:終結(jié)狀態(tài)

四、Executors 類創(chuàng)建線程池

  • new newCachedThreadPool():創(chuàng)建一個(gè)可緩存線程池,如果線程池長(zhǎng)度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。線程池的規(guī)模不存在限制。(數(shù)量不固定的線程池)
  • new newFixedThreadPool():創(chuàng)建一個(gè)固定長(zhǎng)度線程池,可控制線程最大并發(fā)數(shù),超出的線程會(huì)在隊(duì)列中等待。(固定數(shù)量的線程池)
  • new newScheduledThreadPool():創(chuàng)建一個(gè)固定長(zhǎng)度線程池,支持定時(shí)及周期性任務(wù)執(zhí)行。(定時(shí)線程池)
  • new newSingleThreadExecutor():創(chuàng)建一個(gè)單線程化的線程池,它只會(huì)用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行。(單線程的線程池)
  • 固定線程池創(chuàng)建 ( Executors.newFixedThreadPool(5) ):創(chuàng)建一個(gè)固定大小的線程池。線程池中的線程數(shù)量是固定的,即使有些線程處于空閑狀態(tài),它們也不會(huì)被回收。
package com.demo.threadPool;

import java.util.List;
import java.util.concurrent.*;

public class MainThreadPool {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //初始化固定大小線程池
        ExecutorService executor1 = Executors.newFixedThreadPool(5);

        //使用 execute(Runnable command) 方法提交一個(gè)不需要返回結(jié)果的任務(wù),
        // 或者使用submit(Callable<T> task) 方法提交一個(gè)需要返回結(jié)果的任務(wù)。
        for (int i = 0; i < 10; i++) {
            executor1.execute(new TaskR(i));
        }

        //使用 submit(Callable<T> task) 任務(wù)并獲取 Future
        //使用 Future.get() 方法等待任務(wù)完成并獲取結(jié)果。這個(gè)方法會(huì)阻塞調(diào)用線程直到任務(wù)完成。
        for (int i = 0; i < 10; i++) {
            Future<String> future =  executor1.submit(new TaskC(i));
            System.out.println("線程返回結(jié)果  "+future.get());
        }
        // 當(dāng)所有任務(wù)都執(zhí)行完畢,或者需要關(guān)閉線程池時(shí),調(diào)用 shutdown() 方法。
        // 這將等待正在執(zhí)行的任務(wù)完成,但不接收新任務(wù)。
        executor1.shutdown();

        //使用 shutdownNow() 方法嘗試立即停止所有正在執(zhí)行的任務(wù),并返回等待執(zhí)行的任務(wù)列表
        List<Runnable> notExecutedTasks = executor1.shutdownNow();
        for(Runnable ls : notExecutedTasks){
            System.out.println(ls);
        }

        //使用 awaitTermination() 方法等待線程池關(guān)閉,直到所有任務(wù)完成或超時(shí)。
        boolean res = executor1.awaitTermination(60, TimeUnit.SECONDS);
        System.out.println("執(zhí)行結(jié)果:"+res);
    }
}

/**
 * 實(shí)現(xiàn) Runnable 接口
 */
class TaskR implements Runnable {
    private int id;
    public TaskR(int id) {
        this.id = id;
    }
    public void run() {
        System.out.println("TaskR " + id + " is running...");
    }
}

/**
 * 實(shí)現(xiàn) Callable 接口
 * 有返回值
 */
class TaskC implements Callable {
    private int id;
    public TaskC(int id) {
        this.id = id;
    }
    @Override
    public Object call(){
        System.out.println("TaskC " + id + " is running...");
        return id+"--TaskC";
    }
}
  • 單線程池 (newSingleThreadExecutor):創(chuàng)建一個(gè)只有一個(gè)線程的線程池。即使有多個(gè)任務(wù)提交,它們也會(huì)被排隊(duì),逐個(gè)由單個(gè)線程執(zhí)行
package com.demo.threadPool;

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

/**
 * 單線程池 (newSingleThreadExecutor):
 * 創(chuàng)建一個(gè)只有一個(gè)線程的線程池。即使有多個(gè)任務(wù)提交,它們也會(huì)被排隊(duì),逐個(gè)由單個(gè)線程執(zhí)行。
 */
public class MainOne {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        /**
         * 單線程:創(chuàng)建的執(zhí)行服務(wù)內(nèi)部有一個(gè)線程。所有提交給它的任務(wù)將會(huì)序列化執(zhí)行,也就是說,它會(huì)在單個(gè)線程上依次執(zhí)行任務(wù),不會(huì)有并發(fā)執(zhí)行的情況發(fā)生
         * 任務(wù)隊(duì)列:如果有多個(gè)任務(wù)提交給這個(gè)執(zhí)行器,除了當(dāng)前正在執(zhí)行的任務(wù)外,其他任務(wù)將會(huì)在一個(gè)無界隊(duì)列中等待,直到線程可用
         * 處理任務(wù)失?。喝绻麍?zhí)行中的線程由于任務(wù)拋出異常而終止,執(zhí)行服務(wù)會(huì)安排一個(gè)新的線程來替換它,以繼續(xù)執(zhí)行后續(xù)的任務(wù)
         * 使用場(chǎng)景: newSingleThreadExecutor 非常適合需要順序執(zhí)行的任務(wù),并且要求任務(wù)之間不受并發(fā)問題影響的場(chǎng)景
         */
        ExecutorService executor = Executors.newSingleThreadExecutor();

        for (int i = 0; i < 10; i++) {
            executor.execute(new TaskR(i));
        }

        //使用 submit(Callable<T> task) 任務(wù)并獲取 Future
        //使用 Future.get() 方法等待任務(wù)完成并獲取結(jié)果。這個(gè)方法會(huì)阻塞調(diào)用線程直到任務(wù)完成。
        for (int i = 0; i < 10; i++) {
            Future<String> future =  executor.submit(new TaskC(i));
            System.out.println("線程返回結(jié)果  "+future.get());
        }
        // 當(dāng)所有任務(wù)都執(zhí)行完畢,或者需要關(guān)閉線程池時(shí),調(diào)用 shutdown() 方法。
        // 這將等待正在執(zhí)行的任務(wù)完成,但不接收新任務(wù)。
        executor.shutdown();
    }
}
  • 緩存線程池 (newCachedThreadPool):創(chuàng)建一個(gè)可根據(jù)需要?jiǎng)?chuàng)建新線程的線程池。如果線程空閑超過60秒,它們將被終止并從池中移除
package com.demo.threadPool;

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

/**
 * 緩存線程池 (newCachedThreadPool):
 * 創(chuàng)建一個(gè)可根據(jù)需要?jiǎng)?chuàng)建新線程的線程池。如果線程空閑超過60秒,它們將被終止并從池中移除
 */
public class MainCacheThreadPool {
    public static void main(String[] args) throws InterruptedException {

        System.out.println(Thread.currentThread().getName() + "線程: Start at: " + new Date());
        //初始化緩存線程池
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 1; i < 10; i++) {
            System.out.println("添加了第" + i + "個(gè)任務(wù)類");
            Thread.sleep(2000);
            exec.execute(new TaskR(i));
        }
        //所有任務(wù)結(jié)束后關(guān)閉線程池
        exec.shutdown();
        System.out.println(Thread.currentThread().getName() + " 線程: Finished all threads at:" + new Date());

    }
}
  • 調(diào)度線程池 (newScheduledThreadPool):創(chuàng)建一個(gè)支持定時(shí)任務(wù)和周期性任務(wù)的線程池
package com.demo.threadPool;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 固定頻率執(zhí)行
 * 調(diào)度線程池 (newScheduledThreadPool):
 * 創(chuàng)建一個(gè)支持定時(shí)任務(wù)和周期性任務(wù)的線程池
 */
public class MainScheduledThreadPool {
    public static void main(String[] args) {
        /**
         * 場(chǎng)景描述
         * 假設(shè)你需要一個(gè)應(yīng)用程序,該程序能夠每10秒執(zhí)行一次任務(wù),并在啟動(dòng)后1分鐘開始執(zhí)行。此外,
         * 你還需要能夠安排一次性任務(wù)在未來的某個(gè)時(shí)間點(diǎn)執(zhí)行
         */
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(10);

        // 安排定期任務(wù)
        // 初始延遲1分鐘,之后每10秒執(zhí)行一次
        threadPool.scheduleAtFixedRate(new TaskR(2), 60, 10, TimeUnit.SECONDS);

        // 安排一次性任務(wù)
        // 使用 schedule 方法安排一個(gè)任務(wù),在指定的延遲后執(zhí)行一次
        // 延遲5分鐘后執(zhí)行
        threadPool.schedule(new TaskR(3), 5, TimeUnit.MINUTES);

        // 關(guān)閉線程池
        // 當(dāng)不再需要線程池時(shí),調(diào)用 shutdown 方法來關(guān)閉線程池。這將等待正在執(zhí)行的任務(wù)完成,但不接收新任務(wù)
        threadPool.shutdown();

        // 等待線程池關(guān)閉
        // 使用 awaitTermination 方法等待線程池關(guān)閉,直到所有任務(wù)完成或超時(shí)。
        try {
            threadPool.awaitTermination(1, TimeUnit.HOURS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
  • 使用給定的線程工廠創(chuàng)建線程池:可以提供一個(gè)自定義的 ThreadFactory 來創(chuàng)建線程池中的線程
package com.demo.threadPool;


import java.util.concurrent.*;

/**
 * 使用給定的線程工廠創(chuàng)建線程池
 */
public class MainFactory {
    public static void main(String[] args) {
        //自定義線程工廠創(chuàng)建
        ThreadFactory threadFactory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r);
            }
        };
        //使用給定的線程工廠創(chuàng)建線程池
        ExecutorService executor = Executors.newFixedThreadPool(5, threadFactory);
        executor.execute(new TaskR(2));
    }
}
  • 自定義線程工廠創(chuàng)建:自定義線程工廠可以設(shè)置自己的線程名,設(shè)置守護(hù)線程,設(shè)置線程優(yōu)先級(jí),處理未捕獲的異常等
package com.demo.threadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
 *  自定義線程工廠:設(shè)置線程名,守護(hù)線程,優(yōu)先級(jí)以及UncaughtExceptionHandler
 */
public class MainFactory implements ThreadFactory {

    private final ThreadGroup group;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;
    public MainFactory(String namePrefix) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
        this.namePrefix = namePrefix + "-thread-";
    }

    public MainFactory(ThreadGroup group, String namePrefix) {
        this.group = group;
        this.namePrefix = namePrefix;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(group, r,namePrefix + threadNumber.getAndIncrement(),0);
        //守護(hù)線程
        if (t.isDaemon())
            t.setDaemon(true);
        //線程優(yōu)先級(jí)
        if (t.getPriority() != Thread.NORM_PRIORITY)
            t.setPriority(Thread.NORM_PRIORITY);
        /**
         * 處理未捕捉的異常
         */
        t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("處理未捕獲的異常");
            }
        });
        return t;
    }

    //測(cè)試方法
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(5, new MainFactory("測(cè)試線程"));
        for (int i = 0; i < 10; i++) {
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("線程處理");
                    //未捕獲的異常,走自定義的UncaughtExceptionHandler邏輯
                    int i = 1 / 0;
                }
            });
        }
        pool.shutdown();
    }
}

五、ThreadPoolExecutor 類創(chuàng)建線程池

ThreadPoolExecutor 是 java.util.concurrent 包中用來創(chuàng)建線程池的一個(gè)類。它提供了一種靈活的方式來管理線程池,允許你控制線程的創(chuàng)建和銷毀。

ThreadPoolExecutor 類中的幾個(gè)重要方法

  • execute():向線程池提交一個(gè)任務(wù),交由線程池去執(zhí)行
  • submit():也是向線程池提交任務(wù),但是和execute()方法不同,它能夠返回任務(wù)執(zhí)行的結(jié)果它實(shí)際上還是調(diào)用的 execute() 方法,只不過它利用了 Future 來獲取任務(wù)執(zhí)行結(jié)果
  • invokeAll():提交一個(gè)任務(wù)集合
  • invokeAny(): 提交一個(gè)任務(wù)集合,哪個(gè)任務(wù)先成功執(zhí)行完畢,返回此任務(wù)執(zhí)行結(jié)果,其它任務(wù)取消
  • shutdown():關(guān)閉線程池,再也不會(huì)接受新的任務(wù)不會(huì)立即終止線程池,而是要等所有任務(wù)緩存隊(duì)列中的任務(wù)都執(zhí)行完后才終止
  • shutdownNow():關(guān)閉線程池,再也不會(huì)接受新的任務(wù)立即終止線程池,并嘗試打斷正在執(zhí)行的任務(wù),并且清空任務(wù)緩存隊(duì)列,返回尚未執(zhí)行的任務(wù)
  • isShutdown():不在 RUNNING 狀態(tài)的線程池,此方法就返回 true
  • isTerminated():線程池狀態(tài)是否是 TERMINATED
package com.demo.threadPool;
import java.util.Random;
import java.util.concurrent.*;
/**
 * ThreadPoolExecutor 是 java.util.concurrent 包中用來創(chuàng)建線程池的一個(gè)類
 * 它提供了一種靈活的方式來管理線程池,允許你控制線程的創(chuàng)建和銷毀。
 * 以下是幾種常見的創(chuàng)建 ThreadPoolExecutor 線程池的方式
 * 實(shí)際上 Executors 類也是調(diào)用 ThreadPoolExecutor 類創(chuàng)建的線程
 */
public class MainThreadPoolExecutor {
    //測(cè)試方法
    public static void main(String[] args) {
        /**
         * 核心線程數(shù),核心線程就是一直存在的線程
         */
        int corePoolSize = 5;
        /**
         * 最大線程數(shù),表示線程池中最多能創(chuàng)建多少個(gè)線程
         * 非核心線程數(shù) = 最大線程數(shù) - 核心線程數(shù)
         */
        int maximumPoolSize = 10;
        /**
         * 默認(rèn)情況下,只有當(dāng)線程池中的線程數(shù)大于corePoolSize時(shí),
         * keepAliveTime才會(huì)起作用,則會(huì)終止,直到線程池中的線程數(shù)不超過corePoolSize
         * 則會(huì)終止,直到線程池中的線程數(shù)不超過corePoolSize
         * 但是如果調(diào)用了 allowCoreThreadTimeOut(boolean) 方法
         * 在線程池中的線程數(shù)不大于corePoolSize時(shí),keepAliveTime參數(shù)也會(huì)起作用,直到線程池中的線程數(shù)為 0
         * 針對(duì)非核心線程而言,表示線程沒有任務(wù)執(zhí)行時(shí)最多保持多久時(shí)間會(huì)終止
         */
        long keepAliveTime = 60;
        /**
         * 時(shí)間單位
         * 與 keepAliveTime 配合使用,針對(duì)非核心線程
         */
        TimeUnit unit = TimeUnit.SECONDS;
        /**
         * 存放任務(wù)的阻塞隊(duì)列
         */
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(5);
        /**
         * 創(chuàng)建線程的工廠,可以為線程創(chuàng)建時(shí)起個(gè)好名字
         */
        ThreadFactory threadFactory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r);
            }
        };
        /**
         * 拒絕策略
         * 任務(wù)太多的時(shí)候會(huì)進(jìn)行拒絕操作
         * 核心線程,非核心線程,任務(wù)隊(duì)列都放不下時(shí)
         */
        // 自定義拒絕策略
        RejectedExecutionHandler defaultHandler1 = new MyRejectedExecutionHandler();
        // 默認(rèn)策略,在需要拒絕任務(wù)時(shí)拋出RejectedExecutionException
        RejectedExecutionHandler defaultHandler3 = new ThreadPoolExecutor.AbortPolicy();
        // 直接在 execute 方法的調(diào)用線程中運(yùn)行被拒絕的任務(wù),如果線程池已經(jīng)關(guān)閉,任務(wù)將被丟棄;
        RejectedExecutionHandler defaultHandler2 = new ThreadPoolExecutor.CallerRunsPolicy();
        // 直接丟棄任務(wù)
        RejectedExecutionHandler defaultHandler4 = new ThreadPoolExecutor.DiscardPolicy();
        // 丟棄隊(duì)列中等待時(shí)間最長(zhǎng)的任務(wù),并執(zhí)行當(dāng)前提交的任務(wù),如果線程池已經(jīng)關(guān)閉,任務(wù)將被丟棄
        RejectedExecutionHandler defaultHandler5 = new ThreadPoolExecutor.DiscardOldestPolicy();
        /**
         * 創(chuàng)建線程池
         */
        ExecutorService service1 =  new ThreadPoolExecutor( corePoolSize, maximumPoolSize,keepAliveTime, 
                unit,workQueue,threadFactory,defaultHandler1);
        for (int i = 0; i < 10; i++) {
            System.out.println("添加第"+i+"個(gè)任務(wù)");
            service1.execute(new MyThread("線程"+i));
        }
        service1.shutdown();
    }
}

/**
 * 自定義拒絕策略
 */
class MyRejectedExecutionHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        new Thread(r,"新線程"+new Random().nextInt(10)).start();
    }
}

/**
 * 線程類
 */
class MyThread implements Runnable {
    String name;
    public MyThread(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("線程:"+Thread.currentThread().getName() +" 執(zhí)行:"+name +"  run");
    }
}

六、Future 和 Callable 類使用創(chuàng)建線程池

  • Callable 是一個(gè)函數(shù)式接口,它允許你定義一個(gè)任務(wù),該任務(wù)可以返回一個(gè)結(jié)果并拋出異常。它是 Runnable 接口的擴(kuò)展,增加了返回值和拋出異常的能力。
  • 返回值:與 Runnable 接口不同,Callable 任務(wù)可以返回一個(gè)值,返回值通過 Future 對(duì)象獲取。
  • 異常:Callable 任務(wù)可以拋出異常,這些異??梢酝ㄟ^ Future 對(duì)象處理。
  • Future 接口代表異步計(jì)算的結(jié)果。它提供了檢查計(jì)算是否完成的方法,以及獲取計(jì)算結(jié)果的方法。
  • get():獲取計(jì)算結(jié)果。如果計(jì)算尚未完成,此方法會(huì)阻塞,直到計(jì)算完成或拋出異常。
  • isDone():檢查計(jì)算是否完成。
  • cancel():嘗試取消任務(wù)。
  • isCancelled():檢查任務(wù)是否被取消
package com.demo.threadPool;

import java.util.concurrent.*;

/**
 * Future 使用
 */
public class MainFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        System.out.println("開始時(shí)間戳為:" + System.currentTimeMillis());
        Future<String> future = executorService.submit(new Test01());
        String result = future.get(); //獲取計(jì)算結(jié)果。如果計(jì)算尚未完成,此方法會(huì)阻塞,直到計(jì)算完成或拋出異常
        boolean isdone = future.isDone();  //檢查計(jì)算是否完成
        boolean cancel = future.cancel(true);  //嘗試取消任務(wù)
        boolean iscancelled = future.isCancelled(); //檢查任務(wù)是否被取消
        System.out.println("result:"+result);
        System.out.println("isdone:"+isdone);
        System.out.println("cancel:"+cancel);
        System.out.println("iscancelled:"+iscancelled);
        System.out.println("結(jié)束時(shí)間戳為:" + System.currentTimeMillis());
     executorService.shutdown();
    }
}
class Test01 implements Callable {
    @Override
    public Object call() throws Exception {
        return "你好";
    }
}

七、Spring 的 ThreadPoolTaskExecutor 類創(chuàng)建線程池

ThreadPoolTaskExecutor 是 Spring 框架提供的一個(gè)線程池實(shí)現(xiàn),它擴(kuò)展了 Java 的 ThreadPoolExecutor 并提供了一些額外的配置和功能
  • 添加依賴: 如果你的項(xiàng)目是一個(gè) Maven 項(xiàng)目,確保你的 pom.xml 文件中包含了 Spring Boot 的依賴
  • 配置線程池: 在 Spring Boot 應(yīng)用程序中,你可以通過 Java 配置類來配置 ThreadPoolTaskExecutor
package com.cnpc.epai.assetcatalog.dmp.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
 * 線程池配置類
 */
@Configuration
public class ConfigPoolConfiguration {
    @Bean("TaskExecutorDemo")
    public ThreadPoolTaskExecutor taskExecutorDemo(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(10); // 核心線程數(shù)
        threadPoolTaskExecutor.setMaxPoolSize(20);// 最大線程數(shù)
        threadPoolTaskExecutor.setQueueCapacity(100); //工作隊(duì)列
        threadPoolTaskExecutor.setKeepAliveSeconds(60); // 非核心線程的空閑存活時(shí)間
        threadPoolTaskExecutor.setAllowCoreThreadTimeOut(true);//指定是否允許核心線程超時(shí)。這允許動(dòng)態(tài)增長(zhǎng)和收縮,即使與非零隊(duì)列結(jié)合使用也是如此(因?yàn)樽畲蟪卮笮≈挥性陉?duì)列已滿時(shí)才會(huì)增長(zhǎng))
        threadPoolTaskExecutor.setThreadNamePrefix("monitor-thread-pool-");// 設(shè)置線程名前綴
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());// 拒絕策略
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);// 設(shè)置線程池關(guān)閉時(shí)需要等待子任務(wù)執(zhí)行完畢,才銷毀對(duì)應(yīng)的bean
        threadPoolTaskExecutor.initialize();//初始化線程池
        return threadPoolTaskExecutor;
    }
}

測(cè)試類

package com.cnpc.epai.assetcatalog.dmp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Service
public class TestService {
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
    @Async("taskExecutor")
    public void executeTask() {
        taskExecutor.execute(() -> {
            System.out.println("Executing task in thread: " + Thread.currentThread().getName());
        });
    }
}

總結(jié) 

到此這篇關(guān)于Java創(chuàng)建線程池的幾種方式的文章就介紹到這了,更多相關(guān)Java創(chuàng)建線程池方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JavaScript中棧和隊(duì)列應(yīng)用詳情

    JavaScript中棧和隊(duì)列應(yīng)用詳情

    這篇文章主要介紹了JavaScript中棧和隊(duì)列應(yīng)用詳情,棧如果用數(shù)組模擬的話是類似于一個(gè)U形桶狀堆??臻g,文章圍繞制圖展開詳細(xì)的內(nèi)容展開更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-06-06
  • springBoot整合jwt實(shí)現(xiàn)token令牌認(rèn)證的示例代碼

    springBoot整合jwt實(shí)現(xiàn)token令牌認(rèn)證的示例代碼

    實(shí)施Token驗(yàn)證的方法挺多的,還有一些標(biāo)準(zhǔn)方法,比如JWT,本文主要介紹了springBoot整合jwt實(shí)現(xiàn)token令牌認(rèn)證的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(24)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(24)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • 使用@PathVariable注解如何實(shí)現(xiàn)動(dòng)態(tài)傳值

    使用@PathVariable注解如何實(shí)現(xiàn)動(dòng)態(tài)傳值

    這篇文章主要介紹了使用@PathVariable注解如何實(shí)現(xiàn)動(dòng)態(tài)傳值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java遞歸算法簡(jiǎn)單示例兩則

    Java遞歸算法簡(jiǎn)單示例兩則

    這篇文章主要介紹了Java遞歸算法,通過兩則示例分析了Java遞歸算法實(shí)現(xiàn)階乘與求和的具體操作技巧,需要的朋友可以參考下
    2017-09-09
  • 淺談Arrays.asList()方法的使用

    淺談Arrays.asList()方法的使用

    本文主要介紹了Arrays.asList()方法的使用。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • FeignClient實(shí)現(xiàn)接口調(diào)用方式(不同參數(shù)形式)

    FeignClient實(shí)現(xiàn)接口調(diào)用方式(不同參數(shù)形式)

    這篇文章主要介紹了FeignClient實(shí)現(xiàn)接口調(diào)用方式(不同參數(shù)形式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 如何在Spring Boot啟動(dòng)時(shí)運(yùn)行定制的代碼

    如何在Spring Boot啟動(dòng)時(shí)運(yùn)行定制的代碼

    在本文中您將學(xué)習(xí)如何掛鉤應(yīng)用程序引導(dǎo)程序生命周期并在Spring Boot啟動(dòng)時(shí)執(zhí)行代碼。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Java 并發(fā)編程學(xué)習(xí)筆記之Synchronized底層優(yōu)化

    Java 并發(fā)編程學(xué)習(xí)筆記之Synchronized底層優(yōu)化

    這篇文章主要介紹了Java 并發(fā)編程學(xué)習(xí)筆記之Synchronized底層優(yōu)化的相關(guān)資料,主要包含了重量級(jí)鎖,輕量級(jí)鎖,偏向鎖和其他優(yōu)化等方面,有需要的小伙伴可以參考下
    2016-05-05
  • spring是如何實(shí)現(xiàn)聲明式事務(wù)的

    spring是如何實(shí)現(xiàn)聲明式事務(wù)的

    這篇文章主要介紹了spring是如何實(shí)現(xiàn)聲明式事務(wù)的,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評(píng)論