ThreadPoolTaskExecutor中的线程都是守护线程,用户线程执行完毕,杀死所有守护线程
ThreadPoolTaskExecutor对象调用shutdown()方法时是否等待任务执行完毕再尝试shutdown()
// true-等待正在执行任务的线程执行完毕再尝试shutdown(),false-不等待,直接shutdown(),以抛出java.lang.InterruptedException异常的方式shutdown
// true-类似java线程池的shutdown(), false-类似java线程池的shutdownNow()
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
@Async使用注意
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.xcrjgroupId>
<artifactId>springbootbasicartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>2.7.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>2.7.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>RELEASEversion>
<scope>testscope>
dependency>
dependencies>
project>
main @EnableAsync
package com.xcrj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
// 开启异步
@EnableAsync
@SpringBootApplication
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class, args);
System.out.println("启动");
}
}
configuration ThreadPoolTaskExecutor
package com.xcrj.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class AsyncMeConfiguration {
// 配置线程池ThreadPoolTaskExecutor threadPoolTaskExecutor01
@Bean("threadPoolTaskExecutor01")
public ThreadPoolTaskExecutor getAsyncExecutor() {
/**
* corePoolSize:核心线程数,线程池维持的线程数
* maximumPoolSize:池中最多线程数
* keepAliveTime:大于核心线程数的线程存活时间
* unit:存活时间单位
* workQueue:任务队列 (BlockingQueue)(queueCapacity > 0 ? new LinkedBlockingQueue(queueCapacity) : new SynchronousQueue());
* threadFactory:线程工厂
* RejectedExecutionHandler:任务拒绝策略处理器
* waitForTasksToCompleteOnShutdown:是否等待正在执行任务的线程执行完毕再尝试shutdown()
*/
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
threadPoolTaskExecutor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2);
threadPoolTaskExecutor.setQueueCapacity(200);
threadPoolTaskExecutor.setKeepAliveSeconds(10);
threadPoolTaskExecutor.setThreadNamePrefix("asyncMe");
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// true-等待正在执行任务的线程执行完毕再尝试shutdown(),false-不等待,直接shutdown(),以抛出java.lang.InterruptedException异常的方式shutdown
// true-类似java线程池的shutdown(), false-类似java线程池的shutdownNow()
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
}
service方法 @Async
package com.xcrj.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncMeService {
// 使用threadPoolTaskExecutor01线程池中的线程异步执行service1
@Async("threadPoolTaskExecutor01")
public void service1() throws InterruptedException {
System.out.println("开始服务1");
// 执行任务耗时
Thread.sleep(5000);
System.out.println("结束服务1");
}
// 使用threadPoolTaskExecutor01线程池中的线程异步执行service2
@Async("threadPoolTaskExecutor01")
public void service2() throws InterruptedException {
System.out.println("开始服务2");
// 执行任务耗时
Thread.sleep(2000);
System.out.println("结束服务2");
}
}
test
package com.xcrj.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestAsyncMeService {
@Resource
private AsyncMeService asyncMeService;
@Resource(name = "threadPoolTaskExecutor01")
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Test
public void testAsync() throws InterruptedException {
asyncMeService.service1();
asyncMeService.service2();
threadPoolTaskExecutor.shutdown();
// ThreadPoolTaskExecutor中的线程都是守护线程,用户线程执行完毕,杀死所有守护线程
Thread.sleep(10000);
System.out.println("结束test线程");
}
}