RxJava-flatMap收费表
下面是使用flatMap和toList()的示例代码。有人能告诉我下面的代码是否有时会挂起吗?(使用类似方法的实际代码挂起在我的生产环境中),我无法上传我的实际代码,所以我只给出示例RxJava-flatMap收费表,java,rx-java,Java,Rx Java,下面是使用flatMap和toList()的示例代码。有人能告诉我下面的代码是否有时会挂起吗?(使用类似方法的实际代码挂起在我的生产环境中),我无法上传我的实际代码,所以我只给出示例 public class RxZipFlatMapTest { @Test public void testZipFlatMap() { Observable.zip(getDummyList(), getActualList(), new Func2<String, String, String&
public class RxZipFlatMapTest {
@Test
public void testZipFlatMap() {
Observable.zip(getDummyList(), getActualList(), new Func2<String, String, String>() {
@Override
public String call(String s, String s2) {
return "hi " + s + " " + s2;
}
}).toBlocking().first();
System.out.println("Done");
}
private Observable<String> getActualList() {
return Observable.create(new TestObservable()).subscribeOn(Schedulers.newThread());
}
private Observable<String> getDummyList() {
return Observable.create(new TestObservable2()).subscribeOn(Schedulers.newThread());
}
private abstract class TestAbstractObservable implements Observable.OnSubscribe<String> {
}
private class TestObservable extends TestAbstractObservable {
@Override
public void call(Subscriber<? super String> subscriber) {
Set<String> strList = new HashSet<>();
strList.addAll(Arrays.asList("pranesh", "uma"));
Observable.from(strList).flatMap(Async.toAsync(new Func1<String, Object>() {
@Override
public Object call(String s) {
return s;
}
}, Schedulers.newThread())).toList().toBlocking().single();
subscriber.onNext("hello ");
subscriber.onCompleted();
}
}
private class TestObservable2 extends TestAbstractObservable {
@Override
public void call(Subscriber<? super String> subscriber) {
Set<String> strList = new HashSet<>();
strList.addAll(Arrays.asList("sunu", "lakshmi"));
Observable.from(strList).flatMap(Async.toAsync(new Func1<String, Object>() {
@Override
public Object call(String s) {
return s;
}
}, Schedulers.newThread())).toList().toBlocking().single();
subscriber.onNext("hi ");
subscriber.onCompleted();
}
}
}
谢谢
Pranesh在这个例子中,我看不到任何挂起的东西。如果您使用的是
toList
,请确保您的源代码实际完成,否则toList
可能永远不会发出任何信息。另外,请确保您拥有最新的RxJava(撰写本文时为1.2.5)。谢谢@akarnokd,我无法用我的测试用例重现这个问题,我将按照您的建议检查更多关于toList的内容。@akarnokd-这里toList的源代码是flatMap的输出,对吗?你认为有什么方法可以复制这一点吗?Async.toAsync
是有限的,所以flatMap
,的输入是可观察的。在你的例子中,从(strList)
在你的原始代码中可能是无限的。谢谢@akarnokd,我不确定strList是无限的,我也不知道这是怎么可能的。strList值是定义的大小。
Thread 29724: (state = BLOCKED)
- sun.misc.Unsafe.park(boolean, long) @bci=0 (Compiled frame; information may be imprecise)
- java.util.concurrent.locks.LockSupport.park(java.lang.Object) @bci=14, line=175 (Compiled frame)
- java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt() @bci=1, line=836 (Compiled frame)
- java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(int) @bci=72, line=997 (Compiled frame)
- java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(int) @bci=24, line=1304 (Compiled frame)
- java.util.concurrent.CountDownLatch.await() @bci=5, line=231 (Compiled frame)
- rx.observables.BlockingObservable.blockForSingle(rx.Observable) @bci=46, line=461 (Compiled frame)
- rx.observables.BlockingObservable.single() @bci=8, line=341 (Compiled frame)