在Android上调试时,RxJava缓存线程中的InterruptedException

在Android上调试时,RxJava缓存线程中的InterruptedException,第1张

概述有时当我调试我的应用程序时,我在RxCachedThreadScheduler-1中遇到InterruptedException.这是跟踪: Fatal Exception: java.lang.InterruptedException at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.r 有时当我调试我的应用程序时,我在RxCachedThreadScheduler-1中遇到InterruptedException.这是跟踪:
Fatal Exception: java.lang.InterruptedException       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:1991)       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2025)       at java.util.concurrent.ScheduledThreadPoolExecutor$DelayeDWorkQueue.take(ScheduledThreadPoolExecutor.java:1048)       at java.util.concurrent.ScheduledThreadPoolExecutor$DelayeDWorkQueue.take(ScheduledThreadPoolExecutor.java:776)       at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035)

我有一个自定义视图,我在其中订阅我的observable,如下所示:

@OverrIDeprotected voID onAttachedToWindow() {    super.onAttachedToWindow();    sub = FollowHandler.getInstance().getobservable()            .filter(new Func1<FollowEvent,Boolean>() {                @OverrIDe                public Boolean call(FollowEvent followEvent) {                    if(followEvent == null || followEvent.user == null                            || user == null)                        return false;                    return followEvent.user.ID == user.ID;                }            })            .observeOn(AndroIDSchedulers.mainThread())            .subscribe(new Observer<FollowEvent>() {                @OverrIDe                public voID onCompleted() {}                @OverrIDe                public voID onError(Throwable e) {}                @OverrIDe                public voID onNext(FollowEvent followEvent) {                    reactToThisNiceEvent(followEvent);                }            });}@OverrIDeprotected voID onDetachedFromWindow() {    super.onDetachedFromWindow();    if(sub != null)        sub.unsubscribe();}

这是可观察的:

eventSubject.asObservable()        .observeOn(Schedulers.io())        .doOnNext(new Action1<FollowEvent>() {            @OverrIDe            public voID call(FollowEvent followEvent) {                if(followEvent != null)                    doSomethingNice(followEvent);            }        })        .share();

其中eventSubject是一个简单的PublishSubject.
我使用RxAndroID 1.1.0和RxJava 1.1.0.

有谁知道为什么会这样?

解决方法 我不确定,为什么会发生,但尝试这样做:
sub = FollowHandler.getInstance().getobservable()            .filter(...)            .subscribeOn(Schedulers.io())    //  <<<<<<<<<<            .observeOn(AndroIDSchedulers.mainThread())            .subscribe(...);

另外,我认为你不需要share():

eventSubject.asObservable()        .doOnNext(...)        .subscribeOn(Schedulers.io())   // <<<<< subscribeOn instead of observeOn,but actually,you don't need it here...        .share();     // <<<<< remove it

如上所述,我经常使用Subject作为eventbus.我从来没有遇到过这样的问题.

附:
在onDetachedFromWindow()中,如果你检查订阅是否取消订阅会更好.我知道这个方法在主线程中调用,并且对这个Subscription的并发访问是不可能的,但我认为它的风格很好:

if(sub != null && !sub.isUnsubscribed())        sub.unsubscribe();
总结

以上是内存溢出为你收集整理的在Android上调试时,RxJava缓存线程中的InterruptedException全部内容,希望文章能够帮你解决在Android上调试时,RxJava缓存线程中的InterruptedException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1135643.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存