本文整理汇总了Java中com.google.common.util.concurrent.ListenableFuture.addListener方法的典型用法代码示例。如果您正苦于以下问题:Java ListenableFuture.addListener方法的具体用法?Java ListenableFuture.addListener怎么用?Java ListenableFuture.addListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.util.concurrent.ListenableFuture
的用法示例。
在下文中一共展示了ListenableFuture.addListener方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testListener
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
@Test()
public void testListener() throws Exception {
final Path keyfile = Paths.get("./junit/etc/minebox/randomkey.txt");
try {
Assert.assertFalse(Files.exists(keyfile));
final LazyEncyptionKeyProvider key = new LazyEncyptionKeyProvider("./junit/etc/minebox/randomkey.txt");
final ListenableFuture<String> masterPassword = key.getMasterPassword();
Assert.assertFalse(masterPassword.isDone());
final CountDownLatch count = new CountDownLatch(1);
masterPassword.addListener(count::countDown, Executors.newSingleThreadExecutor());
Files.write(keyfile, PWBYTES);
count.await();
Assert.assertTrue(masterPassword.isDone());
Assert.assertEquals(PW, key.getImmediatePassword());
} finally {
Files.delete(keyfile);
}
}
示例2: recordResult
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
@Override
public void recordResult(long startTime, ListenableFuture<Object> result)
{
result.addListener(
() -> {
time.add(nanosSince(startTime));
try {
result.get();
successes.update(1);
}
catch (Throwable throwable) {
failures.update(1);
}
},
directExecutor());
}
示例3: recordResult
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
@Override
public void recordResult(long startTime, ListenableFuture<Object> result)
{
invocations.incrementAndGet();
result.addListener(
() -> {
lastStartTime.set(startTime);
try {
result.get();
successes.incrementAndGet();
}
catch (Throwable throwable) {
failures.incrementAndGet();
}
},
directExecutor());
}
示例4: loadAsync
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
ListenableFuture<V> loadAsync(
final K key,
final int hash,
final LoadingValueReference<K, V> loadingValueReference,
CacheLoader<? super K, V> loader) {
final ListenableFuture<V> loadingFuture = loadingValueReference.loadFuture(key, loader);
loadingFuture.addListener(
new Runnable() {
@Override
public void run() {
try {
getAndRecordStats(key, hash, loadingValueReference, loadingFuture);
} catch (Throwable t) {
logger.log(Level.WARNING, "Exception thrown during refresh", t);
loadingValueReference.setException(t);
}
}
},
directExecutor());
return loadingFuture;
}
示例5: uploadBlobAsync
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
@VisibleForTesting
ListenableFuture<Void> uploadBlobAsync(Chunker chunker)
/* throws IOException */ {
Digest digest = checkNotNull(chunker.digest());
synchronized (lock) {
checkState(!isShutdown, "Must not call uploadBlobs after shutdown.");
ListenableFuture<Void> uploadResult = uploadsInProgress.get(digest);
if (uploadResult == null) {
uploadResult = SettableFuture.create();
uploadResult.addListener(
() -> {
synchronized (lock) {
uploadsInProgress.remove(digest);
}
},
MoreExecutors.directExecutor());
startAsyncUploadWithRetry(
chunker, retrier.newBackoff(), (SettableFuture<Void>) uploadResult);
uploadsInProgress.put(digest, uploadResult);
}
return uploadResult;
}
}
示例6: add
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
/**
* Add a Future delegate
*/
public synchronized void add(final ListenableFuture<V> f) {
if (isCancelled() || isDone()) return;
f.addListener(new Runnable() {
public void run() {
futureCompleted(f);
}
}, MoreExecutors.sameThreadExecutor());
futures.add(f);
}
示例7: invoke
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
@Override
public ListenableFuture<Object> invoke(InvokeRequest request, MethodInvoker next)
{
requestCount.getAndIncrement();
ListenableFuture<Object> result = next.invoke(request);
result.addListener(replyCount::getAndIncrement, directExecutor());
return result;
}
示例8: MockFutureListener
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
public MockFutureListener(ListenableFuture<?> future) {
this.countDownLatch = new CountDownLatch(1);
this.future = future;
future.addListener(this, directExecutor());
}
示例9: addInternalStatsReplyListener
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
/**
* Append a listener to receive an OFStatsReply and update the
* internal OFSwitch data structures.
*
* This presently taps into the following stats request
* messages to listen for the corresponding reply:
* -- OFTableFeaturesStatsRequest
*
* Extend this to tap into and update other OFStatsType messages.
*
* @param future
* @param request
* @return
*/
private <REPLY extends OFStatsReply> ListenableFuture<List<REPLY>> addInternalStatsReplyListener(final ListenableFuture<List<REPLY>> future, OFStatsRequest<REPLY> request) {
switch (request.getStatsType()) {
case TABLE_FEATURES:
/* case YOUR_CASE_HERE */
future.addListener(new Runnable() {
/*
* We know the reply will be a list of OFStatsReply.
*/
@SuppressWarnings("unchecked")
@Override
public void run() {
/*
* The OFConnection handles REPLY_MORE for us in the case there
* are multiple OFStatsReply messages with the same XID.
*/
try {
List<? extends OFStatsReply> replies = future.get();
if (!replies.isEmpty()) {
/*
* By checking only the 0th element, we assume all others are the same type.
* TODO If not, what then?
*/
switch (replies.get(0).getStatsType()) {
case TABLE_FEATURES:
processOFTableFeatures((List<OFTableFeaturesStatsReply>) future.get());
break;
/* case YOUR_CASE_HERE */
default:
throw new Exception("Received an invalid OFStatsReply of "
+ replies.get(0).getStatsType().toString() + ". Expected TABLE_FEATURES.");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, MoreExecutors.sameThreadExecutor()); /* No need for another thread. */
default:
break;
}
return future; /* either unmodified or with an additional listener */
}
示例10: addComplete
import com.google.common.util.concurrent.ListenableFuture; //导入方法依赖的package包/类
private ListenableFuture<Void> addComplete(final ListenableFuture<Void> future) {
future.addListener(this::complete, MoreExecutors.directExecutor());
return future;
}