ThreadPoolTaskExecutor executorService = ContextUtils.getBean("threadPoolTaskExecutor", ThreadPoolTaskExecutor.class);
executorService.execute(() -> {
Date nowDate = new Date();
PaymentOrderDO record = BeanUtils.copyProperties(request, PaymentOrderDO.class);
record.setOrderType(0); //订单类型 0:线上充值 1:线下充值
record.setOrderNo(Long.valueOf(request.getOrderNo()));
record.setPaywayId(paymentPaywayDO.getPaywayId()); //支付方式ID
record.setStatus(OrderStatus.UNPAID.getStatus()); //订单状态:0000:交易成功(审核成功) 1111:待支付(待审核) 2222:失败
record.setPaywayName(paymentPaywayDO.getPaywayName()); //支付名称
record.setCreateTime(nowDate);
record.setUpdateTime(nowDate);
record.setPostscript(Integer.valueOf(GenerateNoUtil.randomNum(6))); //6位数字方便查询
paymentOrderMapper.insertSelective(record);
});
工具类
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author lisi
* @date 2019/9/11 15:18
*/
@Component
public class ContextUtils implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return context;
}
public static <T> T getBean(String beanName, Class<T> c) {
return context.getBean(c);
}
}
ThreadPoolTaskExecutor executorService = ContextUtils.getBean("threadPoolTaskExecutor", ThreadPoolTaskExecutor.class);
@RequestMapping(value="/multThread",method = RequestMethod.GET)
@ResponseBody
public String multThread(String index){
CountDownLatch countDownLatch = new CountDownLatch(4);
for(int i = 0; i< 4; i++){
executorService.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println("线程--> " + index + " 开始");
Thread.sleep(6000);
System.out.println("线程--> " + index + " 结束");
} catch (Exception e) {
e.printStackTrace();
} finally {
countDownLatch.countDown(); // 这个不管是否异常都需要数量减,否则会被堵塞无法结束
}
}
});
}
try {
countDownLatch.await();
System.out.println(index + "所有线程结束");
} catch (Exception e) {
System.out.println("阻塞异常");
}
return index;
}
@RequestMapping(value="/test",method = RequestMethod.GET)
@ResponseBody
public void test (){
Future<Integer> recharge1 = executorService.submit(() -> getTotal1());
Future<Integer> recharge2 = executorService.submit(() -> getTotal2());
try {
System.out.println(recharge1.get(3, TimeUnit.SECONDS));
System.out.println(recharge2.get(3, TimeUnit.SECONDS));
} catch (Exception e) {
e.printStackTrace();
}
}
public Integer getTotal1(){
try {
System.out.println("开始getTotal1");
Thread.sleep(3000);
System.out.println("结束getTotal1");
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
}
public Integer getTotal2(){
try {
System.out.println("开始getTotal2");
Thread.sleep(4000);
System.out.println("结束getTotal2");
} catch (InterruptedException e) {
e.printStackTrace();
}
return 3;
}
public static void main(String[] args) throws Exception {
//线程池
ThreadPoolTaskExecutor executorService = new ThreadPoolTaskExecutor();
executorService.setCorePoolSize(10);
executorService.initialize(); //必须初始化,
for(int i = 0; i<10; i++) {
final int j = i;
Runnable run = new Runnable() {
@Override
public void run() {
System.out.println("开始执行了" + j);
if(j == 4 || j == 6){
try {
Thread.sleep(4000);
System.out.println("休眠" + j);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("结束执行了" + j);
}
};
executorService.execute(run);
}
executorService.shutdown();
}
打印结果
开始执行了0
开始执行了2
结束执行了2
开始执行了1
结束执行了1
开始执行了3
开始执行了6
开始执行了5
结束执行了0
开始执行了4
开始执行了8
结束执行了8
开始执行了7
结束执行了7
结束执行了5
结束执行了3
17:21:26.814 [main] INFO org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Shutting down ExecutorService
开始执行了9
结束执行了9
结束执行了6
java.lang.InterruptedException: sleep interrupted
结束执行了4
@RequestMapping("/uploadVideo/upload")
@ResponseBody
public ResponseMap uploadVideo(@RequestParam(required = false, value = "file") MultipartFile[] files,
HttpServletRequest request) {
ResponseMap responseMap = new ResponseMap();
LogUtil.logInfo("进入uploadVideo方法----》开始上传视频 ");
CountDownLatch latch = new CountDownLatch(files.length);
//线程池
ThreadPoolTaskExecutor executorService = new ThreadPoolTaskExecutor();
executorService.setCorePoolSize(20);
executorService.setMaxPoolSize(50);
executorService.initialize(); //必须初始化
if (null != files) {
for (int i = 0; i < files.length; i++) {
final int j = i;
Runnable run = new Runnable() {
@Override
public void run() {
CourseItemVideo courseItemVideo = new CourseItemVideo();
MultipartFile file = files[j];
// 存储每个视频对应的index唯一标识
System.out.println(file.getOriginalFilename());
courseItemVideo.setVideoName(file.getOriginalFilename());
courseItemVideo.setVideoSize(String.valueOf(file.getSize() / 1024));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd mm:HH:ss");
courseItemVideo.setUploadDate(sdf.format(new Date()));
courseItemVideo.setCreateDate(new Date());
courseItemVideo.setUpdateDate(new Date());
// 上传视频到阿里云
String courseVideoUrl = aliOssServiceApi.uploadCourseVideoBatch(file, request);
LogUtil.logInfo("上传视频返回地址: " + courseVideoUrl);
if (StringUtils.isNotBlank(courseVideoUrl)) {
// 视频完整路径
courseVideoUrl = aliOssServiceApi.getFullPath(BucketEnum.FSSH_VIDEO_COURCE, courseVideoUrl);
courseItemVideo.setVideoUrl(courseVideoUrl);
String videName = courseItemVideo.getVideoName();
courseItemVideo.setVideoName(videName.substring(0,videName.indexOf("~")));
courseItemVideoServiceApi.saveCourseVideo(courseItemVideo);
responseMap.success("上传成功");
}
latch.countDown();
}
};
executorService.execute(run);
}
try {
// 等待所有人任务结束
latch.await();
} catch (Exception e) {
e.printStackTrace();
}
} else {
responseMap.failure("请选择上传文件");
}
return responseMap;
}