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