asp 免费网站模板,crm管理系统架构,云主机和云服务器有什么区别,广东省自然资源厅测绘院为什么使用线程池#xff1f;
降低系统资源消耗#xff0c;通过重用已存在的线程#xff0c;降低线程创建和销毁造成的消耗#xff1b;提高系统响应速度#xff0c;当有任务到达时#xff0c;通过复用已存在的线程#xff0c;无需等待新线程的创建便能立即执行#xf…为什么使用线程池
降低系统资源消耗通过重用已存在的线程降低线程创建和销毁造成的消耗提高系统响应速度当有任务到达时通过复用已存在的线程无需等待新线程的创建便能立即执行方便线程并发数的管控因为线程若是无限制的创建可能会导致内存占用过多而产生OOM并且会造成cpu过度切换cpu切换线程是有时间成本的需要保持当前执行线程的现场并恢复要执行线程的现场提供更强大的功能延时定时线程池
参考博客https://blog.csdn.net/u012060033/article/details/111934507 简单使用 参考博客https://blog.csdn.net/weixin_45866737/article/details/122539694
创建线程池
MyThreadPool .java
Configuration
public class MyThreadPool {//ThreadPoolTaskExecutor不会自动创建ThreadPoolExecutor需要手动调initialize才会创建。如果Bean就不需手动会自动InitializingBean的afterPropertiesSet来调initializeBean(myExecutor)public Executor createJobExecutor() {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();// 线程池活跃的线程数executor.setCorePoolSize(20);// 设置线程队列最大线程数executor.setMaxPoolSize(40);// 设置等待队列大小executor.setQueueCapacity(200);// 线程池维护线程所允许的空闲时间executor.setKeepAliveSeconds(60);// 线程前缀名称executor.setThreadNamePrefix(myExecutor---: );executor.initialize();return executor;}
}service层
Service
public class StudentServiceImpl implements StudentService {OverrideAsync(myExecutor)public FutureStudentVo toVo(Student student) {StudentVo studentVo StudentMapStruct.INSTANCE.studentToVo(student);// 业务操作return new AsyncResult(studentVo);}
}
controller层
Api(tags 学生实体类转vo接口)
RestController
RequestMapping(value /trans)
public class StudentController {AutowiredStudentServiceImpl studentService;ResponseBodyPostMapping(/students)ApiOperation(value 测试接口)public ResponseEntityStudentResponse testStudent(ApiParam(学生请求对象实体类) RequestBody Student student){FutureStudentVo studentVo studentService.toVo(student);while (studentVo.isDone()) {break;}StudentResponse studentResponse StudentMapStruct.INSTANCE.voToResponse(studentVo.get());return new ResponseEntity(studentResponse, HttpStatus.OK);}
}