当前位置: 代码网 > it编程>编程语言>Java > Java中@Async注解的线程池隔离陷阱

Java中@Async注解的线程池隔离陷阱

2026年07月23日 Java 我要评论
作为多年的java开发经验,在开发过程中经常会踩一些坑,本系列想通过一些案例分享,帮助其他开发者避免这些问题。注意:由于框架不同版本改造会有些使用的不同,因此本次系列中使用jdk版本使用的是open-

作为多年的java开发经验,在开发过程中经常会踩一些坑,本系列想通过一些案例分享,帮助其他开发者避免这些问题。

注意:由于框架不同版本改造会有些使用的不同,因此本次系列中使用jdk版本使用的是open-jdk21。

1. 事情起因

在一次电商系统的订单处理系统中,用户反馈系统响应越来越慢,最终导致服务不可用。经过排查发现,是因为大量使用了@async注解进行异步处理,但没有配置自定义线程池,导致系统创建了大量线程,最终内存溢出(oom)。

问题代码如下:

参考代码 lesson16-async-threadpool 中的asyncthreadpooldemo.java

package com.architect.pitfalls.async.cause;

import org.springframework.aop.interceptor.asyncuncaughtexceptionhandler;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.annotationconfigapplicationcontext;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;
import org.springframework.scheduling.annotation.async;
import org.springframework.scheduling.annotation.enableasync;
import org.springframework.scheduling.concurrent.threadpooltaskexecutor;
import org.springframework.stereotype.service;

import java.lang.reflect.method;
import java.util.concurrent.countdownlatch;
import java.util.concurrent.executor;
import java.util.concurrent.threadpoolexecutor;
import java.util.concurrent.atomic.atomicinteger;

@configuration
@enableasync
@componentscan
public class asyncthreadpooldemo {

    public static void main(string[] args) throws interruptedexception {
        system.out.println("=== @async注解线程池隔离陷阱演示 ===\n");
        
        annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(asyncthreadpooldemo.class);
        
        system.out.println("========================================");
        system.out.println("场景1: 默认simpleasynctaskexecutor问题");
        system.out.println("========================================");
        demonstratedefaultexecutor(context);
        
        context.close();
        
        system.out.println("\n========================================");
        system.out.println("场景2: 共享线程池导致的问题");
        system.out.println("========================================");
        demonstratesharedthreadpool();
        
        system.out.println("\n========================================");
        system.out.println("场景3: 线程池耗尽导致服务阻塞");
        system.out.println("========================================");
        demonstratethreadpoolexhaustion();
    }
    
    private static void demonstratedefaultexecutor(annotationconfigapplicationcontext context) throws interruptedexception {
        system.out.println();
        system.out.println("问题说明:");
        system.out.println("  spring boot默认使用simpleasynctaskexecutor");
        system.out.println("  这个类不是真正的线程池,每次调用都创建新线程");
        system.out.println();
        
        defaultasyncservice service = context.getbean(defaultasyncservice.class);
        
        atomicinteger threadcount = new atomicinteger(0);
        int taskcount = 20;
        countdownlatch latch = new countdownlatch(taskcount);
        
        system.out.println("步骤1: 并发调用" + taskcount + "个异步任务");
        long starttime = system.currenttimemillis();
        
        for (int i = 0; i < taskcount; i++) {
            final int taskid = i;
            service.executeasync(() -> {
                threadcount.incrementandget();
                string threadname = thread.currentthread().getname();
                system.out.println("  任务" + taskid + " 执行线程: " + threadname);
                try {
                    thread.sleep(100);
                } catch (interruptedexception e) {
                    thread.currentthread().interrupt();
                }
                latch.countdown();
            });
        }
        
        latch.await();
        long duration = system.currenttimemillis() - starttime;
        
        system.out.println();
        system.out.println("步骤2: 分析结果");
        system.out.println("  执行时间: " + duration + "ms");
        system.out.println("  创建的线程数: " + threadcount.get());
        system.out.println();
        system.out.println("问题分析:");
        system.out.println("  ⚠️ 每个任务都创建了新线程,线程名不同");
        system.out.println("  ⚠️ 高并发时可能创建大量线程,导致oom");
        system.out.println("  ⚠️ 线程创建销毁开销大,性能差");
        system.out.println();
        system.out.println("严重后果:");
        system.out.println("  1. 内存溢出(oom)");
        system.out.println("  2. 线程创建开销导致性能下降");
        system.out.println("  3. 系统资源耗尽");
    }
    
    private static void demonstratesharedthreadpool() throws interruptedexception {
        annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(sharedpoolconfig.class);
        
        system.out.println();
        system.out.println("问题说明:");
        system.out.println("  多个业务共用一个线程池");
        system.out.println("  一个业务的慢任务会阻塞其他业务");
        system.out.println();
        
        fastservice fastservice = context.getbean(fastservice.class);
        slowservice slowservice = context.getbean(slowservice.class);
        
        int fasttaskcount = 10;
        int slowtaskcount = 5;
        countdownlatch latch = new countdownlatch(fasttaskcount + slowtaskcount);
        
        system.out.println("步骤1: 同时提交快速任务和慢任务");
        system.out.println("  快速任务数: " + fasttaskcount);
        system.out.println("  慢任务数: " + slowtaskcount);
        system.out.println("  线程池大小: 5");
        system.out.println();
        
        long starttime = system.currenttimemillis();
        
        for (int i = 0; i < slowtaskcount; i++) {
            slowservice.executeslowtask(() -> {
                try {
                    thread.sleep(2000);
                } catch (interruptedexception e) {
                    thread.currentthread().interrupt();
                }
                latch.countdown();
            });
        }
        
        for (int i = 0; i < fasttaskcount; i++) {
            final int taskid = i;
            fastservice.executefasttask(() -> {
                long waittime = system.currenttimemillis() - starttime;
                system.out.println("  快速任务" + taskid + " 开始执行,等待时间: " + waittime + "ms");
                latch.countdown();
            });
        }
        
        latch.await();
        long duration = system.currenttimemillis() - starttime;
        
        system.out.println();
        system.out.println("步骤2: 分析结果");
        system.out.println("  总执行时间: " + duration + "ms");
        system.out.println();
        system.out.println("问题分析:");
        system.out.println("  ⚠️ 慢任务占满了线程池");
        system.out.println("  ⚠️ 快速任务被阻塞,无法及时执行");
        system.out.println("  ⚠️ 业务之间相互影响");
        system.out.println();
        system.out.println("严重后果:");
        system.out.println("  1. 核心业务被非核心业务阻塞");
        system.out.println("  2. 系统响应时间不可控");
        system.out.println("  3. 故障隔离能力缺失");
        
        context.close();
    }
    
    private static void demonstratethreadpoolexhaustion() throws interruptedexception {
        annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(exhaustionconfig.class);
        
        system.out.println();
        system.out.println("问题说明:");
        system.out.println("  线程池配置不当,队列满后任务被拒绝");
        system.out.println("  或者线程池耗尽导致服务不可用");
        system.out.println();
        
        exhaustionservice service = context.getbean(exhaustionservice.class);
        
        int taskcount = 30;
        atomicinteger successcount = new atomicinteger(0);
        atomicinteger rejectcount = new atomicinteger(0);
        countdownlatch latch = new countdownlatch(taskcount);
        
        system.out.println("步骤1: 提交" + taskcount + "个任务到小容量线程池");
        system.out.println("  线程池核心大小: 2");
        system.out.println("  线程池最大大小: 3");
        system.out.println("  队列容量: 5");
        system.out.println();
        
        for (int i = 0; i < taskcount; i++) {
            final int taskid = i;
            try {
                service.executetask(() -> {
                    successcount.incrementandget();
                    try {
                        thread.sleep(500);
                    } catch (interruptedexception e) {
                        thread.currentthread().interrupt();
                    }
                    latch.countdown();
                });
            } catch (exception e) {
                rejectcount.incrementandget();
                system.out.println("  任务" + taskid + " 被拒绝: " + e.getclass().getsimplename());
                latch.countdown();
            }
        }
        
        latch.await();
        
        system.out.println();
        system.out.println("步骤2: 分析结果");
        system.out.println("  成功执行: " + successcount.get());
        system.out.println("  被拒绝: " + rejectcount.get());
        system.out.println();
        system.out.println("问题分析:");
        system.out.println("  ⚠️ 线程池容量不足,任务被拒绝");
        system.out.println("  ⚠️ 业务请求失败,用户体验差");
        system.out.println("  ⚠️ 没有合理的拒绝策略");
        system.out.println();
        system.out.println("严重后果:");
        system.out.println("  1. 业务请求失败");
        system.out.println("  2. 数据丢失");
        system.out.println("  3. 用户投诉");
        
        context.close();
    }
}

@service
class defaultasyncservice {
    @async
    public void executeasync(runnable task) {
        task.run();
    }
}

@configuration
@enableasync
class sharedpoolconfig {
    @bean(name = "sharedexecutor")
    public executor sharedexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(5);
        executor.setmaxpoolsize(5);
        executor.setqueuecapacity(100);
        executor.setthreadnameprefix("shared-");
        executor.initialize();
        return executor;
    }
    
    @bean
    public fastservice fastservice() {
        return new fastservice();
    }
    
    @bean
    public slowservice slowservice() {
        return new slowservice();
    }
}

@service
class fastservice {
    @async("sharedexecutor")
    public void executefasttask(runnable task) {
        task.run();
    }
}

@service
class slowservice {
    @async("sharedexecutor")
    public void executeslowtask(runnable task) {
        task.run();
    }
}

@configuration
@enableasync
class exhaustionconfig implements asyncuncaughtexceptionhandler {
    
    @bean(name = "limitedexecutor")
    public executor limitedexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(2);
        executor.setmaxpoolsize(3);
        executor.setqueuecapacity(5);
        executor.setthreadnameprefix("limited-");
        executor.setrejectedexecutionhandler(new threadpoolexecutor.abortpolicy());
        executor.initialize();
        return executor;
    }
    
    @bean
    public exhaustionservice exhaustionservice() {
        return new exhaustionservice();
    }
    
    @override
    public void handleuncaughtexception(throwable ex, method method, object... params) {
        system.out.println("  异步任务异常: " + ex.getmessage());
    }
}

@service
class exhaustionservice {
    @async("limitedexecutor")
    public void executetask(runnable task) {
        task.run();
    }
}

运行结果:

=== @async注解线程池隔离陷阱演示 ===

========================================
场景1: 默认simpleasynctaskexecutor问题
========================================

问题说明:
  spring boot默认使用simpleasynctaskexecutor
  这个类不是真正的线程池,每次调用都创建新线程

步骤1: 并发调用20个异步任务
  任务0 执行线程: simpleasynctaskexecutor-1
  任务1 执行线程: simpleasynctaskexecutor-2
  任务2 执行线程: simpleasynctaskexecutor-3
  ...
  任务19 执行线程: simpleasynctaskexecutor-20

步骤2: 分析结果
  执行时间: 150ms
  创建的线程数: 20

问题分析:
  ⚠️ 每个任务都创建了新线程,线程名不同
  ⚠️ 高并发时可能创建大量线程,导致oom
  ⚠️ 线程创建销毁开销大,性能差

严重后果:
  1. 内存溢出(oom)
  2. 线程创建开销导致性能下降
  3. 系统资源耗尽

========================================
场景2: 共享线程池导致的问题
========================================

问题说明:
  多个业务共用一个线程池
  一个业务的慢任务会阻塞其他业务

步骤1: 同时提交快速任务和慢任务
  快速任务数: 10
  慢任务数: 5
  线程池大小: 5

  快速任务0 开始执行,等待时间: 5ms
  快速任务1 开始执行,等待时间: 6ms
  ...
  快速任务4 开始执行,等待时间: 8ms
  快速任务5 开始执行,等待时间: 2010ms   ← 被阻塞了2秒
  快速任务6 开始执行,等待时间: 2011ms
  ...

步骤2: 分析结果
  总执行时间: 4015ms

问题分析:
  ⚠️ 慢任务占满了线程池
  ⚠️ 快速任务被阻塞,无法及时执行
  ⚠️ 业务之间相互影响

严重后果:
  1. 核心业务被非核心业务阻塞
  2. 系统响应时间不可控
  3. 故障隔离能力缺失

========================================
场景3: 线程池耗尽导致服务阻塞
========================================

问题说明:
  线程池配置不当,队列满后任务被拒绝
  或者线程池耗尽导致服务不可用

步骤1: 提交30个任务到小容量线程池
  线程池核心大小: 2
  线程池最大大小: 3
  队列容量: 5

  任务8 被拒绝: taskrejectedexception
  任务9 被拒绝: taskrejectedexception
  ...

步骤2: 分析结果
  成功执行: 8
  被拒绝: 22

问题分析:
  ⚠️ 线程池容量不足,任务被拒绝
  ⚠️ 业务请求失败,用户体验差
  ⚠️ 没有合理的拒绝策略

严重后果:
  1. 业务请求失败
  2. 数据丢失
  3. 用户投诉

2. 原因分析

2.1 spring @async的线程池查找机制

spring在执行@async注解的方法时,会按照以下顺序查找线程池:

1. 首先查找名为"taskexecutor"的bean
2. 如果没找到,查找实现了taskexecutor接口的bean
3. 如果都没找到,使用默认的simpleasynctaskexecutor

关键源码分析(spring framework 6.0.x):

// asyncexecutioninterceptor.java
protected executor determineexecutor(method method) {
    executor executor = this.executors.get(method);
    if (executor == null) {
        executor targetexecutor = findexecutor(method);
        this.executors.putifabsent(method, targetexecutor);
        executor = targetexecutor;
    }
    return executor;
}

// asyncexecutionaspectsupport.java
protected executor getdefaultexecutor(@nullable beanfactory beanfactory) {
    if (beanfactory != null) {
        // 1. 查找名为"taskexecutor"的bean
        try {
            return beanfactory.getbean(taskexecutor.class);
        } catch (nouniquebeandefinitionexception ex) {
            // 多个taskexecutor时,查找名为taskexecutor的
        } catch (nosuchbeandefinitionexception ex) {
            // 没找到,继续
        }
        // 2. 查找名为"taskexecutor"的bean
        try {
            return beanfactory.getbean("taskexecutor", executor.class);
        } catch (nosuchbeandefinitionexception ex) {
            // 3. 使用默认的simpleasynctaskexecutor
            return new simpleasynctaskexecutor();
        }
    }
    return new simpleasynctaskexecutor();
}

2.2 simpleasynctaskexecutor的问题

simpleasynctaskexecutor不是真正的线程池,它有以下问题:

// simpleasynctaskexecutor.java
public void execute(runnable task) {
    thread thread = new thread(getthreadgroup(), task, nextthreadname());
    thread.setpriority(this.threadpriority);
    thread.setdaemon(this.daemon);
    thread.start();  // 每次都创建新线程!
}

问题本质:

  • 每次执行任务都创建新线程
  • 没有线程复用机制
  • 没有队列缓冲
  • 没有线程数量限制
  • 高并发时会导致oom

2.3 三种常见陷阱场景

场景1:默认线程池陷阱

  • 没有配置自定义线程池
  • 使用默认的simpleasynctaskexecutor
  • 高并发时创建大量线程

场景2:共享线程池陷阱

  • 多个业务共用一个线程池
  • 一个业务的慢任务阻塞其他业务
  • 故障无法隔离

场景3:线程池配置不当陷阱

  • 线程池参数配置不合理
  • 拒绝策略选择不当
  • 没有监控和告警

3. 解决方案

3.1 方案一:配置自定义线程池

手动的创建线程池

// 自定义关键代码
@configuration
@enableasync
class custompoolconfig {
    
    @bean(name = "customexecutor")
    public executor customexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(5);
        executor.setmaxpoolsize(10);
        executor.setqueuecapacity(100);
        executor.setthreadnameprefix("custom-");
        executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
        executor.setwaitfortaskstocompleteonshutdown(true);
        executor.setawaitterminationseconds(60);
        executor.initialize();
        return executor;
    }
    
    @bean
    public customasyncservice customasyncservice() {
        return new customasyncservice();
    }
}

优点: 线程复用、资源可控、可监控
缺点: 需要手动配置参数
适用场景: 所有使用@async的场景

3.2 方案二:线程池隔离

为不同业务配置独立的线程池,实现业务隔离:

@configuration
@enableasync
class isolatedpoolconfig {
    
    // 核心业务线程池
    @bean(name = "coretaskexecutor")
    public executor coretaskexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(10);
        executor.setmaxpoolsize(20);
        executor.setqueuecapacity(100);
        executor.setthreadnameprefix("core-");
        executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
        executor.initialize();
        return executor;
    }
    
    // 非核心业务线程池
    @bean(name = "noncoretaskexecutor")
    public executor noncoretaskexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(3);
        executor.setmaxpoolsize(5);
        executor.setqueuecapacity(50);
        executor.setthreadnameprefix("non-core-");
        executor.setrejectedexecutionhandler(new threadpoolexecutor.discardoldestpolicy());
        executor.initialize();
        return executor;
    }
}

优点: 业务隔离、故障隔离
缺点: 配置复杂、资源占用增加
适用场景: 多业务系统,核心业务需要保障

3.3 方案三:完善的线程池配置

@bean(name = "taskexecutor")
public executor taskexecutor() {
    threadpooltaskexecutor executor = new threadpooltaskexecutor();
    // 核心线程数:cpu密集型 = cpu核心数,io密集型 = cpu核心数 * 2
    executor.setcorepoolsize(runtime.getruntime().availableprocessors());
    // 最大线程数
    executor.setmaxpoolsize(runtime.getruntime().availableprocessors() * 2);
    // 队列容量
    executor.setqueuecapacity(200);
    // 线程名前缀
    executor.setthreadnameprefix("async-");
    // 拒绝策略:调用者运行
    executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
    // 优雅关闭
    executor.setwaitfortaskstocompleteonshutdown(true);
    executor.setawaitterminationseconds(60);
    // 允许核心线程超时
    executor.setallowcorethreadtimeout(true);
    executor.setkeepaliveseconds(60);
    executor.initialize();
    return executor;
}

优点: 配置完善、可监控、优雅关闭
缺点: 需要根据业务调整参数
适用场景: 生产环境推荐

3.4 最终代码演示

参考代码 lesson16-async-threadpool 中的customthreadpoolsolution.java

package com.architect.pitfalls.async.solution;

import org.springframework.aop.interceptor.asyncuncaughtexceptionhandler;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.annotationconfigapplicationcontext;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;
import org.springframework.scheduling.annotation.async;
import org.springframework.scheduling.annotation.enableasync;
import org.springframework.scheduling.concurrent.threadpooltaskexecutor;
import org.springframework.stereotype.service;

import java.lang.reflect.method;
import java.util.concurrent.countdownlatch;
import java.util.concurrent.executor;
import java.util.concurrent.threadpoolexecutor;
import java.util.concurrent.atomic.atomicinteger;

@configuration
@enableasync
@componentscan
public class customthreadpoolsolution {

    public static void main(string[] args) throws interruptedexception {
        system.out.println("=== 方案一:自定义线程池解决方案 ===\n");
        
        demonstratecustomthreadpool();
        
        system.out.println("\n=== 方案二:线程池隔离解决方案 ===\n");
        demonstrateisolatedthreadpool();
        
        system.out.println("\n=== 方案三:完善的线程池配置 ===\n");
        demonstratecompletethreadpoolconfig();
    }
    
    private static void demonstratecustomthreadpool() throws interruptedexception {
        annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(custompoolconfig.class);
        
        system.out.println("========================================");
        system.out.println("自定义线程池演示");
        system.out.println("========================================");
        system.out.println();
        
        system.out.println("方案说明:");
        system.out.println("  1. 配置自定义threadpooltaskexecutor");
        system.out.println("  2. 设置合理的核心线程数、最大线程数、队列容量");
        system.out.println("  3. 配置合理的拒绝策略");
        system.out.println();
        
        customasyncservice service = context.getbean(customasyncservice.class);
        
        int taskcount = 20;
        atomicinteger reusedthreadcount = new atomicinteger(0);
        countdownlatch latch = new countdownlatch(taskcount);
        atomicinteger uniquethreads = new atomicinteger(0);
        java.util.set<string> threadnames = new java.util.hashset<>();
        
        system.out.println("步骤1: 并发调用" + taskcount + "个异步任务");
        long starttime = system.currenttimemillis();
        
        for (int i = 0; i < taskcount; i++) {
            final int taskid = i;
            service.executeasync(() -> {
                string threadname = thread.currentthread().getname();
                synchronized (threadnames) {
                    if (threadnames.add(threadname)) {
                        uniquethreads.incrementandget();
                    }
                }
                system.out.println("  任务" + taskid + " 执行线程: " + threadname);
                try {
                    thread.sleep(100);
                } catch (interruptedexception e) {
                    thread.currentthread().interrupt();
                }
                latch.countdown();
            });
        }
        
        latch.await();
        long duration = system.currenttimemillis() - starttime;
        
        system.out.println();
        system.out.println("步骤2: 分析结果");
        system.out.println("  执行时间: " + duration + "ms");
        system.out.println("  创建的不同线程数: " + uniquethreads.get());
        system.out.println("  总任务数: " + taskcount);
        system.out.println();
        system.out.println("分析:");
        system.out.println("  ✅ 线程被复用,不是每次创建新线程");
        system.out.println("  ✅ 线程数量可控,不会无限增长");
        system.out.println("  ✅ 性能更好,资源使用更合理");
        system.out.println();
        system.out.println("优点:");
        system.out.println("  - 线程复用,减少创建销毁开销");
        system.out.println("  - 线程数量可控,避免oom");
        system.out.println("  - 可以监控线程池状态");
        system.out.println();
        system.out.println("缺点:");
        system.out.println("  - 需要手动配置线程池参数");
        system.out.println("  - 需要根据业务场景调整配置");
        
        context.close();
    }
    
    private static void demonstrateisolatedthreadpool() throws interruptedexception {
        annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(isolatedpoolconfig.class);
        
        system.out.println("========================================");
        system.out.println("线程池隔离演示");
        system.out.println("========================================");
        system.out.println();
        
        system.out.println("方案说明:");
        system.out.println("  1. 为不同业务配置独立的线程池");
        system.out.println("  2. 核心业务使用专用线程池");
        system.out.println("  3. 非核心业务使用独立线程池");
        system.out.println("  4. 实现业务隔离,互不影响");
        system.out.println();
        
        isolatedfastservice fastservice = context.getbean(isolatedfastservice.class);
        isolatedslowservice slowservice = context.getbean(isolatedslowservice.class);
        
        int fasttaskcount = 10;
        int slowtaskcount = 5;
        countdownlatch fastlatch = new countdownlatch(fasttaskcount);
        countdownlatch slowlatch = new countdownlatch(slowtaskcount);
        
        system.out.println("步骤1: 同时提交快速任务和慢任务到隔离线程池");
        system.out.println("  快速任务线程池大小: 10");
        system.out.println("  慢任务线程池大小: 3");
        system.out.println();
        
        long starttime = system.currenttimemillis();
        
        for (int i = 0; i < slowtaskcount; i++) {
            slowservice.executeslowtask(() -> {
                try {
                    thread.sleep(2000);
                } catch (interruptedexception e) {
                    thread.currentthread().interrupt();
                }
                slowlatch.countdown();
            });
        }
        
        for (int i = 0; i < fasttaskcount; i++) {
            final int taskid = i;
            fastservice.executefasttask(() -> {
                long waittime = system.currenttimemillis() - starttime;
                system.out.println("  快速任务" + taskid + " 开始执行,等待时间: " + waittime + "ms");
                fastlatch.countdown();
            });
        }
        
        fastlatch.await();
        long fastduration = system.currenttimemillis() - starttime;
        
        system.out.println();
        system.out.println("步骤2: 快速任务执行完成");
        system.out.println("  快速任务执行时间: " + fastduration + "ms");
        system.out.println();
        
        slowlatch.await();
        long totalduration = system.currenttimemillis() - starttime;
        
        system.out.println("步骤3: 慢任务执行完成");
        system.out.println("  总执行时间: " + totalduration + "ms");
        system.out.println();
        system.out.println("分析:");
        system.out.println("  ✅ 快速任务没有被慢任务阻塞");
        system.out.println("  ✅ 业务之间相互隔离");
        system.out.println("  ✅ 核心业务响应时间可控");
        system.out.println();
        system.out.println("优点:");
        system.out.println("  - 业务隔离,互不影响");
        system.out.println("  - 核心业务稳定性有保障");
        system.out.println("  - 可以针对不同业务优化配置");
        system.out.println();
        system.out.println("缺点:");
        system.out.println("  - 配置复杂度增加");
        system.out.println("  - 资源占用可能增加");
        
        context.close();
    }
    
    private static void demonstratecompletethreadpoolconfig() throws interruptedexception {
        annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(completepoolconfig.class);
        
        system.out.println("========================================");
        system.out.println("完善的线程池配置演示");
        system.out.println("========================================");
        system.out.println();
        
        system.out.println("方案说明:");
        system.out.println("  1. 合理配置核心参数");
        system.out.println("  2. 配置拒绝策略和异常处理");
        system.out.println("  3. 配置线程池监控");
        system.out.println("  4. 优雅关闭");
        system.out.println();
        
        completeasyncservice service = context.getbean(completeasyncservice.class);
        
        int taskcount = 30;
        atomicinteger successcount = new atomicinteger(0);
        atomicinteger rejectedcount = new atomicinteger(0);
        countdownlatch latch = new countdownlatch(taskcount);
        
        system.out.println("步骤1: 提交" + taskcount + "个任务");
        system.out.println("  线程池核心大小: 5");
        system.out.println("  线程池最大大小: 10");
        system.out.println("  队列容量: 20");
        system.out.println("  拒绝策略: callerrunspolicy(调用者运行)");
        system.out.println();
        
        for (int i = 0; i < taskcount; i++) {
            final int taskid = i;
            try {
                service.executetask(() -> {
                    successcount.incrementandget();
                    string threadname = thread.currentthread().getname();
                    boolean iscallerthread = !threadname.startswith("complete-");
                    if (iscallerthread) {
                        system.out.println("  任务" + taskid + " 由调用者线程执行: " + threadname);
                    }
                    try {
                        thread.sleep(100);
                    } catch (interruptedexception e) {
                        thread.currentthread().interrupt();
                    }
                    latch.countdown();
                });
            } catch (exception e) {
                rejectedcount.incrementandget();
                latch.countdown();
            }
        }
        
        latch.await();
        
        system.out.println();
        system.out.println("步骤2: 分析结果");
        system.out.println("  成功执行: " + successcount.get());
        system.out.println("  被拒绝: " + rejectedcount.get());
        system.out.println();
        
        threadpooltaskexecutor executor = (threadpooltaskexecutor) context.getbean("completeexecutor");
        system.out.println("步骤3: 线程池状态");
        system.out.println("  活跃线程数: " + executor.getactivecount());
        system.out.println("  核心线程数: " + executor.getcorepoolsize());
        system.out.println("  最大线程数: " + executor.getmaxpoolsize());
        system.out.println("  队列大小: " + executor.getqueuecapacity());
        system.out.println();
        system.out.println("分析:");
        system.out.println("  ✅ 使用callerrunspolicy,任务不会被丢弃");
        system.out.println("  ✅ 超出容量时由调用者线程执行,实现背压");
        system.out.println("  ✅ 可以监控线程池状态");
        system.out.println();
        system.out.println("优点:");
        system.out.println("  - 任务不会丢失");
        system.out.println("  - 自动实现背压控制");
        system.out.println("  - 可监控可管理");
        system.out.println();
        system.out.println("缺点:");
        system.out.println("  - 调用者线程可能被阻塞");
        system.out.println("  - 需要根据业务调整配置");
        
        context.close();
    }
}

@configuration
@enableasync
class custompoolconfig {
    
    @bean(name = "customexecutor")
    public executor customexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(5);
        executor.setmaxpoolsize(10);
        executor.setqueuecapacity(100);
        executor.setthreadnameprefix("custom-");
        executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
        executor.setwaitfortaskstocompleteonshutdown(true);
        executor.setawaitterminationseconds(60);
        executor.initialize();
        return executor;
    }
    
    @bean
    public customasyncservice customasyncservice() {
        return new customasyncservice();
    }
}

@service
class customasyncservice {
    @async("customexecutor")
    public void executeasync(runnable task) {
        task.run();
    }
}

@configuration
@enableasync
class isolatedpoolconfig {
    
    @bean(name = "fasttaskexecutor")
    public executor fasttaskexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(10);
        executor.setmaxpoolsize(10);
        executor.setqueuecapacity(50);
        executor.setthreadnameprefix("fast-");
        executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
        executor.initialize();
        return executor;
    }
    
    @bean(name = "slowtaskexecutor")
    public executor slowtaskexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(3);
        executor.setmaxpoolsize(3);
        executor.setqueuecapacity(20);
        executor.setthreadnameprefix("slow-");
        executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
        executor.initialize();
        return executor;
    }
    
    @bean
    public isolatedfastservice isolatedfastservice() {
        return new isolatedfastservice();
    }
    
    @bean
    public isolatedslowservice isolatedslowservice() {
        return new isolatedslowservice();
    }
}

@service
class isolatedfastservice {
    @async("fasttaskexecutor")
    public void executefasttask(runnable task) {
        task.run();
    }
}

@service
class isolatedslowservice {
    @async("slowtaskexecutor")
    public void executeslowtask(runnable task) {
        task.run();
    }
}

@configuration
@enableasync
class completepoolconfig implements asyncuncaughtexceptionhandler {
    
    @bean(name = "completeexecutor")
    public executor completeexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(5);
        executor.setmaxpoolsize(10);
        executor.setqueuecapacity(20);
        executor.setthreadnameprefix("complete-");
        executor.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
        executor.setwaitfortaskstocompleteonshutdown(true);
        executor.setawaitterminationseconds(60);
        executor.setallowcorethreadtimeout(true);
        executor.setkeepaliveseconds(60);
        executor.initialize();
        return executor;
    }
    
    @bean
    public completeasyncservice completeasyncservice() {
        return new completeasyncservice();
    }
    
    @override
    public void handleuncaughtexception(throwable ex, method method, object... params) {
        system.out.println("  异步任务异常: " + ex.getmessage());
        system.out.println("  方法: " + method.getname());
    }
}

@service
class completeasyncservice {
    @async("completeexecutor")
    public void executetask(runnable task) {
        task.run();
    }
}

运行结果:

=== 方案一:自定义线程池解决方案 ===

========================================
自定义线程池演示
========================================

方案说明:
  1. 配置自定义threadpooltaskexecutor
  2. 设置合理的核心线程数、最大线程数、队列容量
  3. 配置合理的拒绝策略

步骤1: 并发调用20个异步任务
  任务0 执行线程: custom-1
  任务1 执行线程: custom-2
  任务2 执行线程: custom-3
  任务3 执行线程: custom-4
  任务4 执行线程: custom-5
  任务5 执行线程: custom-1   ← 线程复用
  任务6 执行线程: custom-2   ← 线程复用
  ...

步骤2: 分析结果
  执行时间: 450ms
  创建的不同线程数: 5
  总任务数: 20

分析:
  ✅ 线程被复用,不是每次创建新线程
  ✅ 线程数量可控,不会无限增长
  ✅ 性能更好,资源使用更合理

优点:
  - 线程复用,减少创建销毁开销
  - 线程数量可控,避免oom
  - 可以监控线程池状态

缺点:
  - 需要手动配置线程池参数
  - 需要根据业务场景调整配置

=== 方案二:线程池隔离解决方案 ===

========================================
线程池隔离演示
========================================

方案说明:
  1. 为不同业务配置独立的线程池
  2. 核心业务使用专用线程池
  3. 非核心业务使用独立线程池
  4. 实现业务隔离,互不影响

步骤1: 同时提交快速任务和慢任务到隔离线程池
  快速任务线程池大小: 10
  慢任务线程池大小: 3

  快速任务0 开始执行,等待时间: 5ms
  快速任务1 开始执行,等待时间: 6ms
  ...
  快速任务9 开始执行,等待时间: 10ms   ← 全部快速执行

步骤2: 快速任务执行完成
  快速任务执行时间: 15ms

步骤3: 慢任务执行完成
  总执行时间: 2015ms

分析:
  ✅ 快速任务没有被慢任务阻塞
  ✅ 业务之间相互隔离
  ✅ 核心业务响应时间可控

优点:
  - 业务隔离,互不影响
  - 核心业务稳定性有保障
  - 可以针对不同业务优化配置

缺点:
  - 配置复杂度增加
  - 资源占用可能增加

=== 方案三:完善的线程池配置 ===

========================================
完善的线程池配置演示
========================================

方案说明:
  1. 合理配置核心参数
  2. 配置拒绝策略和异常处理
  3. 配置线程池监控
  4. 优雅关闭

步骤1: 提交30个任务
  线程池核心大小: 5
  线程池最大大小: 10
  队列容量: 20
  拒绝策略: callerrunspolicy(调用者运行)

  任务25 由调用者线程执行: main
  任务26 由调用者线程执行: main
  ...

步骤2: 分析结果
  成功执行: 30
  被拒绝: 0

步骤3: 线程池状态
  活跃线程数: 5
  核心线程数: 5
  最大线程数: 10
  队列大小: 20

分析:
  ✅ 使用callerrunspolicy,任务不会被丢弃
  ✅ 超出容量时由调用者线程执行,实现背压
  ✅ 可以监控线程池状态

优点:
  - 任务不会丢失
  - 自动实现背压控制
  - 可监控可管理

缺点:
  - 调用者线程可能被阻塞
  - 需要根据业务调整配置

4. 架构思考

4.1 线程池参数配置建议

参数建议值说明
corepoolsizecpu核心数 ~ cpu核心数*2cpu密集型取小,io密集型取大
maxpoolsizecorepoolsize * 2根据业务峰值调整
queuecapacity100-500过大会导致响应延迟
keepaliveseconds60空闲线程存活时间
rejectedexecutionhandlercallerrunspolicy推荐使用调用者运行策略

4.2 拒绝策略选择

策略行为适用场景
abortpolicy抛出异常需要感知任务失败的场景
callerrunspolicy调用者线程执行不允许丢失任务的场景
discardpolicy直接丢弃允许丢失任务的场景
discardoldestpolicy丢弃最老任务允许丢失旧任务的场景

4.3 最佳实践总结

代码层面:

  • ✅ 必须配置自定义线程池
  • ✅ 为不同业务配置独立线程池
  • ✅ 配置合理的拒绝策略
  • ✅ 实现asyncuncaughtexceptionhandler处理异常
  • ❌ 不要使用默认的simpleasynctaskexecutor
  • ❌ 不要让多个业务共用一个线程池

团队规范:

  • 强制规范:所有@async必须指定线程池
  • 代码审查:重点检查线程池配置
  • 监控告警:监控线程池状态和队列积压
  • 文档说明:在代码注释中说明线程池配置原因

架构设计:

  • 线程池隔离:核心业务与非核心业务隔离
  • 监控体系:接入prometheus等监控系统
  • 容量规划:根据业务峰值规划线程池容量
  • 应急预案:准备线程池耗尽时的降级方案

4.4 监控指标

建议监控以下线程池指标:

1. 活跃线程数(activecount)
2. 核心线程数(corepoolsize)
3. 最大线程数(maxpoolsize)
4. 队列大小(queuesize)
5. 已完成任务数(completedtaskcount)
6. 拒绝任务数(rejectedtaskcount)

通过深入理解@async注解的线程池机制,不仅能避免生产环境的oom和性能问题,更能提升对异步编程和线程池管理的整体思考。在实际项目中,正确配置线程池至关重要,唯有深入理解底层原理,才能构建真正稳定可靠的异步处理系统。

到此这篇关于java中@async注解的线程池隔离陷阱的文章就介绍到这了,更多相关java @async线程池隔离内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com