package chartone; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** * 并发实现-Callable/Future 实现返回值控制的线程 * * @author suntao * @history 2020-02-04 16:50 suntao 新建 * @since JDK1.8 */ public class CallableDemo implements Callable<String> { public static void main(String[] args) throws Exception { // 创建线程池执行服务 ExecutorService excute = Executors.newCachedThreadPool(); CallableDemo cd = new CallableDemo(); Future<String> fs = excute.submit(cd); // 可以执行其他业务逻辑 for (int i = 0; i <100 ; i++) { System.out.println("mianTread = [" + i + "]"); } // 阻塞操作,只有执行完之后才会进行接下来的操作 String result = fs.get(); System.out.println(result); excute.shutdown(); } @Override public String call() throws Exception { for (int i = 0; i <1000 ; i++) { System.out.println(i); } return "我是被调用后的返回值"; } }
原文地址:https://www.cnblogs.com/xiaotao726/p/12267608.html
时间: 2024-10-12 18:04:38