This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() = runBlocking<Unit> { | |
val channel = ConflatedBroadcastChannel<Int>() | |
launch { | |
channel.consumeEach { | |
println("first: $it") | |
delay(500) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() = runBlocking { | |
val channel = Channel<Int>() | |
launch { | |
repeat(5) { | |
channel.send(it*it) | |
} | |
channel.close() | |
} | |
println("begin") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() = runBlocking<Unit> { | |
val channel = Channel<Int>() | |
launch { | |
for (i in 1..5) { | |
channel.send(i) | |
} | |
channel.close() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val channel = Channel<ItemType>(bufferSize) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SendChannel<in E> { | |
public suspend fun send(element: E) | |
public fun offer(element: E): Boolean | |
public fun close(cause: Throwable? = null): Boolean | |
} | |
interface ReceiveChannel<out E> { | |
public suspend fun receive(): E | |
public fun poll(): E? | |
public fun cancel() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (isActive) { | |
// print a message twice a second | |
if (System.currentTimeMillis() >= nextPrintTime) { | |
println("I'm sleeping ${i++} ...") | |
nextPrintTime += 500L | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity: AppCompatActivity(), CoroutineScope { | |
lateinit var job: Job | |
override val coroutineContext: CoroutineContext | |
get() = job + Dispatchers.Main | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
job = Job() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() = runBlocking<Unit> { | |
launchChildren() | |
println("body of main") | |
} | |
suspend fun launchChildren() = supervisorScope { | |
launch(Dispatchers.Default) { | |
repeat(5) { | |
println("coroutine 1: count $it") | |
if(it == 2) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() = runBlocking{ | |
launchChildren() | |
println("never gets printed") // this line never gets printed | |
} | |
suspend fun launchChildren(): Int = coroutineScope { | |
val first = async { | |
try { | |
firstValue() | |
} finally { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() = runBlocking<Unit>{ | |
launch { | |
withContext(NonCancellable) { | |
repeat(5) { | |
println("count: $it") | |
delay(500) | |
} | |
} | |
} |