FutureTask

Demo:

public class FutureTaskTest {
    public static void main(String[] args) throws Exception{
        // 1. 使用线程
        FutureTask task1 = new FutureTask(()-> {
            return "task1 executed";
        });
        new Thread(task1).start();
        System.out.println(task1.get());

        // 2. 使用线程池
        FutureTask task2 = new FutureTask(()-> {
            return "task2 executed";
        });
        ExecutorService es = Executors.newFixedThreadPool(1);
        es.execute(task2);
        System.out.println(task2.get());
        es.shutdown();
    }
}

FutureTask

 

上一篇:python学习日记(面向对象——组合)


下一篇:李宏毅机器学习-Task2