{"action":"created","issue":{"url":"https://api.github.com/repos/ReactiveX/RxJava/issues/3758","repository_url":"https://api.github.com/repos/ReactiveX/RxJava","labels_url":"https://api.github.com/repos/ReactiveX/RxJava/issues/3758/labels{/name}","comments_url":"https://api.github.com/repos/ReactiveX/RxJava/issues/3758/comments","events_url":"https://api.github.com/repos/ReactiveX/RxJava/issues/3758/events","html_url":"https://github.com/ReactiveX/RxJava/issues/3758","id":140558779,"number":3758,"title":"Threading in a SerializedSubject","user":{"login":"a-reisberg","id":16026762,"avatar_url":"https://avatars.githubusercontent.com/u/16026762?v=3","gravatar_id":"","url":"https://api.github.com/users/a-reisberg","html_url":"https://github.com/a-reisberg","followers_url":"https://api.github.com/users/a-reisberg/followers","following_url":"https://api.github.com/users/a-reisberg/following{/other_user}","gists_url":"https://api.github.com/users/a-reisberg/gists{/gist_id}","starred_url":"https://api.github.com/users/a-reisberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/a-reisberg/subscriptions","organizations_url":"https://api.github.com/users/a-reisberg/orgs","repos_url":"https://api.github.com/users/a-reisberg/repos","events_url":"https://api.github.com/users/a-reisberg/events{/privacy}","received_events_url":"https://api.github.com/users/a-reisberg/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ReactiveX/RxJava/labels/Question","name":"Question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-03-14T02:29:05Z","updated_at":"2016-03-14T20:27:07Z","closed_at":null,"body":"With usual `Observable`, the `Observer` is executed on the same thread as the one that the `Observable` is created. With `SerializedSubject`, there's some magic that is unclear. Below is the code snippet:\r\n\r\n```scala\r\nobject Launcher extends App {\r\n  val pubSub = PublishSubject.create[Int]\r\n  val serSub = new SerializedSubject(pubSub)\r\n  val observable = Observable.just(1, 2).subscribeOn(Schedulers.computation())\r\n\r\n  serSub.subscribe(new Action1[Int] {\r\n    override def call(t: Int): Unit =\r\n      println(s\"Subject subscribeOn $t, ${Thread.currentThread().getName}\")\r\n  })\r\n\r\n  observable.subscribe(serSub)\r\n  Observable.just(6).subscribe(serSub)\r\n\r\n  while (true) {}\r\n}\r\n```\r\n\r\nOne would expect that the numbers 1 and 2 are printed inside some computation thread, and the 6 is printed inside main thread. Now, if I remove the `Observable.just(6)` line, everything is printed inside the computation thread. Is that the expected behavior? If so, how does the `SerializedSubject` decide which thread to run?\r\n\r\nNow, I can modify a code a bit (publish to pubSub and subscribe to serSub)\r\n```scala\r\nobject Launcher extends App {\r\n  val pubSub = PublishSubject.create[Int]\r\n  val serSub = new SerializedSubject(pubSub)\r\n  val observable = Observable.just(1, 2).subscribeOn(Schedulers.computation())\r\n\r\n  serSub.subscribe(new Action1[Int] {\r\n    override def call(t: Int): Unit =\r\n      println(s\"Subject subscribeOn $t, ${Thread.currentThread().getName}\")\r\n  })\r\n\r\n  observable.subscribe(pubSub)\r\n  Observable.just(6, 7, 8, 9, 10).subscribe(pubSub)\r\n\r\n  while (true) {}\r\n}\r\n```\r\nthen things get printed in the expected thread. Is this the legit way to do it if I want this behavior?"},"comment":{"url":"https://api.github.com/repos/ReactiveX/RxJava/issues/comments/196507928","html_url":"https://github.com/ReactiveX/RxJava/issues/3758#issuecomment-196507928","issue_url":"https://api.github.com/repos/ReactiveX/RxJava/issues/3758","id":196507928,"user":{"login":"a-reisberg","id":16026762,"avatar_url":"https://avatars.githubusercontent.com/u/16026762?v=3","gravatar_id":"","url":"https://api.github.com/users/a-reisberg","html_url":"https://github.com/a-reisberg","followers_url":"https://api.github.com/users/a-reisberg/followers","following_url":"https://api.github.com/users/a-reisberg/following{/other_user}","gists_url":"https://api.github.com/users/a-reisberg/gists{/gist_id}","starred_url":"https://api.github.com/users/a-reisberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/a-reisberg/subscriptions","organizations_url":"https://api.github.com/users/a-reisberg/orgs","repos_url":"https://api.github.com/users/a-reisberg/repos","events_url":"https://api.github.com/users/a-reisberg/events{/privacy}","received_events_url":"https://api.github.com/users/a-reisberg/received_events","type":"User","site_admin":false},"created_at":"2016-03-14T20:27:07Z","updated_at":"2016-03-14T20:27:07Z","body":"@akarnokd That's a very helpful answer! I noticed that you are using `observeOn` in your example instead of `subscribeOn` (as you mentioned in the previous comment). Since `subscribeOn` usually changes the thread up in the stream, I would expect that it changes the thread where the `subject`'s `onNext` is called and hence, where the subscribed action is executed. However, when I play with it, `subscribeOn` doesn't actually affect anything. Is it because `SerializedSubject` doing some *serializing* which makes `subscribeOn` useless? \r\n\r\nIn general, due to what @davidmoten said, the actual thread where things get executed (without using `observeOn` to modify it) is essentially only determined at run time. Is my understanding correct?\r\n\r\nThanks again!"}}