0、前言
当我们需要实现并发、异步等操作时,通常都会使用到ThreadPoolTaskExecutor。它是springcore包中的,而ThreadPoolExecutor是JDK中的JUC。ThreadPoolTaskExecutor是对ThreadPoolExecutor进行了封装处理。
1、示例
1.1、配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@EnableAsync
@Configuration
public class ExecutorConfig {
@Bean(name = "asyncServiceExecutor")
public Executor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//设置核心线程数
executor.setCorePoolSize(10);
//设置最大线程数
executor.setMaxPoolSize(30);
//设置队列大小
executor.setQueueCapacity(50);
//配置线程池的前缀
executor.setThreadNamePrefix("async-service-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//设置空闲时间
executor.setKeepAliveSeconds(60);
//进行加载
executor.initialize();
return executor;
}
}
1.2、应用
1.2.0 需异步的方法
import com.baomidou.mybatisplus.extension.service.IService;
import com.chengd.pojo.GeneCase;
public interface GeneCaseService extends IService<GeneCase>
{
void testThread();
}
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengd.mapper.GeneCaseMapper;
import com.chengd.pojo.GeneCase;
import com.chengd.service.GeneCaseService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class GeneCaseServiceImpl extends ServiceImpl<GeneCaseMapper, GeneCase> implements GeneCaseService {
@Override
@Async
public void testThread() {
for (int i = 0; i < 200; i++) {
System.out.println("副线程名称 " + Thread.currentThread().getName() + " === " + i);
}
}
}
1.2.1 Controller
import com.chengd.common.Result;
import com.chengd.service.TestService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/test")
@Api(tags = "测试ThreadPoolTaskExecutor")
public class TestController {
@Resource
private TestService testService;
@GetMapping("/thread")
public Result<Object> testThread() throws InterruptedException {
testService.testThread();
return Result.succeed("请求成功");
}
}
1.2.2 Service
public interface TestService {
/**
* 测试Thread
*/
void testThread() throws InterruptedException;
}
import com.chengd.service.GeneCaseService;
import com.chengd.service.TestService;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class TestServiceImpl implements TestService {
@Resource
private GeneCaseService geneCaseService;
@Resource(name = "asyncServiceExecutor")
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Override
public void testThread() throws InterruptedException {
threadPoolTaskExecutor.submit(() -> geneCaseService.testThread());
threadPoolTaskExecutor.submit(() -> geneCaseService.testThread());
for (int i = 0; i < 200; i++) {
System.out.println("主线程名称 " + Thread.currentThread().getName() + " === " + i);
Thread.sleep(10);
}
}
}
1.3、测试结果
注意事项:
配置类的 @EnableAsync
需异步方法的 @Async