Blocking
@InterfaceStability.Experimental @InterfaceAudience.Private @Deprecated public class Blocking extends Object
Contains various utility methods related to blocking operations.
Modifier and Type | Method and Description |
---|---|
static <T> T |
blockForSingle(Observable<? extends T> observable,
long timeout,
TimeUnit tu)
Deprecated.
Blocks on an
Observable and returns a single event or throws an Exception . |
public static <T> T blockForSingle(Observable<? extends T> observable, long timeout, TimeUnit tu)
Blocks on an Observable
and returns a single event or throws an Exception
.
Note that when this method is used, only the first item emitted will be returned. The caller needs to make sure that the source Observable
only ever returns a single item (or none). The BlockingObservable
code applies different operators like single, last, first and more, these need to be applied manually.
This code is based on BlockingObservable.blockForSingle(rx.Observable<? extends T>)
, but does not wait forever. Instead, it utilizes the internal CountDownLatch
to optimize the timeout case, with less GC and CPU overhead than chaining in an Observable.timeout(long, TimeUnit)
operator.
If an error happens inside the Observable
, it will be raised as an Exception
. If the timeout kicks in, a TimeoutException
nested in a RuntimeException
is thrown to be fully compatible with the Observable.timeout(long, TimeUnit)
behavior.
observable
- the source Observable
timeout
- the maximum timeout before an exception is thrown.tu
- the timeout unit.T
- the type returned.Observable
or throws in an error case.Copyright © 2015 Couchbase, Inc.