Day301
存储器,用来存储 call()方法的结果
让操作进行
异步执行
辅助 Callable,获取到返回值
判断任务是否执行完毕
取消任务
限时获取任务结果
[](()2、Callable 和 Future 的关系
操作获取 Callable 结果任务
[](()3、主要方法
get(): 获取结果-----有 5 种情况
情况 1:
任务正常
完成
:
立刻返回结果
情况 2:
任务尚
未完成(还没开始或进行中)
:
阻塞直到任务完成
情况 3:
任务执行过程中
抛出异常
:
_也抛出异常_ExecutionException
情况 4:
任务被
取消
:
抛出取消异常 CancellationException
情况 5:
任务
超时
:
抛出超时异常 TimeoutException
cancel(): 取消任务的执行
isDown(): 判断线程是否执行完毕
只返回这个任务是否执行完毕,就算任务执行失败,也返回 true
usCancelled(): 判断是否被取消
[](()4、Future 代码演示
[](()①线程池的 submit 方法返回 Future 对象
/******
@author 阿昌
@create 2021-06-17 20:52
*/
public class OneFuture {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(10);
Future<Integer> future = pool.submit(new CallableTask());
try {
System.out.println(future.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
pool.shutdown();
}
static class CallableTask implements Callable<Integer>{
@Override
public Integer call() throws Exception {
Thread.sleep(3000);
return new Random().nextInt();
}
}
}
[](()②Callable 的 Lambda 表达式写法
public class OneFutureLambda {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(10);
//lambda 写法
Callable<Integer> callable = ()->{
Thread.sleep(3000);
return new Random().nextInt();
};
Future<Integer> future = pool.submit(callable);
try {
System.out.println(future.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
pool.shutdown();
}
}
[](()③多个任务,用 Future 数组来获取结果
/******
@author 阿昌
@create 2021-06-17 21:04
*/
public class MultiFuture {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
ArrayList<Future<Integer>> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Future<Integer> future = pool.submit(new CallableTask());
list.add(future);
}
for (Future<Integer> future : list) {
try {
System.out.println(future.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
pool.shutdown();
}
static class CallableTask implements Callable<Integer> {
@Override
public Integer call() throws Exception {
Thread.sleep(3000);
return new Random().nextInt();
}
}
}
[](()④任务执行过程中抛出 Exception 和 isDone
/******
@author 阿昌
@create 2021-06-17 21:16
*/
public class GetException {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(20);
Future<Integer> future = pool.submit(new CallableTask());
try {
for (int i = 0; i < 5; i++) {
System.out.println(i);
Thread.s 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 leep(500);//
}
System.out.println(future.isDone());//检查是否完成
future.get();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("抛出 InterruptedException 异常");
} catch (ExecutionException e) {
e.printStackTrace();
System.out.println("抛出 ExecutionException 异常");
}
pool.shutdown();
}
static class CallableTask implements Callable<Integer> {
@Override
public Integer call() throws Exception {
throw new RuntimeException("callable 抛出异常");
}
}
}
[](()⑤获取任务超时,get()重载方法与 cancel()取消方法
当 cancel()传入 true 后,他会发出给子线程中断信号,但不会接收子线程返回的值
/******
@author 阿昌
@create 2021-06-17 21:25
*/
public class Timeout {
private static final Ad DEFAULT_AD = new Ad("无网络时,默认的广告");
private static final ExecutorService pool = Executors.newFixedThreadPool(10);
public static void main(String[] args) {
Timeout timeout = new Timeout();
timeout.printAd();
}
//打印广告方法
public void printAd() {
Future<Ad> future = pool.submit(new FetchAdTask());
Ad ad;
try {
ad = future.get(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
ad = new Ad("被中断的默认广告");
} catch (ExecutionException e) {
ad = new Ad("抛异常的默认广告");
} catch (TimeoutException e) {
ad = new Ad("被超时的默认广告");
System.out.println("未获取到广告");
boolean flag = future.cancel(true);
System.out.println("cancel 的结果:"+flag);
pool.shutdown();
System.out.println(ad);
评论