本文整理汇总了Java中org.threadly.concurrent.future.ListenableFuture.addListener方法的典型用法代码示例。如果您正苦于以下问题:Java ListenableFuture.addListener方法的具体用法?Java ListenableFuture.addListener怎么用?Java ListenableFuture.addListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.threadly.concurrent.future.ListenableFuture
的用法示例。
在下文中一共展示了ListenableFuture.addListener方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeOrQueue
import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
/**
* This is called once a task is ready to be executed (or if unable to execute immediately,
* queued). In addition to the task itself, this function takes in any future which represents
* task execution (if available, otherwise {@code null}). Passing in as a separate argument
* allows us to avoid a {@code instanceof} check, but does require it to be specified for
* pre-future listener completion support.
*
* @param task Task to be executed
* @param future Future to represent task completion or {@code null} if not available
*/
protected void executeOrQueue(Runnable task, ListenableFuture<?> future) {
if (limitFutureListenersExecution || future == null) {
executeOrQueueWrapper(new LimiterRunnableWrapper(task));
} else {
// we will release the limit restriction as soon as the future completes.
// listeners should be invoked in order, so we just need to be the first listener here
// We add a `SameThreadSubmitterExecutor` so that we get executed first as if it was async
future.addListener(this::releaseExecutionLimit, SameThreadSubmitterExecutor.instance());
if (canRunTask()) {
executor.execute(task);
} else {
addToQueue(new TransparentRunnableContainer(task));
}
}
}
示例2: listenableFutureTest
import org.threadly.concurrent.future.ListenableFuture; //导入方法依赖的package包/类
@Test
public void listenableFutureTest() {
PriorityScheduler executor = new StrictPriorityScheduler(1);
try {
PrioritySchedulerServiceWrapper wrapper = new PrioritySchedulerServiceWrapper(executor);
TestRunnable futureListener = new TestRunnable();
ListenableFuture<?> future = wrapper.submit(DoNothingRunnable.instance());
future.addListener(futureListener);
futureListener.blockTillFinished(); // throws exception if never called
} finally {
executor.shutdownNow();
}
}