RxJava Observable Single显示当前线程

时间:2018-09-20 14:09:45

标签: java multithreading rx-java

我试图了解可观察的工作原理,我想展示当前线程以更好地理解。整个应用程序是使用Java 8构建的,并使用了lambda表达式。没有足够的经验,我发现在这样的表达式中显示当前线程很麻烦:

.subscribeOn(Schedulers.io())                          
.observeOn(Schedulers.computation())
.flatMap(..... -> {

我喜欢这样写:

log.info("Current Thread:", Thread.currentThread().name)

但是我无法找到一种方法来实现它,如subscribeOn(Schedulers.io())和observeOn(Schedulers.computation())返回并返回Single,因此无法放置这样的内容:

.subscribeOn(Schedulers.io())
.log.info("Current Thread:", Thread.currentThread().name)
.observeOn(Schedulers.computation())
.log.info("Current Thread:", Thread.currentThread().name)
.flatMap(..... -> {

谢谢

1 个答案:

答案 0 :(得分:1)

您正在尝试仅记录当前线程,对吗?

只需将map用于日志并返回相同的值:

.subscribeOn(Schedulers.io())
.map(it -> {
    log.info("Current Thread:", Thread.currentThread().name)
    return it;
})
.observeOn(Schedulers.computation())
.map(it -> {
    log.info("Current Thread:", Thread.currentThread().name)
    return it;
})
.flatMap(..... -> {