package com.zksy.business.dzgwcs.dating;


import com.zksy.common.config.ThreadPoolConfig;
import com.zksy.common.utils.ThreadPoolRunTaskListUtil;
import com.zksy.zksyApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@SpringBootTest(classes = zksyApplication.class)
@RunWith(SpringRunner.class)
public class MyTest {

    @Value("${system.ghQuestionnaire}")
    private Boolean ghQuestionnaire;

    public static void main(String[] args) {

    }

    @Test
    public void fadfafdasf() {
        System.out.println(123);
    }

    @Autowired
    private ThreadPoolConfig poolConfig;

    public interface CallableResult<T> extends Callable<T> {
    }
    @Test
    public void testUtil() {
        ThreadPoolRunTaskListUtil<String> util = new ThreadPoolRunTaskListUtil(poolConfig,() -> {
            try {
                Thread.sleep(2000);
                System.out.println(111);
                return "111";
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, () -> {
            try {
                Thread.sleep(2000);
                System.out.println(111);
                return "111";
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, () -> {
            try {
                Thread.sleep(1000);
                System.out.println(222);
                return "222";
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
//        Vector resultVector = util.runTaskList().getResultVector();
        Vector vector = util.runTaskList(Vector::new).getResultVector();
    }
    @Test
    public void test() {
        CallableResult<Integer> doSomething = () -> {
            try {
                Thread.sleep(2000);
                System.out.println(111);
                return 111;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        };
        CallableResult<Integer> doSomething2 = () -> {
            try {
                Thread.sleep(1000);
                System.out.println(222);
                return 222;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        };

        CallableResult<Integer> doSomething3 = () -> {
            try {
                Thread.sleep(3000);
                System.out.println(333);
                return 333;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        };
        List<Future<Integer>> futures = new ArrayList<>();

        ArrayList<CallableResult<Integer>> list = new ArrayList<>();
        list.add(doSomething);
        list.add(doSomething2);
        list.add(doSomething3);
        list.forEach((callableResult)->{
            futures.add(poolConfig.threadPoolTaskExecutor().submit(callableResult));
        });

        Vector<Integer> resVector = new Vector<>();
        // 等待所有任务完成
        for (Future<Integer> future : futures) {
            try {
                Integer result = future.get(); // 这里会阻塞，直到得到结果
                resVector.add(result);
//                System.out.println("Received result: " + result);
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
        resVector.forEach(System.out::println);
    }

}
