Last active
June 4, 2020 16:15
-
-
Save madawei2699/2142580f956523a5afb788f30bfd5e2f to your computer and use it in GitHub Desktop.
rx java parallel demo
This file contains hidden or 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
public class Rx { | |
public static void main(String[] args) { | |
int threadNum = Runtime.getRuntime().availableProcessors() + 1; | |
ExecutorService executor = Executors.newFixedThreadPool(threadNum); | |
final Scheduler scheduler = Schedulers.from(executor); | |
Observable.range(1, 20) | |
.flatMap(x -> { | |
Observable<Integer> observableA = Observable.just(x).observeOn(scheduler).map(a -> { | |
System.out.println(Thread.currentThread().getName() + ": =observableA=> " + a); | |
return a; | |
}); | |
Observable<Integer> observableB = Observable.just(x).observeOn(scheduler).map(b -> { | |
System.out.println(Thread.currentThread().getName() + ": =observableB=> " + b); | |
return b; | |
}); | |
return Observable.combineLatest(observableA, observableB, (a, b) -> { | |
System.out.println(Thread.currentThread().getName() + ": =combineLatest=> " + "a = " + a + ", b = " + b); | |
return a + b; | |
}).subscribeOn(scheduler).map(y -> { | |
System.out.println(Thread.currentThread().getName() + ": =scheduler=> " + y); | |
return y; | |
}); | |
}).subscribe(x -> { | |
System.out.println(Thread.currentThread().getName() + ": =main=> " + x); | |
}, Throwable::printStackTrace, executor::shutdown); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment