Search code examples
multithreadingscalarx-javarx-scala

RxScala: How to keep the thread doing Observable.interval alive?


I am trying to write a simple RxScala program:

import rx.lang.scala.Observable

import scala.concurrent.duration.DurationInt
import scala.language.{implicitConversions, postfixOps}

object Main {
  def main(args: Array[String]): Unit = {
    val o = Observable.interval(1 second)
    o.subscribe(println(_))
  }
}

When I run this program, I do not see anything printed out. I suspect that this is because that thread producing the numbers in Observable.interval dies. I noticed a call to waitFor(o) in the RxScalaDemo, but I can't figure out where that is imported from.

How do I keep this program running for ever printing the number sequence?


Solution

  • You're not seeing anything because your main method exits immediately after you subscribe to the Observable. At that point, your program is done.

    A common trick for test programs like this is to read a byte from stdin once you've subscribed.