code: public static <T> T blockForSingle(final Observable<? extends T> observable, final long timeout,
          final TimeUnit tu) {
          final CountDownLatch latch = new CountDownLatch(1);
          TrackingSubscriber<T> subscriber = new TrackingSubscriber<T>(latch);
  
          Subscription subscription = observable.subscribe(subscriber);
  
          try {
              if (!latch.await(timeout, tu)) {
                  if (!subscription.isUnsubscribed()) {
                      subscription.unsubscribe();
                  }
                  throw new RuntimeException(new TimeoutException());
              }
          } catch (InterruptedException e) {
              Thread.currentThread().interrupt();
              throw new RuntimeException("Interrupted while waiting for subscription to complete.", e);
          }
  
          if (subscriber.returnException() != null) {
              if (subscriber.returnException() instanceof RuntimeException) {
                  throw (RuntimeException) subscriber.returnException();
              } else {
                  throw new RuntimeException(subscriber.returnException());
              }
          }
  
          return subscriber.returnItem();
      }
docs: Blocks on an {@link Observable} and returns a single event or throws an {@link 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 {@link Observable} only ever returns a single item (or none). The {@link BlockingObservable}
  code applies different operators like single, last, first and more, these need to be applied manually.
  
  This code is based on {@link BlockingObservable#blockForSingle}, but does not wait forever. Instead, it
  utilizes the internal {@link CountDownLatch} to optimize the timeout case, with less GC and CPU overhead
  than chaining in an {@link Observable#timeout(long, TimeUnit)} operator.
  
  If an error happens inside the {@link Observable}, it will be raised as an {@link Exception}. If the timeout
  kicks in, a {@link TimeoutException} nested in a {@link RuntimeException} is thrown to be fully compatible
  with the {@link Observable#timeout(long, TimeUnit)} behavior.
  
  @param observable the source {@link Observable}
  @param timeout the maximum timeout before an exception is thrown.
  @param tu the timeout unit.
  @param <T> the type returned.
  @return the extracted value from the {@link Observable} or throws in an error case.