标签:port over href executors 查询 imp span 异步 参数
1.可取消的异步计算,FutureTask实现了Future的基本方法,提供了start、cancel 操作,可以查询计算是否完成,并且可以获取计算
的结果。结果只可以计算完成之后去获取,get方法会阻塞当前计算没有完成的线程,一定计算完成则会立即释放。
1.submit()可以传入参数为实现callable接口的实例,返回future实例对象。
2.execute()返回void。
package demo2;import java.util.concurrent.*;/*** Created by liudan on 2017/7/31.*/public class DemoFuture implements Callable<String> {public String name;public DemoFuture(String name) {this.name = name;}@Overridepublic String call() throws Exception {Thread.sleep(3000);String r = this.name+" 任务处理 success";return r;}public static void main(String[] args) throws ExecutionException, InterruptedException {FutureTask<String> futureTask1 = new FutureTask<String>(new DemoFuture("订单1"));FutureTask<String> futureTask2 = new FutureTask<String>(new DemoFuture("订单2"));ExecutorService executorService = Executors.newFixedThreadPool(2);Future future1 = executorService.submit(futureTask1);Future future2 = executorService.submit(futureTask2);System.out.println("请求完毕...");try {System.out.println("处理中...");Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}System.err.println("over:"+futureTask1.get().toString());System.err.println("over:"+futureTask2.get().toString());//futureTask1.get()方法,假如get不到,则一直等待阻塞自己的线程。executorService.shutdown();}}输出:请求完毕... 处理中... over:订单1 任务处理 success over:订单2 任务处理 success
标签:port over href executors 查询 imp span 异步 参数
原文地址:http://www.cnblogs.com/xxt19970908/p/7302395.html