我使用一次性flowable来发布和订阅项目。但当我尝试使用connectableflowable时,我无法向发射器发送取消信号。我如何理解flowable是在flowable.create方法中处理的?
您可以通过注解和取消注解“publish().autoconnect()”代码来查看场景。
Disposable disposable = Flowable.create(emitter -> {
AtomicBoolean isRunning = new AtomicBoolean(true);
AtomicInteger i = new AtomicInteger();
new Thread(() -> {
while (isRunning.get()) {
i.getAndIncrement();
System.out.println("Emitting:" + i.get());
emitter.onNext(i.get());
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
emitter.setCancellable(() -> {
System.out.println("Cancelled");
isRunning.set(false);
});
}, BackpressureStrategy.BUFFER)
.publish() //comment here
.autoConnect() //and here
.subscribe(s -> {
System.out.println("Subscribed:" + s);
});
Thread.sleep(10_000);
disposable.dispose();
Thread.sleep(100_000);
1条答案
按热度按时间vzgqcmou1#
有一个超负荷,让您访问
Disposable
要取消连接: