我正在rxjava代码中为调度程序线程池使用java.util.concurrent.executorservice:
public class Test {
ExecutorService poolA = newFixedThreadPool(10, threadFactory("Scheduler-A-%d"));
Scheduler schedulerA = Schedulers.from(poolA);
private ThreadFactory threadFactory(String pattern) {
return new ThreadFactoryBuilder()
.setNameFormat(pattern).build();
}
@Test
public void testSubscribeOn() {
log("Starting");
final Observable<String> obs = simple();
log("Created");
obs
.doOnNext(Utils::log)
.map(x -> x+1)
.doOnNext(Utils::log)
.subscribeOn(schedulerA)
.map(x -> x+2)
.doOnNext(Utils::log)
.subscribe(
x -> log("Got " + x),
Throwable::printStackTrace,
() -> log("Completed"));
log("exiting");
}
}
public class Utils {
private static long start = System.currentTimeMillis();
public static void log(Object label) {
System.out.println(
System.currentTimeMillis() - start + "\t| " +
Thread.currentThread().getName() + "\t| " +
label);
}
}
我观察到的是,主线程在调度程序线程开始工作之前退出,因此不显示来自调度程序线程的输出。据我所知,executorservice线程不是守护进程线程,那么为什么会出现这种行为呢?
暂无答案!
目前还没有任何答案,快来回答吧!