文章目录
Code
Future 接口 的局限性有很多,其中一个就是需要主动的去询问是否完成,如果等子线程的任务完成以后,通知我,那岂不是更好?
public class FutureInAction3 {
public static void main(String[] args) {
Future<String> future = invoke(() -> {
try {
Thread.sleep(10000L);
return "I am Finished.";
} catch (InterruptedException e) {
return "I am Error";
}
});
future.setCompletable(new Completable<String>() {
@Override
public void complete(String s) {
System.out.println("complete called ---- " + s);
}
@Override
public void exception(Throwable cause) {
System.out.println("error");
cause.printStackTrace();
}
});
System.out.println("....do something else .....");
System.out.println("try to get result ->" + future.get());
}
private static <T> Future<T> invoke(Callable<T> callable) {
AtomicReference<T> result = new AtomicReference<>();
AtomicBoolean finished = new AtomicBoolean(false);
Future<T> future = new Future<T>() {
private Completable<T> completable;
@Override
public T get() {
return result.get();
}
@Override
public boolean isDone() {
return finished.get();
}
// 设置完成
@Override
public void setCompletable(Completable<T> completable) {
this.completable = completable;
}
// 获取
@Override
public Completable<T> getCompletable() {
return completable;
}
};
Thread t = new Thread(() -> {
try {
T value = callable.action();
result.set(value);
finished.set(true);
if (future.getCompletable() != null)
future.getCompletable().complete(value);
} catch (Throwable cause) {
if (future.getCompletable() != null)
future.getCompletable().exception(cause);
}
});
t.start();
return future;
}
private interface Future<T> {
T get();
boolean isDone();
// 1
void setCompletable(Completable<T> completable);
// 2
Completable<T> getCompletable();
}
private interface Callable<T> {
T action();
}
// 回调接口
private interface Completable<T> {
void complete(T t);
void exception(Throwable cause);
}
}