本文整理匯總了Java中com.google.common.util.concurrent.AbstractFuture類的典型用法代碼示例。如果您正苦於以下問題:Java AbstractFuture類的具體用法?Java AbstractFuture怎麽用?Java AbstractFuture使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AbstractFuture類屬於com.google.common.util.concurrent包,在下文中一共展示了AbstractFuture類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: fetchMoreResultsAsync
import com.google.common.util.concurrent.AbstractFuture; //導入依賴的package包/類
@Override
public ListenableFuture<ResultSet> fetchMoreResultsAsync() {
return new AbstractFuture<ResultSet>() {
{
new Thread() {
public void run() {
for (int i = 0; i< 5; i++) {
try {
Thread.sleep(fetchDelayMillis);
} catch (InterruptedException ignore) { }
System.out.print("z");
}
loadNextChunk();
set(null);
};
}.start();
}
};
}
示例2: testDelayedCheckingOfCachingProviders
import com.google.common.util.concurrent.AbstractFuture; //導入依賴的package包/類
@Test
public void testDelayedCheckingOfCachingProviders() {
String soyFileContent =
"{namespace ns}\n"
+ "\n"
+ "{template .template}\n"
+ " {@param foo: int}\n"
+ " Before: {$foo}\n"
+ "{/template}\n";
final StringBuilder outputSb = new StringBuilder();
final AtomicReference<String> outputAtFutureGetTime = new AtomicReference<>();
AbstractFuture<Integer> fooFuture =
new AbstractFuture<Integer>() {
{
set(1);
}
@Override
public Integer get() throws InterruptedException, ExecutionException {
outputAtFutureGetTime.set(outputSb.toString());
return super.get();
}
};
SoyRecord data = SoyValueConverterUtility.newDict("foo", fooFuture);
assertThat(
renderTemplateInFile(
SoyFileSetParserBuilder.forFileContents(soyFileContent).parse(),
"ns.template",
data,
TEST_IJ_DATA,
Predicates.<String>alwaysFalse(),
outputSb))
.isEqualTo("Before: 1");
assertThat(outputAtFutureGetTime.get()).isEqualTo("Before: ");
}
示例3: to
import com.google.common.util.concurrent.AbstractFuture; //導入依賴的package包/類
/**
* Immediately subscribes to the {@link Observable} and returns a future that will contain the only one value T passed in to the
* {@link Observer#onNext(Object)}. If more than one value is received then an {@link Observer#onError(Throwable)} is invoked.
* <p>
* If the source {@link Observable} emits more than one item or no items, notify of an IllegalArgumentException or NoSuchElementException respectively.
*
* @param observable The source {@link Observable} for the value.
* @return a {@link ListenableFuture} that sets the value on completion.
*/
public static <T> ListenableFuture<T> to(final Observable<T> observable) {
class ListenFutureSubscriberAdaptor extends AbstractFuture<T> {
final Subscriber<? super T> subscriber;
private ListenFutureSubscriberAdaptor() {
subscriber = new Subscriber<T>() {
private T value;
@Override
public void onCompleted() {
set(value);
}
@Override
public void onError(Throwable e) {
setException(e);
}
@Override
public void onNext(T t) {
// wait for the onCompleted to make sure the observable on emits one value.
value = t;
}
};
}
@Override
protected void interruptTask() {
subscriber.unsubscribe();
}
}
ListenFutureSubscriberAdaptor future = new ListenFutureSubscriberAdaptor();
// Futures are hot so subscribe immediately
observable.single().subscribe(future.subscriber);
return future;
}
示例4: testStreamLazyParamsToOutputStreamDirectly
import com.google.common.util.concurrent.AbstractFuture; //導入依賴的package包/類
@Test
public void testStreamLazyParamsToOutputStreamDirectly() {
String soyFileContent =
Joiner.on("\n")
.join(
"{namespace ns}",
"",
"{template .callee}",
" {@param body: html}",
" <div>",
" {$body}",
" </div>",
"{/template}",
"",
"{template .caller}",
" {@param future: string}",
" {call .callee}",
" {param body kind=\"html\"}",
" static-content{sp}",
" {$future}",
" {/param}",
" {/call}",
"{/template}");
final StringBuilder outputSb = new StringBuilder();
final AtomicReference<String> outputAtFutureGetTime = new AtomicReference<>();
AbstractFuture<String> future =
new AbstractFuture<String>() {
{
set("future-content");
}
@Override
public String get() throws InterruptedException, ExecutionException {
outputAtFutureGetTime.set(outputSb.toString());
return super.get();
}
};
SoyRecord data = SoyValueConverterUtility.newDict("future", future);
assertThat(
renderTemplateInFile(
SoyFileSetParserBuilder.forFileContents(soyFileContent).parse(),
"ns.caller",
data,
TEST_IJ_DATA,
Predicates.<String>alwaysFalse(),
outputSb))
.isEqualTo("<div>static-content future-content</div>");
assertThat(outputAtFutureGetTime.get()).isEqualTo("<div>static-content ");
}