Kotlin flow withTimeout repeat

阳光穿透心脏的1/2处 2023-10-14 13:26 18阅读 0赞

Kotlin flow withTimeout repeat

  1. import kotlinx.coroutines.*
  2. import kotlinx.coroutines.flow.flow
  3. import kotlinx.coroutines.flow.onCompletion
  4. import kotlinx.coroutines.flow.onEach
  5. import kotlinx.coroutines.flow.onEmpty
  6. fun main() {
  7. runBlocking {
  8. val fa = flow {
  9. withTimeout(5000) {
  10. repeat(3) {
  11. delay(1000)
  12. emit(it)
  13. }
  14. }
  15. }.onEach {
  16. println("a-onEach-$it")
  17. }.onCompletion {
  18. println("a onCompletion")
  19. }.onEmpty {
  20. println("a onEmpty")
  21. }.collect {
  22. println("a-$it")
  23. }
  24. val fb = flow {
  25. withTimeout(2000) {
  26. repeat(3) {
  27. delay(1000)
  28. emit(it)
  29. }
  30. }
  31. }.onEach {
  32. println("b-onEach-$it")
  33. }.onCompletion {
  34. println("b onCompletion")
  35. }.onEmpty {
  36. println("b onEmpty")
  37. }.collect {
  38. println("b-$it")
  39. }
  40. }
  41. }

a-onEach-0
a-0
a-onEach-1
a-1
a-onEach-2
a-2
a onCompletion
b-onEach-0
b-0
b onCompletion
Exception in thread “main” kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 2000 ms
at kotlinx.coroutines.TimeoutKt.TimeoutCancellationException(Timeout.kt:184)
at kotlinx.coroutines.TimeoutCoroutine.run(Timeout.kt:154)
at kotlinx.coroutines.EventLoopImplBase$DelayedRunnableTask.run(EventLoop.common.kt:508)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:284)
at kotlinx.coroutines.DefaultExecutor.run(DefaultExecutor.kt:108)
at java.base/java.lang.Thread.run(Thread.java:1623)

Process finished with exit code 1

kotlin协程flow filter map flowOn zip combine(1)_zhangphil的博客-CSDN博客一、flow ,emit,onCompletion,collect。四、map,重组改写数据。八、conflate 合并。九、debounce去重。二、函数作为flow。favicon32.icohttps://blog.csdn.net/zhangphil/article/details/130084723kotlin协程flow retry retryWhen(2)_zhangphil的博客-CSDN博客kotlin协程flow retry retryWhen(2)二、retryWhen。favicon32.icohttps://blog.csdn.net/zhangphil/article/details/130086523kotlin协程flow retryWhen当功能函数加载失败后重试(3)_zhangphil的博客-CSDN博客kotlin协程flow retryWhen当功能函数加载失败后重试(3)加载一次失败,重试1次成功。favicon32.icohttps://blog.csdn.net/zhangphil/article/details/130092299kotlin协程flow retry功能函数返回失败后重试(4)_zhangphil的博客-CSDN博客一、flow ,emit,onCompletion,collect。kotlin协程flow retryWhen当功能函数加载失败后重试(3)_zhangphil的博客-CSDN博客。kotlin协程flow retryWhen当功能函数加载失败后重试(3)加载一次失败,重试1次成功。kotlin协程flow retry retryWhen(2)_zhangphil的博客-CSDN博客。kotlin协程flow retry retryWhen(2)二、retryWhen。初次加载失败,重试两次成功。favicon32.icohttps://blog.csdn.net/zhangphil/article/details/130093111

发表评论

表情:
评论列表 (有 0 条评论,18人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Repeatable Read

    在Repeatable Read隔离级别下,一个事务可能会遇到幻读(Phantom Read)的问题。 幻读是指,在一个事务中,第一次查询某条记录,发现没有,但是,当试图更新