package org.algorithm.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.*;

/**
 * @Author: bijl
 * @Date: 2019/6/13 10:32
 * @Decription: 多线程测试
 */
public class MultiThreadsTest {
    public static void main(String[] args) {
        ExecutorService exe = Executors.newCachedThreadPool();
//        for (int i = 0; i < 10; i++)
//            exe.execute(new MyThread());
        List<Future<Float>> futures = new ArrayList<>();
        Future<Float> future = null;
        for(int j=0; j<2;j ++){
            for (int i = 0; i < 10; i++) {
                future = exe.submit(new MyCallable("" + i));
                System.out.println("............_" + i);
                futures.add(future);

//                try {
//                    System.out.println(future.get());
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                } catch (ExecutionException e) {
//                    e.printStackTrace();
//                }

            }
            for (Future<Float> future1:futures){
                try {
                    System.out.println(future1.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
//            exe.shutdown();
        }
        System.out.println("All done.");
    }
}

class MyThread implements Runnable {


    public void run() {
        System.out.println(Thread.currentThread());
        System.out.println("............");
    }
}

class MyCallable implements Callable<Float> {
    private String values = null;

    public MyCallable(String values) {
        this.values = values;
    }

    @Override
    public Float call() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Float.parseFloat(this.values);
    }
}