简体   繁体   English

RxJava Observable Single显示当前线程

[英]RxJava Observable Single show current thread

I'm trying to understand how observable works and I'd like to show the current thread to get a better understanding. 我试图了解可观察的工作原理,并且我想展示当前线程以更好地理解。 The whole app is built with java 8 and make use of lambda expressions. 整个应用程序是使用Java 8构建的,并使用了lambda表达式。 No having much experience with it I'm finding some troubles to show my current thread in an expression like this: 没有足够的经验,我发现在这样的表达式中显示当前线程有一些麻烦:

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

I like to put something like this: 我喜欢这样写:

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

But I couldn't find a way to do it as subscribeOn(Schedulers.io()) and observeOn(Schedulers.computation()) return and Single and there is no way to put something like this: 但是我找不到办法做到这一点,因为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(..... -> {

Thanks 谢谢

You are trying just to log the current Thread, correct? 您正在尝试仅记录当前线程,对吗?

Just use map for log and return the same value: 只需使用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(..... -> {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM