package com.green.testlocalhost.controller;
import com.green.testlocalhost.vo.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
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.CompletableFuture;
/**
* @Author zdj
* @Date 2022/10/28 17:11
* @Version 1.0
*/
@Api(tags = "数据处理")
@RestController
@RequestMapping("/dataDetal")
public class DataController {
/**
* spring自带的线程池对象
**/
@Autowired
private ThreadPoolTaskExecutor applicationTaskExecutor;
/**
* 多线程处理1000万条数据
* 开启两个线程处理这1000万条数据
* @return
*/
@RequestMapping(value = "/detalData", method = RequestMethod.GET)
@ApiOperation(value = "多线程处理1000万条数据", notes = "多线程处理1000万条数据", httpMethod = "GET")
public ResultData detalData(){
CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(() -> {
for (int i = 0; i < 100000; i++) {
System.out.println("a-" + i);
}
}, applicationTaskExecutor);
CompletableFuture<Void> completableFuture2 = CompletableFuture.runAsync(() -> {
for (int i = 0; i < 100000; i++) {
System.out.println("w-" + i);
}
}, applicationTaskExecutor);
// 等待这两个线程都执行完
CompletableFuture.allOf(completableFuture1, completableFuture2).get();
return ResultData.ok("处理完成!", null);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137443.html