[英]How to stop main thread and get possibility to stop all flow in RxJava
I need to stop main thread and get the possibility to stop flow我需要停止主线程并获得停止流的可能性
I use我用
.subscribe();
The main thread do not stop, but i get the Disposable
, which i can use to stop all flow主线程没有停止,但我得到了
Disposable
,我可以用它来停止所有流
But when i use但是当我使用
.blockingSubscribe();
It return void, and i cant stop all flowable, but the main thread stopped它返回 void,我无法停止所有可流动的,但主线程已停止
I can use我可以用
.filter(s -> !stopService.get())
but it seems to get a better way to stop all flowable但它似乎有更好的方法来阻止所有可流动的
Is there another way, to stop main thread and get a possibility to stop all flowable?还有另一种方法来停止主线程并有可能停止所有可流动的吗?
Maybe there is a way to use Disposable and some main thread blocking operator?也许有一种方法可以使用 Disposable 和一些主线程阻塞运算符?
It return void, and i cant stop all flowable
它返回无效,我无法停止所有可流动的
There is no point in returning a Disposable
because blockingSubscribe
only returns when the flow terminates, thus having a Disposable
after and disposing it has no effect.返回
Disposable
是没有意义的,因为blockingSubscribe
仅在流终止时返回,因此有一个Disposable
after 并处置它没有任何效果。
You could use takeUntil
with a PublishProcessor
to request a termination while using blockingSubscribe
.您可以在使用
blockingSubscribe
时将takeUntil
与PublishProcessor
一起使用来请求终止。
var stop = PublishProcessor.create();
// hand the processor to something that would signal it to stop, e.g.,
ForkJoinPool.commonPool().submit(() -> {
System.out.println("Press ENTER to stop");
System.in.read();
stop.onComplete();
return null; // to get submit(Callable)
});
source
.takeUntil(stop)
.blockingSubscribe();
In general, if you want to block the Java main thread, you'll need an asynchronous signal to get it unblocked.一般来说,如果你想阻塞 Java 主线程,你需要一个异步信号来解除阻塞。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.