Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

java - 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

But when i use

.blockingSubscribe();

It return void, and i cant stop all flowable, but the main thread stopped

I can use

.filter(s -> !stopService.get())

but it seems to get a better way to stop all flowable

  1. Is there another way, to stop main thread and get a possibility to stop all flowable?

  2. Maybe there is a way to use Disposable and some main thread blocking operator?

question from:https://stackoverflow.com/questions/65934577/how-to-stop-main-thread-and-get-possibility-to-stop-all-flow-in-rxjava

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

You could use takeUntil with a PublishProcessor to request a termination while using blockingSubscribe.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...