1 /* <lambda>null2 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 6 package kotlinx.coroutines.rx2.guide.basic05 7 8 import io.reactivex.schedulers.* 9 import kotlinx.coroutines.* 10 import kotlinx.coroutines.rx2.* 11 import kotlin.coroutines.* 12 13 fun main() = runBlocking<Unit> { 14 // coroutine -- fast producer of elements in the context of the main thread 15 val source = rxFlowable { 16 for (x in 1..3) { 17 send(x) // this is a suspending function 18 println("Sent $x") // print after successfully sent item 19 } 20 } 21 // subscribe on another thread with a slow subscriber using Rx 22 source 23 .observeOn(Schedulers.io(), false, 1) // specify buffer size of 1 item 24 .doOnComplete { println("Complete") } 25 .subscribe { x -> 26 Thread.sleep(500) // 500ms to process each item 27 println("Processed $x") 28 } 29 delay(2000) // suspend the main thread for a few seconds 30 } 31