- 使用多线程来实现异步操作,所有的解释可以看我的@Async实现异步操作
- 多线程实现异步操作其实跟@Async实现异步操作是一样的,只需要改几行代码就行了
- 自定义线程池
package com.zhou.demo.threadpool;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executor;
/**
* @author Mr.zhou
* 定义线程池
*/
@Component
@EnableAsync
public class ThreadExecutorConfig {
private static final int COREPOOLSIZE = 10;
private static final int MAXPOOLSIZE = 200;
private static final int QUEUECAPACITY = 10;
@Bean
public static Executor smartExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(COREPOOLSIZE); // 线程池的核心大小
executor.setMaxPoolSize(MAXPOOLSIZE); // 线程池的最大数量
executor.setQueueCapacity(QUEUECAPACITY);
executor.setThreadNamePrefix("smartExecutor"); //线程池的名称 可以自己定义
executor.initialize();
return executor;
}
}
- 需要执行的任务
package com.zhou.demo.threadpool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.Random;
/**
* @author Mr.zhou
*/
// Component 将这个类加载到spring容器当中 Slf4j是springboot自带的日志框架
@Service
@Slf4j
public class AsyncTask {
// 生成一个随机数
private static Random random = new Random();
public void task() throws InterruptedException {
System.out.println("开始做任务一");
long start = System.currentTimeMillis();
// 让这个线程睡眠,让测试效果更加的明显
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");
}
public void taskAsync1() throws Exception {
// 让这个线程睡眠,让测试效果更加的明显
Thread.sleep(1000);
for (int i = 0; i < 10; i++) {
log.info("##########################" + i);
}
}
}
- 接口调用
package com.zhou.demo.threadpool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author Mr.zhou
*/
@RestController
@RequestMapping("api/task")
@Slf4j
public class TaskController{
/**
* 这里使用构造函数注入,spring官方文档推荐的
* 也可以用注解的形式注入
*/
private AsyncTask asyncTask;
public TaskController(AsyncTask asyncTask) {
this.asyncTask = asyncTask;
}
@RequestMapping(value = "/async", method = RequestMethod.GET)
public String task1() throws Exception {
log.info("test async................");
Executor executor = ThreadExecutorConfig.smartExecutor();
executor.execute(() -> {
try {
asyncTask.taskAsync1();
} catch (Exception e) {
e.printStackTrace();
}
});
executor.execute(() -> {
try {
asyncTask.task();
} catch (Exception e) {
e.printStackTrace();
}
;
});
return "执行成功";
}
}