package com.ruoyi.demo.controller.test;


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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/thread")
public class ThreadTestController {

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @GetMapping("/test1")
    public void test() {
        for (int i = 0; i < 100; i++) {
            int finalI = i;
            threadPoolTaskExecutor.execute(() -> {
                System.out.println(finalI + "");
            });
        }

    }


    @GetMapping("/test2")
    public void test2() {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            return "1";
        }, threadPoolTaskExecutor);

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            return "2";
        }, threadPoolTaskExecutor);

        try {
            CompletableFuture.allOf(
                future1,
                future2
            ).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }


    }
}
