Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有一种简单的方法可以改变未来<;未来<;T>&燃气轮机;走向未来<;T>;?_Java_Concurrency_Guava - Fatal编程技术网

Java 有没有一种简单的方法可以改变未来<;未来<;T>&燃气轮机;走向未来<;T>;?

Java 有没有一种简单的方法可以改变未来<;未来<;T>&燃气轮机;走向未来<;T>;?,java,concurrency,guava,Java,Concurrency,Guava,我有一些代码可以将请求提交给另一个线程,这可能会也可能不会将该请求提交给另一个线程。这将产生一个返回类型Future。是否有一些非邪恶的方法可以立即将此转化为等待整个未来链完成的未来 我已经在使用Guava库来处理其他有趣的并发内容,并将其作为Google Collections的替代品,而且它工作得很好,但我似乎找不到适合这种情况的东西。您可以创建一个类,如: public class UnwrapFuture<T> implements Future<T> {

我有一些代码可以将请求提交给另一个线程,这可能会也可能不会将该请求提交给另一个线程。这将产生一个返回类型
Future
。是否有一些非邪恶的方法可以立即将此转化为等待整个未来链完成的未来


我已经在使用Guava库来处理其他有趣的并发内容,并将其作为Google Collections的替代品,而且它工作得很好,但我似乎找不到适合这种情况的东西。

您可以创建一个类,如:

public class UnwrapFuture<T> implements Future<T> {
    Future<Future<T>> wrappedFuture;

    public UnwrapFuture(Future<Future<T>> wrappedFuture) {
        this.wrappedFuture = wrappedFuture;
    }

    public boolean cancel(boolean mayInterruptIfRunning) {
        try {
            return wrappedFuture.get().cancel(mayInterruptIfRunning);
        } catch (InterruptedException e) {
            //todo: do something
        } catch (ExecutionException e) {
            //todo: do something
        }
    }
    ...
}
public类UnwrapFuture实现Future{
展望未来;
公共未包装未来(未来包装未来){
this.wrappedFuture=wrappedFuture;
}
公共布尔值取消(布尔值可能中断刷新){
试一试{
返回wrappedFuture.get();
}捕捉(中断异常e){
//托多:做点什么
}捕获(执行例外){
//托多:做点什么
}
}
...
}

您必须处理get()可以引发但其他方法无法引发的异常。

这是我第一次尝试,但我确信它有很多错误。我非常乐意用类似于
Futures.compress(f)
的东西来代替它

公共类压缩未来实现未来{
非公开最终未来代表;
公共压缩未来(未来代表){
this.delegate=委托;
}
@凌驾
公共布尔值取消(布尔值可能中断刷新){
if(delegate.isDone()){
返回委托。取消(可能中断fRunning);
}
试一试{
返回delegate.get().cancel(可能中断frunning);
}捕捉(中断异常e){
抛出新的RuntimeException(“获取完成的未来时出错”,e);
}捕获(执行例外){
抛出新的RuntimeException(“获取完成的未来时出错”,e);
}
}
@凌驾
public T get()抛出InterruptedException、ExecutionException{
返回delegate.get().get();
}
@凌驾
公共T get(长超时,时间单位)抛出InterruptedException、ExecutionException、TimeoutException{
long endTime=System.currentTimeMillis()+unit.toMillis(超时);
Future next=delegate.get(超时,单位);
返回next.get(endTime-System.currentTimeMillis(),TimeUnit.millides);
}
@凌驾
公共布尔值已取消(){
如果(!delegate.isDone()){
返回delegate.isCancelled();
}
试一试{
返回delegate.get().isCancelled();
}捕捉(中断异常e){
抛出新的RuntimeException(“获取完成的未来时出错”,e);
}捕获(执行例外){
抛出新的RuntimeException(“获取完成的未来时出错”,e);
}
}
@凌驾
公共布尔isDone(){
如果(!delegate.isDone()){
返回false;
}
试一试{
返回delegate.get().isDone();
}捕捉(中断异常e){
抛出新的RuntimeException(“获取完成的未来时出错”,e);
}捕获(执行例外){
抛出新的RuntimeException(“获取完成的未来时出错”,e);
}
}
}

我认为这是实施未来合同所能做的最好的事情。我采取了尽可能不清洁的态度,以确保它符合合同要求。尤其是带有超时的get的实现

import java.util.concurrent.*;

public class Futures {
  public <T> Future<T> flatten(Future<Future<T>> future) {
    return new FlattenedFuture<T>(future);
  }

  private static class FlattenedFuture<T> implements Future<T> {
    private final Future<Future<T>> future;

    public FlattenedFuture(Future<Future<T>> future) {
      this.future = future;
    }

    public boolean cancel(boolean mayInterruptIfRunning) {
      if (!future.isDone()) {
        return future.cancel(mayInterruptIfRunning);
      } else {
        while (true) {
          try {
            return future.get().cancel(mayInterruptIfRunning);
          } catch (CancellationException ce) {
            return true;
          } catch (ExecutionException ee) {
            return false;
          } catch (InterruptedException ie) {
            // pass
          }
        }
      }
    }

    public T get() throws InterruptedException, 
                          CancellationException, 
                          ExecutionException 
    {
      return future.get().get();
    }

    public T get(long timeout, TimeUnit unit) throws InterruptedException, 
                                                     CancellationException, 
                                                     ExecutionException, 
                                                     TimeoutException 
    {
      if (future.isDone()) {
        return future.get().get(timeout, unit);
      } else {
        return future.get(timeout, unit).get(0, TimeUnit.SECONDS);
      }
    }

    public boolean isCancelled() {
      while (true) {
        try {
          return future.isCancelled() || future.get().isCancelled();
        } catch (CancellationException ce) {
          return true;
        } catch (ExecutionException ee) {
          return false;
        } catch (InterruptedException ie) {
          // pass
        }
      }
    }

    public boolean isDone() {
      return future.isDone() && innerIsDone();
    }

    private boolean innerIsDone() {
      while (true) {
        try {
          return future.get().isDone();
        } catch (CancellationException ce) {
          return true;
        } catch (ExecutionException ee) {
          return true;
        } catch (InterruptedException ie) {
          // pass
        }
      }
    }
  }
}
import java.util.concurrent.*;
公共类期货{
公共未来扁平化(未来){
返回新的扁平未来(future);
}
私有静态类FlattedFuture实现了Future{
私人最终未来;
公共平坦未来(未来){
this.future=未来;
}
公共布尔值取消(布尔值可能中断刷新){
如果(!future.isDone()){
返回未来。取消(可能中断计划);
}否则{
while(true){
试一试{
返回future.get().cancel(可能中断frunning);
}捕获(取消异常){
返回true;
}捕获(被执行者){
返回false;
}捕获(中断异常ie){
//通过
}
}
}
}
public T get()抛出InterruptedException,
取消例外,
执行例外
{
返回future.get().get();
}
公共T get(长超时,时间单位)抛出InterruptedException,
取消例外,
死刑例外,
超时异常
{
if(future.isDone()){
返回future.get().get(超时,单位);
}否则{
返回future.get(超时,单位).get(0,TimeUnit.SECONDS);
}
}
公共布尔值已取消(){
while(true){
试一试{
返回future.isCancelled()| future.get().isCancelled();
}捕获(取消异常){
返回true;
}捕获(被执行者){
返回false;
}捕获(中断异常ie){
//通过
}
}
}
公共布尔isDone(){
返回future.isDone()&&innerIsDone();
}
私有布尔innerIsDone(){
while(true){
试一试{
返回future.get().isDone();
}捕获(取消异常){
返回true;
}捕获(被执行者){
返回true;
}捕获(中断异常ie){
//通过
}
}
}
}
}

另一种可能的实现方式
import java.util.concurrent.*;

public class Futures {
  public <T> Future<T> flatten(Future<Future<T>> future) {
    return new FlattenedFuture<T>(future);
  }

  private static class FlattenedFuture<T> implements Future<T> {
    private final Future<Future<T>> future;

    public FlattenedFuture(Future<Future<T>> future) {
      this.future = future;
    }

    public boolean cancel(boolean mayInterruptIfRunning) {
      if (!future.isDone()) {
        return future.cancel(mayInterruptIfRunning);
      } else {
        while (true) {
          try {
            return future.get().cancel(mayInterruptIfRunning);
          } catch (CancellationException ce) {
            return true;
          } catch (ExecutionException ee) {
            return false;
          } catch (InterruptedException ie) {
            // pass
          }
        }
      }
    }

    public T get() throws InterruptedException, 
                          CancellationException, 
                          ExecutionException 
    {
      return future.get().get();
    }

    public T get(long timeout, TimeUnit unit) throws InterruptedException, 
                                                     CancellationException, 
                                                     ExecutionException, 
                                                     TimeoutException 
    {
      if (future.isDone()) {
        return future.get().get(timeout, unit);
      } else {
        return future.get(timeout, unit).get(0, TimeUnit.SECONDS);
      }
    }

    public boolean isCancelled() {
      while (true) {
        try {
          return future.isCancelled() || future.get().isCancelled();
        } catch (CancellationException ce) {
          return true;
        } catch (ExecutionException ee) {
          return false;
        } catch (InterruptedException ie) {
          // pass
        }
      }
    }

    public boolean isDone() {
      return future.isDone() && innerIsDone();
    }

    private boolean innerIsDone() {
      while (true) {
        try {
          return future.get().isDone();
        } catch (CancellationException ce) {
          return true;
        } catch (ExecutionException ee) {
          return true;
        } catch (InterruptedException ie) {
          // pass
        }
      }
    }
  }
}
import java.util.concurrent.*;
import com.google.common.util.concurrent.*;
import com.google.common.base.*;

public class FFutures {
  public <T> Future<T> flatten(Future<Future<T>> future) {
    return Futures.chain(Futures.makeListenable(future), new Function<Future<T>, ListenableFuture<T>>() {
      public ListenableFuture<T> apply(Future<T> f) {
        return Futures.makeListenable(f);
      }
    });
  }
}