Skip to content

Instantly share code, notes, and snippets.

@johncarl81
Created January 17, 2016 20:13
Show Gist options
  • Select an option

  • Save johncarl81/2a778e2f80cc08f5fd66 to your computer and use it in GitHub Desktop.

Select an option

Save johncarl81/2a778e2f80cc08f5fd66 to your computer and use it in GitHub Desktop.
public class RXTest {
public static void main(String[] args){
ExecutorService oneExecutor = Executors.newSingleThreadExecutor();
ExecutorService twoExecutor = Executors.newSingleThreadExecutor();
ExecutorService threeExecutor = Executors.newSingleThreadExecutor();
Scheduler one = Schedulers.from(oneExecutor);
Scheduler two = Schedulers.from(twoExecutor);
Scheduler three = Schedulers.from(threeExecutor);
createObservable()
.map(createFunction("Function 1"))
.subscribeOn(one)
.map(createFunction("Fuction 2"))
.observeOn(two)
.map(createFunction("Fuction 3"))
.observeOn(three)
.subscribe(createSubscriber());
System.out.println("Finished!");
/*
Outputs:
Finished!
subscriber : pool-1-thread-1
Function 1 : pool-1-thread-1
Fuction 2 : pool-1-thread-1
Fuction 3 : pool-2-thread-1
Action : pool-3-thread-1
*/
}
private static Observable<String> createObservable() {
return Observable.create(new Observable.OnSubscribe<String>() {
public void call(Subscriber<? super String> subscriber) {
work("subscriber");
subscriber.onNext("test");
subscriber.onCompleted();
}
});
}
private static Action1<String> createSubscriber() {
return new Action1<String>() {
public void call(String s) {
work("Action");
}
};
}
public static void work(String name){
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " : " + Thread.currentThread().getName());
}
public static Func1<String, String> createFunction(final String name){
return new Func1<String, String>() {
public String call(String s) {
work(name);
return s;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment