0

There are three parallel polling events, polled every 200 milliseconds, and when one of the polls reaches the result, the three polling events stop, or stop after timeout 2s,How to achieve it using rxjava

var e1 = Observable.interval(200, TimeUnit.MICROSECONDS)
            .subscribeOn(new IoScheduler())
            .map((s) -> EEventType.EVENT_1)
            .filter(s -> s != EEventType.NONE);
var e2 = Observable.interval(200, TimeUnit.MICROSECONDS)
            .subscribeOn(new IoScheduler())
            .map((s) -> EEventType.NONE)
            .filter(s -> s != EEventType.NONE);
var e3 = Observable.interval(200, TimeUnit.MICROSECONDS)
            .subscribeOn(new IoScheduler())
            .map((s) -> EEventType.NONE)
            .filter(s -> s != EEventType.NONE);

Observable.merge(e1, e2, e3)
    .timeout(2000, TimeUnit.MICROSECONDS)
    .subscribe(System.out::println, Throwable::printStackTrace);
akarnokd
  • 69,132
  • 14
  • 157
  • 192
codinlog
  • 1
  • 1
  • What do you mean by "reaches the result"? Please clarify your question. – akarnokd Mar 24 '22 at 10:37
  • The three events are parallel, and 200ms intervals to get whether an event has occurred, the timeout is 2s, of course, the event does not necessarily occur, as long as one of the events occurs and is the required event, then get the event. thank you! – codinlog Mar 25 '22 at 10:24
  • Forgive me for my broken English!!! – codinlog Mar 25 '22 at 10:27
  • Not sure I understood the question. Does a `.first()` after your `timeout` work? – Fred Apr 04 '22 at 07:08

0 Answers0