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();
    }
}

 

 

posted @ 2021-06-17 17:54  圣金巫灵  阅读(41)  评论(0编辑  收藏  举报