Java教程

Java - SpringBoot异步线程池 ThreadPoolTaskExecutor

本文主要是介绍Java - SpringBoot异步线程池 ThreadPoolTaskExecutor,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、配置线程池

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ExecutorConfig {

    @Bean
    public Executor asyncServiceExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);// 配置核心线程数
        executor.setMaxPoolSize(10);// 配置最大线程数
        executor.setQueueCapacity(100);// 配置队列大小
        executor.setThreadNamePrefix("async-service-");// 配置线程池中的线程的名称前缀
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());// 设置拒绝策略
        executor.initialize();// 执行初始化
        return executor;
    }

}

*ThreadPoolExecutor的拒绝策略: 当线程池已经达到maxPoolSize的时候, 如何处理新任务.

  1. AbortPolicy: 丢弃任务并抛出RejectedExecutionException异常
  2. DiscardPolicy: 丢弃任务, 但是不抛出异常
  3. DiscardOldestPolicy: 丢弃队列最前面的任务, 然后重新提交被拒绝的任务
  4. CallerRunsPolicy: 由调用线程(提交任务的线程)处理该任务

二、构建异步线程业务层

*AsyncService

import java.util.concurrent.Future;

public interface AsyncService {

    Future<String> getResult();

	void doSomething();

}

*AsyncServiceImpl

import com.lky.service.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

@Component
@Async
public class AsyncServiceImpl implements AsyncService {

    @Override
    public Future<String> getResult() {
    	// 业务处理代码...
        return new AsyncResult<>("result");
    }
    
	@Override
    public void doSomething() {
		// 业务处理代码...
		System.out.println("do something");
    }

}
  • @Async注解, 用在类上表示该类所有方法均为异步, 用在方法上表示仅该方法为异步.

三、调用异步线程

import com.lky.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Controller
@EnableAsync
public class AsyncController {

    @Autowired
    private AsyncService asyncService;

    @RequestMapping("/async")
    @ResponseBody
    public Map<String, String> async(@RequestBody Map<String, String> params) throws ExecutionException, InterruptedException {
        Future<String> asyncResult = asyncService.getResult();
        System.out.println("asyncResult = " + asyncResult.get());
        return null;
    }

}
  • 若异步线程任务有返回值, 当使用get方法获取返回值时会造成线程阻塞, 从而导致异步变同步.
  • @EnableAsync注解, 若当前类需要使用异步线程任务, 必须在类上使用该注解.
这篇关于Java - SpringBoot异步线程池 ThreadPoolTaskExecutor的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!