其他分享
首页 > 其他分享> > FutureTask

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

 

标签:task1,task2,executed,FutureTask,new,es
来源: https://www.cnblogs.com/dayanjing/p/14894689.html