Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何阻止rx java runnable发出?_Java_Rx Java_Rx Java2_Graphql Java - Fatal编程技术网

如何阻止rx java runnable发出?

如何阻止rx java runnable发出?,java,rx-java,rx-java2,graphql-java,Java,Rx Java,Rx Java2,Graphql Java,我可以观察到以下情况: ScheduledExecutorService executorService = Executors.newScheduledThreadPool( 1 ); Observable<List<Widget>> findWidgetsObservable = Observable.create( emitter -> { executorService.scheduleWithFixedDelay(

我可以观察到以下情况:

      ScheduledExecutorService executorService = Executors.newScheduledThreadPool( 1 );
      Observable<List<Widget>> findWidgetsObservable = Observable.create( emitter -> {
         executorService.scheduleWithFixedDelay( emitFindWidgets( emitter, 0, 30, TimeUnit.SECONDS );
      } );

     private Runnable emitFindWidgets( ObservableEmitter<List<Widgets>> emitter ) {
        return () -> {
              emitter.onNext( Collections.emptyList() ); // dummy empty array
        };
     }
但是,Runnable继续无限期地省略。有什么方法可以解决这个问题并完全停止排放


我有一个想法:scheduleWithFixedDelay返回一个ScheduledFuture,它有一个cancel()方法,但我不确定当调度本身被限定在一个可观察的范围内时,我是否可以这样做!非常感谢您的帮助。

runnable将继续发出,因为您正在调度一个不知道/不绑定到可观察流的调度器上的发射

当您处理您的连接时,您停止从上游接收项目,因为到上游可观察的连接被切断。但是,由于您正在安排发射器在单独的调度器上重复运行,因此runnable将继续运行

您可以使用自定义计划程序描述自定义计划行为,并将其传递到subscribeOn(您的自定义计划程序)

另外,您提到可以在
doOnDispose()
中的
上调用
取消()

但是您应该在可观察链中显式地切换调度程序。否则,调试会变得更加困难。

您看过Javadocs中的示例了吗?
      ConnectableObservable<List<Widget>> connectableObservable = findWidgetsObservable.share().publish();
      Disposable connectionDisposable = connectableObservable.connect();
      return connectableObservable.toFlowable( BackpressureStrategy.LATEST )
    Disposable connectionDisposable = connectableObservable.connect();
    return connectableObservable.toFlowable( BackpressureStrategy.LATEST ).doOnCancel( () -> {
             findWidgetsObservable.toFuture().cancel( true );
             connectionDisposable.dispose();
    })