com.google.common.util.concurrent.AbstractFuture类的使用及代码示例

x33g5p2x  于11个月前 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(67)

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

AbstractFuture介绍

[英]An abstract implementation of ListenableFuture, intended for advanced users only. More common ways to create a ListenableFuture include instantiating a SettableFuture, submitting a task to a ListeningExecutorService, and deriving a Future from an existing one, typically using methods like Futures#transform(ListenableFuture,com.google.common.base.Function,java.util.concurrent.Executor) and Futures#catching(ListenableFuture,Class,com.google.common.base.Function,java.util.concurrent.Executor).

This class implements all methods in ListenableFuture. Subclasses should provide a way to set the result of the computation through the protected methods #set(Object), #setFuture(ListenableFuture) and #setException(Throwable). Subclasses may also override #afterDone(), which will be invoked automatically when the future completes. Subclasses should rarely override other methods.
[中]ListenableFuture的抽象实现,仅供高级用户使用。创建ListenableFuture的更常见的方法包括实例化SettableFuture、将任务提交给ListeningExecutorService以及从现有任务派生未来,通常使用期货转换(ListenableFuture、com.google.common.base.Function、java.util.concurrent.Executor)和期货捕获(ListenableFuture、Class、com.google.common.base.Function、java.util.concurrent.Executor)等方法。
此类实现ListenableFuture中的所有方法。子类应该提供一种通过受保护的方法#set(Object)、#setFuture(ListenableFuture)和#setException(Throwable)设置计算结果的方法。子类还可以重写#afterDone(),它将在将来完成时自动调用。子类应该很少重写其他方法。

代码示例

代码示例来源:origin: prestodb/presto

@Override
protected boolean setException(Throwable throwable)
{
  return super.setException(throwable);
}

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

@Override
 public AbstractFuture<BarChild> apply(Foo unused) {
  AbstractFuture<BarChild> future = new AbstractFuture<BarChild>() {};
  future.set(barChild);
  return future;
 }
};

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

@CanIgnoreReturnValue
 @Override
 public final boolean cancel(boolean mayInterruptIfRunning) {
  return super.cancel(mayInterruptIfRunning);
 }
}

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

/**
 * If this future has been cancelled (and possibly interrupted), cancels (and possibly interrupts)
 * the given future (if available).
 */
final void maybePropagateCancellationTo(@Nullable Future<?> related) {
 if (related != null & isCancelled()) {
  related.cancel(wasInterrupted());
 }
}

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

@Override
public String toString() {
 StringBuilder builder = new StringBuilder().append(super.toString()).append("[status=");
 if (isCancelled()) {
  builder.append("CANCELLED");
 } else if (isDone()) {
  addDoneString(builder);
 } else {
  String pendingDescription;
  try {
   pendingDescription = pendingToString();
  } catch (RuntimeException e) {
   // Don't call getMessage or toString() on the exception, in case the exception thrown by the
   // subclass is implemented with bugs similar to the subclass.
   pendingDescription = "Exception thrown from implementation: " + e.getClass();
  }
  // The future may complete during or before the call to getPendingToString, so we use null
  // as a signal that we should try checking if the future is done again.
  if (pendingDescription != null && !pendingDescription.isEmpty()) {
   builder.append("PENDING, info=[").append(pendingDescription).append("]");
  } else if (isDone()) {
   addDoneString(builder);
  } else {
   builder.append("PENDING");
  }
 }
 return builder.append("]").toString();
}

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

@Override
 public void run() {
  future.cancel(true);
  if (!future.isDone()) {
   errorMessage.set("Cancel call exited before future was complete.");
  }
 }
});

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

@CanIgnoreReturnValue
@Override
public final V get() throws InterruptedException, ExecutionException {
 return super.get();
}

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

@CanIgnoreReturnValue
 @Override
 public boolean setException(Throwable t) {
  return super.setException(t);
 }
}

代码示例来源: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

@CanIgnoreReturnValue
@Override
public boolean set(T t) {
 return super.set(t);
}

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

@CanIgnoreReturnValue
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
     abstractFuture.interruptTask();
    complete(abstractFuture);
    if (localValue instanceof SetFuture) {

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

@Override
public boolean cancel(boolean interruptIfRunning) {
 InCompletionOrderState<T> localState = state;
 if (super.cancel(interruptIfRunning)) {
  localState.recordOutputCancellation(interruptIfRunning);
  return true;
 }
 return false;
}

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

@Override
 public void run() {
  future.setException(new IllegalArgumentException("failure"));
  if (!future.isDone()) {
   errorMessage.set("SetException call exited before future was complete.");
  }
 }
});

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

@Override
 public void run() {
  future.set("success");
  if (!future.isDone()) {
   errorMessage.set("Set call exited before future was complete.");
  }
 }
});

代码示例来源: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

@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

public void testGetFailure_Failed() {
 AbstractFuture<String> future = new AbstractFuture<String>() {};
 final Throwable failure = new Throwable();
 future.setException(failure);
 assertThat(future.tryInternalFastPathGetFailure()).isNull();
}

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

public void testCancel_done() throws Exception {
 AbstractFuture<String> future =
   new AbstractFuture<String>() {
    {
     set("foo");
    }
   };
 assertFalse(future.cancel(true));
 assertFalse(future.isCancelled());
 assertTrue(future.isDone());
}

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

public void testGetFailure_Completed() {
 AbstractFuture<String> future = new AbstractFuture<String>() {};
 future.set("261");
 assertThat(future.tryInternalFastPathGetFailure()).isNull();
}

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

@Override
public void run() {
 try {
  future.get();
 } catch (Exception e) {
  // nothing
 }
}

相关文章