concurrent (八) Future
作用:
接受多线程的执行结果
全路径:
java.util.concurrent
声明:
public interface Future<V>
类图结构:

方法
boolean cancel(boolean mayInterruptIfRunning);//方法可以用来停止一个任务,如果任务可以停止(通过mayInterruptIfRunning来进行判断),则可以返回true,如果任务已经完成或者已经停止,或者这个任务无法停止,则会返回false.
boolean isCancelled(); //判断当前方法是否取消
boolean isDone(); //判断当前方法是否完成
V get() throws InterruptedException, ExecutionException; // 方法可以当任务结束后返回一个结果,如果调用时,工作还没有结束,则会阻塞线程,直到任务执行完毕
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException; //做多等待timeout的时间就会返回结果
举个例子
Callable callable=new Callable() {
@Override
public String call() throws Exception {
Thread.sleep(1000*5);
return "233";
}
};
FutureTask<String> ft=new FutureTask<>(callable);
new Thread(ft).start();
System.out.println(ft.get());

浙公网安备 33010602011771号