예제 #1
0
 /**
  * Incrementing int without backpressure.
  *
  * @param counter
  * @return
  */
 private static Observable<Integer> firehose(final AtomicInteger counter) {
   return Observable.create(
       s -> {
         Subscription s2 = new FirehoseNoBackpressure(counter, s);
         s.onSubscribe(s2);
       });
 }
예제 #2
0
  private static Observable<Integer> incrementingIntegers(
      final AtomicInteger counter, final ConcurrentLinkedQueue<Thread> threadsSeen) {
    return Observable.create(
        new Publisher<Integer>() {

          @Override
          public void subscribe(final Subscriber<? super Integer> s) {
            s.onSubscribe(
                new Subscription() {
                  int i = 0;

                  volatile boolean cancelled;

                  final AtomicLong requested = new AtomicLong();

                  @Override
                  public void request(long n) {
                    if (SubscriptionHelper.validateRequest(n)) {
                      return;
                    }
                    if (threadsSeen != null) {
                      threadsSeen.offer(Thread.currentThread());
                    }
                    long _c = BackpressureHelper.add(requested, n);
                    if (_c == 0) {
                      while (!cancelled) {
                        counter.incrementAndGet();
                        s.onNext(i++);
                        if (requested.decrementAndGet() == 0) {
                          // we're done emitting the number requested so return
                          return;
                        }
                      }
                    }
                  }

                  @Override
                  public void cancel() {
                    cancelled = true;
                  }
                });
          }
        });
  }