我试图了解可观察的工作原理,我想展示当前线程以更好地理解。整个应用程序是使用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(..... -> {
谢谢
答案 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(..... -> {