Future设计模式
2023-12-16 02:39:33
? ? ? ? Future设计模式,针对长时间执行任务场景,提供了一种凭据式解决方案。Future提供了获取计算结果和判断是否完成的两个方法,其中获取计算结果将会导致调用阻塞(在任务未完成的情况下)。
示例代码如下:
public interface Future<T> {
//get result
T get() throws InterruptedException;
// task is done or not
boolean done();
}
//配置任务的入参类型和返回参数类型
public interface Task<IN,OUT> {
OUT get(IN input);//确定IN和OUT的关系
}
public class FutureTask<T> implements Future<T>{
private T result;
private boolean isDone=false;
private final Object LOCK=new Object();
@Override
public T get() throws InterruptedException {
synchronized(LOCK) {
while(!isDone) {
LOCK.wait();
}
return result;
}
}
protected void finish(T result) {
synchronized(LOCK) {
if(isDone)return;
this.result=result;
this.isDone=true;
LOCK.notifyAll();
}
}
@Override
public boolean done() {
return isDone;
}
}
public interface FutureService<IN,OUT> {
Future<?> submit(Runnable runnable);
Future<OUT> submit(Task<IN,OUT> task, IN input);
static <T,R> FutureService<T,R> newService(){
return new FutureServiceImpl<>();
}
}
import java.util.concurrent.atomic.AtomicInteger;
public class FutureServiceImpl<IN, OUT> implements FutureService<IN,OUT>{
private final String FUTURE_THREAD_PREFIX="FUTURE-";
private final AtomicInteger nextCounter=new AtomicInteger(0);
private String getNextName() {
return this.FUTURE_THREAD_PREFIX+this.nextCounter.getAndIncrement();
}
@Override
public Future<?> submit(Runnable runnable) {
final FutureTask<Void> future=new FutureTask<>();
new Thread(()->{
runnable.run();
future.finish(null);
},this.getNextName()).start();
return future;
}
@Override
public Future<OUT> submit(Task<IN, OUT> task, IN input) {
final FutureTask<OUT> future=new FutureTask<>();
new Thread(()->{
OUT result=task.get(input);
future.finish(result);
},this.getNextName()).start();
return future;
}
}
import java.util.concurrent.TimeUnit;
public class FTest {
public static void main(String[] args) {
FutureService<Void,Void> service=FutureService.newService();
Future<?> future=service.submit(()->{
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("done the future task.");
});
try {
System.out.println(future.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FutureService<String,Integer> s=FutureService.newService();
Future<Integer> f=s.submit(input->{
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return input.length();
}, "Hello");
try {
System.out.println(f.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
文章来源:https://blog.csdn.net/seacean2000/article/details/134965047
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!