Question about interrupted threads in RxJava2
See original GitHub issuehi, i have a question that’s similar to #4863, but a bit simpler. i am running into a strange issue that i have been able to simplify to a simple test case - what’s happening is, sometimes, by the time my most downstream method is called, the thread is interrupted (which causes issues because the code i call there bails if the thread is interrupted).
i’ve been able to simplify the code and repro using this:
private Single<ArrayList<Integer>> fakeDataPiece() {
return Single.fromCallable(() -> {
Thread.sleep(500);
return new ArrayList<Integer>();
}).subscribeOn(Schedulers.io());
}
private Single<BookmarkData> fakeGetData() {
return Single.zip(fakeDataPiece(), fakeDataPiece(), fakeDataPiece(),
(integers, integers2, integers3) ->
new BookmarkData(new ArrayList<>(), new ArrayList<>(), new ArrayList<>()))
.subscribeOn(Schedulers.io());
}
public Single<Uri> exportBookmarksObservable() {
return fakeGetData()
.flatMap(bookmarkData -> Single.just(exportBookmarks(bookmarkData)))
.subscribeOn(Schedulers.io());
}
by the time exportBookmarks
is called, in many cases, Thread.currentThread().isInterrupted()
returns true
.
my question is: why is the thread interrupted? (i am curious as to whether this is expected or if there is something i am misunderstanding or doing wrong).
observations:
- if i explicitly remove all the
subscribeOn
s except for the one inexportBookmarksObservable
, then i don’t see the issue (though it does change the behavior a bit in the sense that, with this, all workers are run on the same thread as opposed to each piece of work happening on a potentially different thread). - if i add a
doOnSuccess
infakeGetData()
, i find the thread is interrupted (and is the same thread thatexportBookmarks
gets called on, while also being interrupted). this also is the same as one of thefakeDataPiece
threads, which, at the time of its completion, is not interrupted. - calling
Thread.interrupted()
as suggested in #4863 also fixes the problem.
i am using RxJava 2.0.4 - thanks!
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
How to implement an interrupted thread in rxjava?
I have the following code that I want to turn Reactive: class ReadThread extends Thread { @Override public void run() { super.run(); ...
Read more >Reactive Multi-Threading with RxJava - Pitfalls and Solutions
I recently had a rough time refactoring a multi-threaded, reactive message processor. It just didn't seem to be working the way I expected....
Read more >Java Interrupting a Thread - javatpoint
If any thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the interrupt() method on the thread, breaks...
Read more >Get rid of Thread.sleep() from your Java code - Medium
Wherever you want to delay execution, you just put in something like this: try { Thread.sleep(1000); } catch (InterruptedException e) { }
Read more >Dealing with Backpressure with RxJava - Baeldung
We will look at the different solutions to the problem of growing buffer ... + v); Thread.sleep(1000); } catch (InterruptedException e) { e....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks, I see the problem now:
Single.zip
delegates toFlowable.zip
which cancels when a source completes before the others complete.This has to be fixed on the operator level so I can’t give you any workaround other than the clearing of the interrupted flag or a custom operator that suppresses cancellation if it comes after an onsuccess.
thanks!