Skip to content

Instantly share code, notes, and snippets.

@dominicthomas
Created April 4, 2018 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominicthomas/e8bd717c455fd6f01fd092a23ccbe7f4 to your computer and use it in GitHub Desktop.
Save dominicthomas/e8bd717c455fd6f01fd092a23ccbe7f4 to your computer and use it in GitHub Desktop.
Small snippet to check which thread an rxjava observable is requesting/returning on
@Test
public void testRxjavaThreading() throws InterruptedException {
final BlockingQueue<Runnable> tasks = new LinkedBlockingQueue<>();
System.out.println("Caller thread: " + Thread.currentThread().getName());
final Disposable subscribe = Observable.fromCallable(
new Callable<Integer>() {
@Override
public Integer call() throws Exception {
System.out.println("Observable thread: " + Thread.currentThread().getName());
return 1;
}
})
.subscribeOn(Schedulers.single())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Integer>() {
@Override
public void accept(@NonNull Integer integer) throws Exception {
System.out.println("Observer thread: " + Thread.currentThread().getName());
}
});
tasks.take().run();
subscribe.dispose();
assertTrue(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment