com.google.common.util.concurrent.AbstractFuture.setFuture()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(133)

本文整理了Java中com.google.common.util.concurrent.AbstractFuture.setFuture()方法的一些代码示例,展示了AbstractFuture.setFuture()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AbstractFuture.setFuture()方法的具体详情如下:
包路径:com.google.common.util.concurrent.AbstractFuture
类名称:AbstractFuture
方法名:setFuture

AbstractFuture.setFuture介绍

[英]Sets the result of this Future to match the supplied input Future once the supplied Future is done, unless this Future has already been cancelled or set (including "set asynchronously," defined below).

If the supplied future is #isDone when this method is called and the call is accepted, then this future is guaranteed to have been completed with the supplied future by the time this method returns. If the supplied future is not done and the call is accepted, then the future will be set asynchronously. Note that such a result, though not yet known, cannot be overridden by a call to a set* method, only by a call to #cancel.

If the call setFuture(delegate) is accepted and this Future is later cancelled, cancellation will be propagated to delegate. Additionally, any call to setFuture after any cancellation will propagate cancellation to the supplied Future.

Note that, even if the supplied future is cancelled and it causes this future to complete, it will never trigger interruption behavior. In particular, it will not cause this future to invoke the #interruptTask method, and the #wasInterrupted method will not return true.
[中]完成提供的未来后,将此未来的结果设置为与提供的输入未来匹配,除非已取消或设置此未来(包括下面定义的“异步设置”)。
如果调用此方法并接受调用时提供的未来为#isDone,则此方法返回时,此未来保证已使用提供的未来完成。如果未完成提供的未来,并且接受了调用,则将异步设置未来。请注意,这种结果虽然还不知道,但不能通过调用set*方法来覆盖,只能通过调用#cancel来覆盖。
如果接受了调用setFuture(委托),并且以后取消了此未来,则取消将传播到委托。此外,任何取消后对setFuture的任何调用都会将取消传播到所提供的Future。
请注意,即使所提供的未来被取消并导致该未来完成,它也不会触发中断行为。特别是,它不会导致这个未来调用#interruptTask方法,而#wasdirected方法将不会返回true。

代码示例

代码示例来源:origin: google/guava

private void recordInputCompletion(
  ImmutableList<AbstractFuture<T>> delegates, int inputFutureIndex) {
 ListenableFuture<? extends T> inputFuture = inputFutures[inputFutureIndex];
 // Null out our reference to this future, so it can be GCed
 inputFutures[inputFutureIndex] = null;
 for (int i = delegateIndex; i < delegates.size(); i++) {
  if (delegates.get(i).setFuture(inputFuture)) {
   recordCompletion();
   // this is technically unnecessary, but should speed up later accesses
   delegateIndex = i + 1;
   return;
  }
 }
 // If all the delegates were complete, no reason for the next listener to have to
 // go through the whole list. Avoids O(n^2) behavior when the entire output list is
 // cancelled.
 delegateIndex = delegates.size();
}

代码示例来源:origin: google/guava

@Override
 public Void call() {
  if (currentFuture.get().setFuture(future)) {
   numSuccessfulSetCalls.incrementAndGet();
  }
  awaitUnchecked(barrier);
  return null;
 }
};

代码示例来源:origin: google/guava

@Override
 public Void call() {
  setFutureSuccess.set(currentFuture.get().setFuture(future));
  awaitUnchecked(barrier);
  return null;
 }
};

代码示例来源:origin: google/guava

@Override
 public Void call() {
  if (currentFuture.get().setFuture(future)) {
   numSuccessfulSetCalls.incrementAndGet();
  }
  awaitUnchecked(barrier);
  return null;
 }
};

代码示例来源:origin: google/guava

@Override
 public Void call() {
  if (currentFuture.get().setFuture(future)) {
   numSuccessfulSetCalls.incrementAndGet();
  }
  awaitUnchecked(barrier);
  return null;
 }
};

代码示例来源:origin: google/j2objc

private void recordInputCompletion(
  ImmutableList<AbstractFuture<T>> delegates, int inputFutureIndex) {
 ListenableFuture<? extends T> inputFuture = inputFutures[inputFutureIndex];
 // Null out our reference to this future, so it can be GCed
 inputFutures[inputFutureIndex] = null;
 for (int i = delegateIndex; i < delegates.size(); i++) {
  if (delegates.get(i).setFuture(inputFuture)) {
   recordCompletion();
   // this is technically unnecessary, but should speed up later accesses
   delegateIndex = i + 1;
   return;
  }
 }
 // If all the delegates were complete, no reason for the next listener to have to
 // go through the whole list. Avoids O(n^2) behavior when the entire output list is
 // cancelled.
 delegateIndex = delegates.size();
}

代码示例来源:origin: wildfly/wildfly

private void recordInputCompletion(
  ImmutableList<AbstractFuture<T>> delegates, int inputFutureIndex) {
 ListenableFuture<? extends T> inputFuture = inputFutures[inputFutureIndex];
 // Null out our reference to this future, so it can be GCed
 inputFutures[inputFutureIndex] = null;
 for (int i = delegateIndex; i < delegates.size(); i++) {
  if (delegates.get(i).setFuture(inputFuture)) {
   recordCompletion();
   // this is technically unnecessary, but should speed up later accesses
   delegateIndex = i + 1;
   return;
  }
 }
 // If all the delegates were complete, no reason for the next listener to have to
 // go through the whole list. Avoids O(n^2) behavior when the entire output list is
 // cancelled.
 delegateIndex = delegates.size();
}

代码示例来源:origin: google/guava

@Override
 public void run() {
  AbstractFuture<String> future = setFutureFuture.get();
  setFutureSetSucess.set(currentFuture.get().setFuture(future));
  setFutureCompletionSucess.set(future.set("hello-async-world"));
  awaitUnchecked(barrier);
 }
};

代码示例来源:origin: google/guava

private static void assertCannotSet(AbstractFuture<Integer> future) {
 assertThat(future.set(99)).isFalse();
 assertThat(future.setException(new IndexOutOfBoundsException())).isFalse();
 assertThat(future.setFuture(new AbstractFuture<Integer>() {})).isFalse();
 assertThat(future.setFuture(immediateFuture(99))).isFalse();
}

代码示例来源:origin: google/guava

public void testToString_completed() throws Exception {
 AbstractFuture<Object> testFuture2 =
   new AbstractFuture<Object>() {
    @Override
    public String pendingToString() {
     return "cause=[Someday...]";
    }
   };
 AbstractFuture<Object> testFuture3 = new AbstractFuture<Object>() {};
 testFuture3.setFuture(testFuture2);
 assertThat(testFuture3.toString())
   .matches(
     "[^\\[]+\\[status=PENDING, info=\\[setFuture="
       + "\\[[^\\[]+\\[status=PENDING, info=\\[cause=\\[Someday...\\]\\]\\]\\]\\]\\]");
 testFuture2.set("result string");
 assertThat(testFuture3.toString())
   .matches("[^\\[]+\\[status=SUCCESS, result=\\[result string\\]\\]");
}

代码示例来源:origin: google/guava

public void testEvilFuture_setFuture() throws Exception {
 final RuntimeException exception = new RuntimeException("you didn't say the magic word!");
 AbstractFuture<String> evilFuture =
   new AbstractFuture<String>() {
    @Override
    public void addListener(Runnable r, Executor e) {
     throw exception;
    }
   };
 AbstractFuture<String> normalFuture = new AbstractFuture<String>() {};
 normalFuture.setFuture(evilFuture);
 assertTrue(normalFuture.isDone());
 try {
  normalFuture.get();
  fail();
 } catch (ExecutionException e) {
  assertThat(e).hasCauseThat().isSameAs(exception);
 }
}

代码示例来源:origin: com.google.dagger/dagger-producers

@Override
public boolean setFuture(ListenableFuture<? extends T> future) {
 return super.setFuture(future);
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

private void recordInputCompletion(
  ImmutableList<AbstractFuture<T>> delegates, int inputFutureIndex) {
 ListenableFuture<? extends T> inputFuture = inputFutures[inputFutureIndex];
 // Null out our reference to this future, so it can be GCed
 inputFutures[inputFutureIndex] = null;
 for (int i = delegateIndex; i < delegates.size(); i++) {
  if (delegates.get(i).setFuture(inputFuture)) {
   recordCompletion();
   // this is technically unnecessary, but should speed up later accesses
   delegateIndex = i + 1;
   return;
  }
 }
 // If all the delegates were complete, no reason for the next listener to have to
 // go through the whole list. Avoids O(n^2) behavior when the entire output list is
 // cancelled.
 delegateIndex = delegates.size();
}

代码示例来源:origin: com.google.guava/guava-tests

@Override public Void call() {
  if (currentFuture.get().setFuture(future)) {
   numSuccessfulSetCalls.incrementAndGet();
  }
  awaitUnchecked(barrier);
  return null;
 }
};

代码示例来源:origin: com.google.guava/guava-tests

@Override public Void call() {
  if (currentFuture.get().setFuture(future)) {
   numSuccessfulSetCalls.incrementAndGet();
  }
  awaitUnchecked(barrier);
  return null;
 }
};

代码示例来源:origin: com.google.guava/guava-tests

@Override public Void call() {
  if (currentFuture.get().setFuture(future)) {
   numSuccessfulSetCalls.incrementAndGet();
  }
  awaitUnchecked(barrier);
  return null;
 }
};

代码示例来源:origin: com.google.guava/guava-tests

@Override public Void call() {
  setFutureSuccess.set(currentFuture.get().setFuture(future));
  awaitUnchecked(barrier);
  return null;
 }
};

代码示例来源:origin: com.google.guava/guava-tests

@Override public void run() {
  AbstractFuture<String> future = setFutureFuture.get();
  setFutureSetSucess.set(currentFuture.get().setFuture(future));
  setFutureCompletionSucess.set(future.set("hello-async-world"));
  awaitUnchecked(barrier);
 }
};

代码示例来源:origin: com.google.guava/guava-tests

private static void assertCannotSet(AbstractFuture<Integer> future) {
 assertThat(future.set(99)).isFalse();
 assertThat(future.setException(new IndexOutOfBoundsException())).isFalse();
 assertThat(future.setFuture(new AbstractFuture<Integer>() {})).isFalse();
 assertThat(future.setFuture(immediateFuture(99))).isFalse();
}

代码示例来源:origin: com.google.guava/guava-tests

public void testEvilFuture_setFuture() throws Exception {
 final RuntimeException exception = new RuntimeException("you didn't say the magic word!");
 AbstractFuture<String> evilFuture = new AbstractFuture<String>() {
  @Override public void addListener(Runnable r, Executor e) {
   throw exception;
  }
 };
 AbstractFuture<String> normalFuture = new AbstractFuture<String>() {};
 normalFuture.setFuture(evilFuture);
 assertTrue(normalFuture.isDone());
 try {
  normalFuture.get();
  fail();
 } catch (ExecutionException e) {
  assertSame(exception, e.getCause());
 }
}

相关文章