Java 如何观察线程池队列上的fast源代码
需要帮助在主线程上创建一个可观察的开始,然后转到一个线程池,允许源继续发送新项目(不管它们是否仍在线程池中处理) 这是我的例子:Java 如何观察线程池队列上的fast源代码,java,multithreading,rx-java,Java,Multithreading,Rx Java,需要帮助在主线程上创建一个可观察的开始,然后转到一个线程池,允许源继续发送新项目(不管它们是否仍在线程池中处理) 这是我的例子: public static void main(String[] args) { Observable<Integer> source = Observable.range(1,10); source.map(i -> sleep(i, 10)) .doOnNext(i -> System.out.pr
public static void main(String[] args) {
Observable<Integer> source = Observable.range(1,10);
source.map(i -> sleep(i, 10))
.doOnNext(i -> System.out.println("Emitting " + i + " on thread " + Thread.currentThread().getName()))
.observeOn(Schedulers.computation())
.map(i -> sleep(i * 10, 300))
.subscribe( i -> System.out.println("Received " + i + " on thread " + Thread.currentThread().getName()));
sleep(-1, 30000);
}
private static int sleep(int i, int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
return i;
}
尽管项目是在主线程上发出的,但我希望它们在之后转移到计算/IO线程池
应该是这样的:
我认为你没有充分减缓源排放,而且它们的排放速度如此之快,以至于在
observeOn()
有机会安排排放之前,所有项目都已排放
试着睡到500毫秒而不是10毫秒。然后您将看到像您预期的那样交错
public class JavaLauncher {
public static void main(String[] args) {
Observable<Integer> source = Observable.range(1,10);
source.map(i -> sleep(i, 500))
.doOnNext(i -> System.out.println("Emitting " + i + " on thread " + Thread.currentThread().getName()))
.observeOn(Schedulers.computation())
.map(i -> sleep(i * 10, 250))
.subscribe( i -> System.out.println("Received " + i + " on thread " + Thread.currentThread().getName()));
sleep(-1, 30000);
}
private static int sleep(int i, int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
return i;
}
}
更新-并行化版本
public class JavaLauncher {
public static void main(String[] args) {
Observable<Integer> source = Observable.range(1,10);
source.map(i -> sleep(i, 250))
.doOnNext(i -> System.out.println("Emitting " + i + " on thread " + Thread.currentThread().getName()))
.flatMap(i ->
Observable.just(i)
.subscribeOn(Schedulers.computation())
.map(i2 -> sleep(i2 * 10, 500))
)
.subscribe( i -> System.out.println("Received " + i + " on thread " + Thread.currentThread().getName()));
sleep(-1, 30000);
}
private static int sleep(int i, int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
return i;
}
}
我认为在这个例子中,一切都发生得很快,它似乎在
observeOn()
之前阻塞,尽管它不是。让我看看我是否可以用一种证明睡眠时间有效的方式来夸大睡眠时间。请记住,observeOn()
并不是在“线程池”上处理项目。这只是一个线程从池。如果要利用池中的所有线程,需要在输出中显示它仍然始终使用池中的1个线程。但正如您在评论中提到的,您本来希望这样,但我没有。是的,subscribeOn()
和observeOn()
只将发射重定向到另一个线程。如果您想真正并行化并使用调度器中可用的所有线程,您需要做一些FlatMap功夫。我把代码的并行版本放在这里。我也会仔细阅读关于并行化的文章,因为很多人假设并困惑如何正确地进行并行化。我害怕那种解决办法。通过创建一个新的序列(可观察的),您可以将其发送到一个新线程,但这样我不确定背压操作符将如何工作(源代码有时可能会快得多)。
Emitting 1 on thread main
Emitting 2 on thread main
Emitting 3 on thread main
Received 10 on thread RxComputationThreadPool-3
Emitting 4 on thread main
Received 20 on thread RxComputationThreadPool-3
Emitting 5 on thread main
Emitting 6 on thread main
Received 30 on thread RxComputationThreadPool-3
Emitting 7 on thread main
Emitting 8 on thread main
Received 40 on thread RxComputationThreadPool-3
Emitting 9 on thread main
Emitting 10 on thread main
Received 50 on thread RxComputationThreadPool-3
Received 60 on thread RxComputationThreadPool-3
Received 70 on thread RxComputationThreadPool-3
Received 80 on thread RxComputationThreadPool-3
Received 90 on thread RxComputationThreadPool-3
Received 100 on thread RxComputationThreadPool-3
public class JavaLauncher {
public static void main(String[] args) {
Observable<Integer> source = Observable.range(1,10);
source.map(i -> sleep(i, 250))
.doOnNext(i -> System.out.println("Emitting " + i + " on thread " + Thread.currentThread().getName()))
.flatMap(i ->
Observable.just(i)
.subscribeOn(Schedulers.computation())
.map(i2 -> sleep(i2 * 10, 500))
)
.subscribe( i -> System.out.println("Received " + i + " on thread " + Thread.currentThread().getName()));
sleep(-1, 30000);
}
private static int sleep(int i, int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
return i;
}
}
Emitting 1 on thread main
Emitting 2 on thread main
Emitting 3 on thread main
Received 10 on thread RxComputationThreadPool-3
Emitting 4 on thread main
Received 20 on thread RxComputationThreadPool-4
Received 30 on thread RxComputationThreadPool-1
Emitting 5 on thread main
Received 40 on thread RxComputationThreadPool-2
Emitting 6 on thread main
Received 50 on thread RxComputationThreadPool-3
Emitting 7 on thread main
Received 60 on thread RxComputationThreadPool-4
Emitting 8 on thread main
Received 70 on thread RxComputationThreadPool-1
Emitting 9 on thread main
Received 80 on thread RxComputationThreadPool-2
Emitting 10 on thread main
Received 90 on thread RxComputationThreadPool-3
Received 100 on thread RxComputationThreadPool-4