Maven
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置 ThreadPoolConfig
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.ThreadPoolExecutor;
/**
* @Author 陈文
* @Date 12/29/2023
*/
@EnableAsync
@Configuration
public class ThreadPoolConfig {
// 获取服务器的cpu个数
private static final int CPU_COUNT = 4;
// private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();// 获取cpu个数
private static final int COUR_SIZE = CPU_COUNT * 2;
private static final int MAX_COUR_SIZE = CPU_COUNT * 4;
// 接下来配置一个bean,配置线程池。
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(COUR_SIZE);// 设置核心线程数
threadPoolTaskExecutor.setMaxPoolSize(MAX_COUR_SIZE);// 配置最大线程数
threadPoolTaskExecutor.setKeepAliveSeconds(60);
threadPoolTaskExecutor.setQueueCapacity(MAX_COUR_SIZE * 4);// 配置队列容量(这里设置成最大线程数的四倍)
threadPoolTaskExecutor.setThreadNamePrefix("rec");// 给线程池设置名称
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());// 设置任务的拒绝策略
return threadPoolTaskExecutor;
}
}
测试接口 TestService
/**
* @Author 陈文
* @Date 12/29/2023
*/
public interface TestService {
void test(String info);
}
测试接口实现 TestServiceImpl
import com.zngx.forward.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @Author 陈文
* @Date 12/29/2023
*/
@Slf4j
@Service
public class TestServiceImpl implements TestService {
@Override
@Async("threadPoolTaskExecutor")
public void test(String info) {
String name = Thread.currentThread().getName();
log.info(name+"==="+info);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
log.error("interrupted:"+name+":"+info);
}
}
}
Test测试 TestThread
import com.zngx.forward.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @Author 陈文
* @Date 12/29/2023
*/
@Slf4j
@SpringBootTest
public class TestThread {
@Resource
private ThreadPoolTaskExecutor poolTaskExecutor;
@Resource
private TestService testService;
@Test
public void test(){
int mm = 1;
while (true){
for (int i=0;i<4;i++){
testService.test(mm+"test"+i);
}
ThreadPoolExecutor executorService = poolTaskExecutor.getThreadPoolExecutor();
System.out.println("当前线程核心线程数"+executorService.getCorePoolSize()+",最大线程数:"+executorService.getMaximumPoolSize()+",当前线程池大小:"+executorService.getPoolSize()+"活动线程数:"+executorService.getActiveCount()+",收到任务:"+executorService.getTaskCount()+"完成任务数:"+executorService.getCompletedTaskCount()+"等待任务数:"+executorService.getQueue().size());
mm++;
}
}
}