1.启动类添加支持异步调用的注解
package com.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.springboot.mapper")
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.线程池配置
package com.springboot.threadpool;
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;
@Configuration
@EnableAsync
public class ThreadPoolConfig {
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int COUR_SIZE = CPU_COUNT * 2;
private static final int MAX_COUR_SIZE = CPU_COUNT * 4;
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(COUR_SIZE);
threadPoolTaskExecutor.setMaxPoolSize(MAX_COUR_SIZE);
threadPoolTaskExecutor.setQueueCapacity(MAX_COUR_SIZE * 4);
threadPoolTaskExecutor.setThreadNamePrefix("ThreadPoolTaskExecutor-");
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return threadPoolTaskExecutor;
}
}
3.具体使用如下
package com.springboot.threadpool;
public interface TestService {
void testService();
}
package com.springboot.threadpool;
import com.springboot.mapper.AdminMapper;
import com.springboot.mapper.EmployeeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl implements TestService {
private static final Logger logger = LoggerFactory.getLogger("TestService");
@Autowired
private EmployeeMapper employeeMapper;
@Autowired
private AdminMapper adminMapper;
@Override
@Async("threadPoolTaskExecutor")
public void testService() {
logger.info("start service");
try {
Thread.sleep(100000);
logger.info("怒发冲冠,凭栏处,潇潇雨歇,抬望眼,仰天长啸,壮怀激烈,三十功名尘与土,八千里路云和月,莫等闲,白了少年头,空悲切。" +
"靖康耻,犹未雪,臣子恨,何时灭,驾长车,踏破贺兰山缺,壮志饥餐胡虏肉,笑谈渴饮匈奴血,待从头,收拾旧山河,朝天阙");
logger.info("");
int t=5/0;
employeeMapper.getAllEpmloyee(null);
adminMapper.getAllAdmin(null);
} catch (Exception e) {
e.printStackTrace();
}
logger.info("end service");
}
}
package com.springboot.threadpool;
import com.springboot.errutil.ReturnMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class ThreadPoolController {
private final Logger logger = LoggerFactory.getLogger("ThreadPoolController");
@Resource
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Autowired
private TestService testService;
@GetMapping("/thread")
public ReturnMsg testThread() {
threadPoolTaskExecutor.execute(() -> {
try {
Thread.sleep(10000);
logger.info("执行线程池任务");
logger.info(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
logger.info("主线程名称:{}", Thread.currentThread().getName());
return new ReturnMsg(true, "0000", Thread.currentThread().getName(), Thread.currentThread().getName());
}
@GetMapping("/threadTervice")
public ReturnMsg threadTervice() throws InterruptedException {
logger.info("start controller");
long beginTime=System.currentTimeMillis();
testService.testService();
logger.info("end controller");
long endTIme = System.currentTimeMillis();
logger.info("threadTervice执行了{}ms",endTIme-beginTime);
return new ReturnMsg(true, "0000", Thread.currentThread().getName(), Thread.currentThread().getName());
}
}