1 // This file was automatically generated from channels.md by Knit tool. Do not edit. 2 package kotlinx.coroutines.guide.exampleChannel10 3 4 import kotlinx.coroutines.* 5 import kotlinx.coroutines.channels.* 6 <lambda>null7fun main() = runBlocking<Unit> { 8 val tickerChannel = ticker(delayMillis = 200, initialDelayMillis = 0) // create a ticker channel 9 var nextElement = withTimeoutOrNull(1) { tickerChannel.receive() } 10 println("Initial element is available immediately: $nextElement") // no initial delay 11 12 nextElement = withTimeoutOrNull(100) { tickerChannel.receive() } // all subsequent elements have 200ms delay 13 println("Next element is not ready in 100 ms: $nextElement") 14 15 nextElement = withTimeoutOrNull(120) { tickerChannel.receive() } 16 println("Next element is ready in 200 ms: $nextElement") 17 18 // Emulate large consumption delays 19 println("Consumer pauses for 300ms") 20 delay(300) 21 // Next element is available immediately 22 nextElement = withTimeoutOrNull(1) { tickerChannel.receive() } 23 println("Next element is available immediately after large consumer delay: $nextElement") 24 // Note that the pause between `receive` calls is taken into account and next element arrives faster 25 nextElement = withTimeoutOrNull(120) { tickerChannel.receive() } 26 println("Next element is ready in 100ms after consumer pause in 300ms: $nextElement") 27 28 tickerChannel.cancel() // indicate that no more elements are needed 29 } 30