Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Samuel Tardieu
    @samueltardieu
    @tusharmath Those are Java methods available on Object and thus any Java (or Scala) object, as toString() is for example.
    Tushar Mathur
    @tusharmath
    okay
    Dorus
    @Dorus
    Ah sorry, i got distracted. But looks like you already have an answer. I was going to point at the Java guarded blocks tutorial as these are Java methods available on every Object. The only scala part is using new AnyRef instead of new Object().
    Tushar Mathur
    @tusharmath
    I thought Rx was single threaded?
    How many threads would I have created in the above code?
    is it one per interval call?
    Dmitriy Zaitsev
    @DmitriyZaitsev
    interval implicitly subscribes on computation scheduler
    your observables a, b, c emit elements on different threads
    Tushar Mathur
    @tusharmath
    But the actual excution is still happening on the main thread right?
    every interval creates a new thread, which is quite essentially idle and waiting for a timeout. Eventually when it fires it goes to the main thread to get consumed?
    Dorus
    @Dorus
    I always understood it runs on a threadpool, so most of the time it will actually run singlethreaded, as not all timeouts are expected to happen at the same time.
    However, the only thing it guarantees is that events run serialized, different events might still run parallel for different steps, and flatMap might even run everything concurrent (just that it starts serialized but if the inner observable has a delay, it already starts the next one).
    Im not sure what the computation does, but @DmitriyZaitsev might be right that it starts multiple threads.
    As that's often optimal for computation work.
    Tushar Mathur
    @tusharmath
    Which part would be pushed to the thread pool ?
    In the above example
    Will the map functions be called parallely ?
    or would it be done on the main thread?
    Or is it something we can control?
    Dorus
    @Dorus
    Can you point back at the example?
    You can give it an scheduler yes
    Tushar Mathur
    @tusharmath
      def main(args: Array[String]) {
        val lock: AnyRef = new AnyRef
        val a = Observable.interval(1000 millis).map(_ => "A")
        val d = Observable.interval(3000 millis).map(_ => "D")
        val e = Observable.interval(5000 millis).map(_ => "E")
        a.merge(d).merge(e)
          .take(100)
          .subscribe(new Subscriber[String]() {
            def onCompleted() {
              lock synchronized {
                lock.notifyAll()
              }
            }
            def onError(throwable: Throwable) {}
            def onNext(s: String) = println(s)
          })
        lock synchronized {
          lock.wait()
        }
      }
    }
    Dorus
    @Dorus
    What part needs to run on one thread?
    There are multiple options, but 90% of the time the default will perform best and adding too many hints isn't optimal.
    Tushar Mathur
    @tusharmath
    I don't know how this would actually work
    what is off loaded to the thread pool and what is not
    For instance — in this case
    Dorus
    @Dorus
    You can set the scheduler on interval, or use observeOn (and subscribeOn) to give hints, gurantee some parts are on the IO scheduler etc.
    Tushar Mathur
    @tusharmath
    1. Scheduling could be done on a different thread
    2. The map functions could be called in parallel too
    Dorus
    @Dorus
    Pretty much everything runs on the threadpool since interval has an delay.
    Tushar Mathur
    @tusharmath
    So if the map function was doing something computationally expensive, would it be automatically moved on a different thread?
    Dorus
    @Dorus
    Well, the other 2 intervals will keep running on different threads yes.
    However, the next interval of the one that's slow wont fire.
    I'm not 100% sure if it plays catch up later on or not.
    Tushar Mathur
    @tusharmath
    what about the map()
    ?
    Dorus
    @Dorus
    Map never runs parallel with itself, that's a propertie of Rx.
    Part of the Rx contract.
    If you need the stuff inside Map to run paralele, you should have used flatMap
    However, the 3 sources do run parallel.
    (or could, they're not guaranteed to do so, they only will if they fire at the same time ofcourse.)
    Tbh Rx is more suited for IO work than computation heavy work.
    But you can still work with it
    Tushar Mathur
    @tusharmath
    i see
    Dorus
    @Dorus
    The only way to make things run parallel in Rx is by calling them async. Operations like merge you use here, or flatMap, do run concurrent.
    At least, everything before merge runs concurrent. After merge the pipeline is serialized.
    Hope this makes sense.
    Things in onNext will run serialized, but the 3 calls to map can run parallele.
    Tushar Mathur
    @tusharmath
    How does one know what part of the code is being executed in parallel?
    Dorus
    @Dorus
    Just that the events from a single interval source here wont run parallel.