当前位置:网站首页>JUC-实现Future
JUC-实现Future
2022-06-09 11:27:00 【iiaythi】
JUC-实现Future
Future 为异步执行结果
实现
Future
public interface Future<T> {
T get() throws InterruptedException;
boolean isDone();
}
FutureTask
FutureTask 是 Future 的实现
public class FutureTask<T> implements Future<T> {
private T result;
private Object lock = new Object();
private boolean isDone;
@Override
public T get() throws InterruptedException {
synchronized (lock) {
while (!isDone) {
lock.wait();
}
return result;
}
}
protected void finish(T result) {
synchronized (lock) {
this.result = result;
isDone = true;
lock.notifyAll();
}
}
@Override
public boolean isDone() {
return isDone;
}
}
FutureService
public interface FutureService<IN,OUT> {
Future<?> submit(Runnable task);
Future<OUT> submit(Task<IN, OUT> task, IN in);
static <T,R> FutureService<T,R> newInstance() {
return new FutureServiceImpl();
}
}
FutureServiceImpl
public class FutureServiceImpl<IN,OUT> implements FutureService<IN, OUT> {
@Override
public Future<?> submit(Runnable task) {
FutureTask<Void> result = new FutureTask<>();
new Thread(()->{
task.run();
result.finish(null);
}).start();
return result;
}
@Override
public Future<OUT> submit(Task<IN, OUT> task, IN in) {
FutureTask<OUT> result = new FutureTask<>();
new Thread(()->{
OUT execute = task.execute(in);
result.finish(execute);
}).start();
return result;
}
}
Task
public interface Task<IN, OUT> {
OUT execute(IN in);
}
测试
public class FutureMain {
public static void run1() throws Exception {
FutureService<Void, Void> futureService = FutureService.newInstance();
Future<?> future = futureService.submit(() -> {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("I am finished");
});
future.get();
System.out.println("main thread finished");
}
public static void run2() throws Exception {
FutureService<String, String> futureService = FutureService.newInstance();
Future<String> future = futureService.submit((input) -> {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
return input.concat(" I am finished");
}, "hello");
System.out.println(future.get());
System.out.println("main thread finished");
}
public static void main(String[] args) throws Exception {
run1();
run2();
}
}
边栏推荐
- Real questions and answers of comprehensive knowledge of system integration project management engineer in the second half of 2021
- go-zero 微服务实战系列(二、服务拆分)
- 组成最大面积
- Composition maximum area
- 匿名內部類與局部變量
- Error in library(patRoon) : 不存在叫‘patRoon‘这个名字的程辑包
- 15 Win32 class library name in WMI
- 盘点现有开源软件许可合规工具
- What is the difference between a fire engineer and a fireman?
- 06 | the first step of China Taiwan landing: enterprise strategy decomposition and current situation research (Discovery)
猜你喜欢

2. < tag backtracking, combination and pruning > lt.216 Total number of combinations|||

05 | D4模型:中台规划建设方法论概述

Excel | App_WorkbookActive中存在错误不能设置类 Addin 的Installed属性

1.<tag-回溯和组合及其剪枝>lt.77.组合 +剪枝 dyh

6. < tag backtracking and cutting problems > lt.131 Split palindrome string

Error in library(patRoon) : 不存在叫‘patRoon‘这个名字的程辑包

Security evaluation of commercial password application

7. remove elements

12. < tag binary tree and BST foundation > lt.701 Insert operation DBC in binary search tree
![[reprint] what is the](/img/7a/de7df9830f589be0b2214f875eb95e.png)
[reprint] what is the "brain crack" of distributed systems?
随机推荐
UDP可靠性实践
Endnote | 如何快速分享文献给别人(包含题录+备注+pdf文件)| endnote保存备份
爱可可AI前沿推介(6.9)
[reprint] understand G1 garbage collector
H3C Certified Safety Technology Senior Engineer
科研论文写作
PMP项目管理知识体系
5.括号生成
Win11 officially released new features
10. < tag binary tree and BST foundation > lt.700 Search in binary search tree + lt.98 Validate binary search tree + lt.530 Minimum absolute difference of binary search tree (the same as lt.783)
05 | D4 model: overview of China Taiwan planning and construction methodology
实验室常用工具 | 实验溶液配制 | 摩尔浓度及分子量计算工具 molarity-calculator
Error in library(patRoon) : 不存在叫‘patRoon‘这个名字的程辑包
软件项目管理考点整理(Software Project Management)
Redis数据结构与介绍
接力AlphaFold!星药科技重磅发布TBind-开启分子蛋白复合物结构预测新纪元
13. < tag binary tree and BST foundation > lt.450 Delete node DBC in binary search tree
H3C Certified Wireless Internet expert
04 | everything must be done in advance: four issues that must be clearly considered before the construction of China Taiwan Relations
How to insert the video monitoring memory card into the Yuntai pro of Xiaomi smart camera