Created
April 4, 2018 10:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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