@Async

首先我们需要了解其规则——@Async有两个限制:

  • 必须仅应用在public方法上
  • 自执行——从相同类中调用异步方法不起作用。

原因很简单——public方法能够被代理,自执行不工作是因为它绕过代理并直接调用底层方法。

 

  • 处理调整报
    
    Executor [java.util.concurrent.ThreadPoolExecutor@2b045d98[Running, pool size = 200, active threads = 1, queued tasks = 420, completed tasks = 244874312]] did not accept task: org.springframework.aop.interceptor.AsyncExecutionInterceptor$1@4f9ae398
  •  
@Configuration
@ComponentScan("com.xxx.xxx.biz.*.*Service")
@EnableAsync
public class ThreadConfig extends AsyncConfigurerSupport{
  
     @Override
     public ThreadPoolTaskExecutor getAsyncExecutor() {
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
          // 核心池的大小(即线程池中的线程数目大于这个参数时,提交的任务会被放进任务缓存队列)
          executor.setCorePoolSize(50);
          // 线程池最大能容忍的线程数
          executor.setMaxPoolSize(500);
          // 任务缓存队列,用来存放等待执行的任务
          executor.setQueueCapacity(800);
          executor.setWaitForTasksToCompleteOnShutdown(true);
          // 线程池维护线程所允许的空闲时间
          executor.setAwaitTerminationSeconds(60 * 30);
          executor.setThreadNamePrefix("xxx-xxx-thread-");
          // 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
          executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
          executor.initialize();  
          return executor;  
     }

     @Override
     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
          return new AsyncUncaughtExceptionHandler() {
               @Override
               public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
                    log.warn("ThreadConfig处理异步常异-->{},{}",method.getName(),objects,throwable);
               }
          };
     }
}
在ThreadPoolExecutor中表现为:

如果当前运行的线程数小于corePoolSize,那么就创建线程来执行任务(执行时需要获取全局锁)。
如果运行的线程大于或等于corePoolSize,那么就把task加入BlockQueue。
如果创建的线程数量大于BlockQueue的最大容量,那么创建新线程来执行该任务。
如果创建线程导致当前运行的线程数超过maximumPoolSize,就根据饱和策略来拒绝该任务。

 

GitHub 加速计划 / th / ThreadPool
8
0
下载
A simple C++11 Thread Pool implementation
最近提交(Master分支:4 个月前 )
9a42ec13 - 11 年前
fcc91415 - 11 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐