最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

  • new newCachedThreadPool():創(chuàng)建一個(gè)可緩存線程池,如果線程池長(zhǎng)度超過(guò)處理需要,可靈活回收空閑線程,若無(wú)可回收,則新建線程。線程池的規(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ì)用唯一的工作線程來(lái)執(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í)行,也就是說(shuō),它會(huì)在單個(gè)線程上依次執(zhí)行任務(wù),不會(huì)有并發(fā)執(zhí)行的情況發(fā)生
         * 任務(wù)隊(duì)列:如果有多個(gè)任務(wù)提交給這個(gè)執(zhí)行器,除了當(dāng)前正在執(zhí)行的任務(wù)外,其他任務(wù)將會(huì)在一個(gè)無(wú)界隊(duì)列中等待,直到線程可用
         * 處理任務(wù)失?。喝绻麍?zhí)行中的線程由于任務(wù)拋出異常而終止,執(zhí)行服務(wù)會(huì)安排一個(gè)新的線程來(lái)替換它,以繼續(xù)執(zhí)行后續(xù)的任務(wù)
         * 使用場(chǎng)景: newSingleThreadExecutor 非常適合需要順序執(zhí)行的任務(wù),并且要求任務(wù)之間不受并發(fā)問(wèn)題影響的場(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)建新線程的線程池。如果線程空閑超過(guò)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)建新線程的線程池。如果線程空閑超過(guò)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ù)類(lèi)");
            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分鐘開(kāi)始執(zhí)行。此外,
         * 你還需要能夠安排一次性任務(wù)在未來(lái)的某個(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 方法來(lái)關(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 來(lái)創(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 類(lèi)創(chuàng)建線程池

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

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

  • execute():向線程池提交一個(gè)任務(wù),交由線程池去執(zhí)行
  • submit():也是向線程池提交任務(wù),但是和execute()方法不同,它能夠返回任務(wù)執(zhí)行的結(jié)果它實(shí)際上還是調(diào)用的 execute() 方法,只不過(guò)它利用了 Future 來(lái)獲取任務(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 包中用來(lái)創(chuàng)建線程池的一個(gè)類(lèi)
 * 它提供了一種靈活的方式來(lái)管理線程池,允許你控制線程的創(chuàng)建和銷(xiāo)毀。
 * 以下是幾種常見(jiàn)的創(chuàng)建 ThreadPoolExecutor 線程池的方式
 * 實(shí)際上 Executors 類(lèi)也是調(diào)用 ThreadPoolExecutor 類(lèi)創(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ù)不超過(guò)corePoolSize
         * 則會(huì)終止,直到線程池中的線程數(shù)不超過(guò)corePoolSize
         * 但是如果調(diào)用了 allowCoreThreadTimeOut(boolean) 方法
         * 在線程池中的線程數(shù)不大于corePoolSize時(shí),keepAliveTime參數(shù)也會(huì)起作用,直到線程池中的線程數(shù)為 0
         * 針對(duì)非核心線程而言,表示線程沒(méi)有任務(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();
    }
}

/**
 * 線程類(lèi)
 */
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 類(lèi)使用創(chuàng)建線程池

  • Callable 是一個(gè)函數(shù)式接口,它允許你定義一個(gè)任務(wù),該任務(wù)可以返回一個(gè)結(jié)果并拋出異常。它是 Runnable 接口的擴(kuò)展,增加了返回值和拋出異常的能力。
  • 返回值:與 Runnable 接口不同,Callable 任務(wù)可以返回一個(gè)值,返回值通過(guò) Future 對(duì)象獲取。
  • 異常:Callable 任務(wù)可以?huà)伋霎惓#@些異??梢酝ㄟ^(guò) 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("開(kāi)始時(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 類(lèi)創(chuàng)建線程池

ThreadPoolTaskExecutor 是 Spring 框架提供的一個(gè)線程池實(shí)現(xiàn),它擴(kuò)展了 Java 的 ThreadPoolExecutor 并提供了一些額外的配置和功能
  • 添加依賴(lài): 如果你的項(xiàng)目是一個(gè) Maven 項(xiàng)目,確保你的 pom.xml 文件中包含了 Spring Boot 的依賴(lài)
  • 配置線程池: 在 Spring Boot 應(yīng)用程序中,你可以通過(guò) Java 配置類(lèi)來(lái)配置 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;
/**
 * 線程池配置類(lèi)
 */
@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ì)列已滿(mǎn)時(shí)才會(huì)增長(zhǎng))
        threadPoolTaskExecutor.setThreadNamePrefix("monitor-thread-pool-");// 設(shè)置線程名前綴
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());// 拒絕策略
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);// 設(shè)置線程池關(guān)閉時(shí)需要等待子任務(wù)執(zhí)行完畢,才銷(xiāo)毀對(duì)應(yīng)的bean
        threadPoolTaskExecutor.initialize();//初始化線程池
        return threadPoolTaskExecutor;
    }
}

測(cè)試類(lèi)

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)文章

  • SpringBoot實(shí)現(xiàn)elasticsearch索引操作的代碼示例

    SpringBoot實(shí)現(xiàn)elasticsearch索引操作的代碼示例

    這篇文章主要給大家介紹了SpringBoot如何實(shí)現(xiàn)elasticsearch 索引操作,文中有詳細(xì)的代碼示例,感興趣的同學(xué)可以參考閱讀下
    2023-07-07
  • IntelliJ IDEA2020.3詳細(xì)安裝教程

    IntelliJ IDEA2020.3詳細(xì)安裝教程

    這篇文章主要介紹了IntelliJ IDEA2020.3詳細(xì)安裝教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • java swing 創(chuàng)建一個(gè)簡(jiǎn)單的QQ界面教程

    java swing 創(chuàng)建一個(gè)簡(jiǎn)單的QQ界面教程

    這篇文章主要介紹了java swing 創(chuàng)建一個(gè)簡(jiǎn)單的QQ界面教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • String s = new String(''a '') 到底產(chǎn)生幾個(gè)對(duì)象

    String s = new String(''a '') 到底產(chǎn)生幾個(gè)對(duì)象

    這篇文章主要介紹了String s = new String(" a ") 到底產(chǎn)生幾個(gè)對(duì)象,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • IDEA多Module設(shè)置language level默認(rèn)級(jí)別方式

    IDEA多Module設(shè)置language level默認(rèn)級(jí)別方式

    文章介紹了在Java項(xiàng)目中遇到編譯錯(cuò)誤,源發(fā)行版8需要目標(biāo)發(fā)行版1.8的問(wèn)題,并提供了兩種解決方案:通過(guò)在出現(xiàn)問(wèn)題的module的pom文件中加入特定代碼,或者修改maven的setting.xml文件來(lái)全局設(shè)置languagelevel,作者為解決這個(gè)問(wèn)題提供了個(gè)人經(jīng)驗(yàn),并鼓勵(lì)讀者參考和支持
    2025-12-12
  • Spring AOP 切面@Around注解的用法說(shuō)明

    Spring AOP 切面@Around注解的用法說(shuō)明

    這篇文章主要介紹了Spring AOP 切面@Around注解的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • SpringBoot如何訪問(wèn)jsp頁(yè)面

    SpringBoot如何訪問(wèn)jsp頁(yè)面

    本文介紹了如何在Spring Boot項(xiàng)目中進(jìn)行Web開(kāi)發(fā),包括創(chuàng)建項(xiàng)目、配置文件、添加依賴(lài)、控制層修改、測(cè)試效果以及在IDEA中進(jìn)行配置的詳細(xì)步驟
    2025-01-01
  • 解析springboot整合谷歌開(kāi)源緩存框架Guava Cache原理

    解析springboot整合谷歌開(kāi)源緩存框架Guava Cache原理

    本文主要為大家解析了springboot整合谷歌開(kāi)源緩存框架Guava Cache的原理以及在實(shí)際開(kāi)發(fā)過(guò)程中的使用,附含源碼,有需要的朋友可以參考下
    2021-08-08
  • FastJson實(shí)現(xiàn)駝峰下劃線相互轉(zhuǎn)換方法詳解

    FastJson實(shí)現(xiàn)駝峰下劃線相互轉(zhuǎn)換方法詳解

    這篇文章主要介紹了使用FastJson進(jìn)行駝峰下劃線相互轉(zhuǎn)換寫(xiě)法及誤區(qū),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-01-01
  • SpringBoot啟動(dòng)yaml報(bào)錯(cuò)的解決

    SpringBoot啟動(dòng)yaml報(bào)錯(cuò)的解決

    這篇文章主要介紹了SpringBoot啟動(dòng)yaml報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論

鸡西市| 安远县| 海兴县| 峨眉山市| 蓬莱市| 台前县| 马山县| 天长市| 安庆市| 同心县| 双柏县| 儋州市| 双鸭山市| 阿克| 本溪| 广安市| 柳州市| 达尔| 会昌县| 尉氏县| 西盟| 桂平市| 桃园市| 广元市| 泰顺县| 徐水县| 新丰县| 长岭县| 大竹县| 靖边县| 临江市| 德清县| 休宁县| 于都县| 茶陵县| 毕节市| 玉龙| 邯郸县| 朝阳市| 商城县| 平安县|