LoginSignup
4
4

More than 5 years have passed since last update.

RxJavaで時間軸にまつわる実験をするのに便利なコード

Last updated at Posted at 2015-01-20

RxJavaで時間軸にまつわる実験をするのに便利なコードを書いてみた。初期値としてミリ秒単位で数字を並べると、その数字の後にそのミリ秒分の delay が入るという物。

Observable
    .just(100, 200, 100, 300, 100)
    .concatMap(ms -> Observable.just(ms).delay(ms, TimeUnit.MILLISECONDS))
    .timeInterval()
    .toBlocking()
    .forEach(n -> System.out.println(n));

ちなみにtoBlocking()を入れているのは、RxJavaではObservable.interval() や Observable#delay を入れると、ストリームが途中の状態でもメインスレッドが終了してしまうため。

なので、toBlocking()を入れなくても Thread.sleep() を入れるなどの手段でも適切に実行することはできる。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4