diff --git a/src/main/kotlin/demos/channel.kt b/src/main/kotlin/demos/channel.kt new file mode 100644 index 0000000..46983d8 --- /dev/null +++ b/src/main/kotlin/demos/channel.kt @@ -0,0 +1,33 @@ +package demos + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.isActive +import kotlin.coroutines.CoroutineContext +import kotlin.random.Random + +class ChannelDemo: CoroutineScope{ + override val coroutineContext: CoroutineContext + get() = TODO("Not yet implemented") + + val channel = Channel() + val channel2 = Channel() + + suspend fun produce(){ + while (isActive){ + channel.send(Random.nextDouble()) + } + } + + suspend fun consume(){ +// while (isActive){ +// val res = channel.receive() +// println(res) +// } + for(res in channel){ + println(res) + channel2.send(res) + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/demos/coroutineContext.kt b/src/main/kotlin/demos/coroutineContext.kt new file mode 100644 index 0000000..6fc5e30 --- /dev/null +++ b/src/main/kotlin/demos/coroutineContext.kt @@ -0,0 +1,52 @@ +package demos + +import kotlinx.coroutines.* +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.coroutineContext + +//fun doSomething(){ +// coroutineContext +//} + +suspend fun doSomethingSuspended(name: String) { + println("$name : ") + println(coroutineContext) + println(coroutineContext[Job]) +} + +class MyApplication(parentContext: CoroutineContext) : CoroutineScope { + override val coroutineContext: CoroutineContext = + parentContext + SupervisorJob(parentContext[Job]) + CoroutineName("MyApplication") +} + +fun main(): Unit { + runBlocking { + val myApplication = MyApplication(coroutineContext) + + myApplication.launch(Dispatchers.IO + CoroutineName("A")) { + delay(20) + doSomethingSuspended("A") + } + +// val job = myApplication.launch(CoroutineName("B")) { +// doSomethingSuspended("B") +// error("F") +// } + + val result = async { + delay(20) + 22 + } + + println(result.await()) +// supervisorScope { +// launch(CoroutineExceptionHandler { coroutineContext, throwable -> +// println("Fail") +// }) { +// error("ddd") +// } +// } + } + +} + diff --git a/src/main/kotlin/demos/flow.kt b/src/main/kotlin/demos/flow.kt new file mode 100644 index 0000000..8c7a02e --- /dev/null +++ b/src/main/kotlin/demos/flow.kt @@ -0,0 +1,50 @@ +package demos + +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.* +import kotlin.random.Random + +suspend fun main() { + coroutineScope { + val f = flow { + while (true) { + val value = Random.nextDouble() + delay(10) + println("Emitted $value!") + emit(value) + } + } + + val res = f + .map { it + 1 } + .flatMapConcat { + flow { + emit(it) + emit(it+1) + } + }.onEach { + delay(20) + println("Collected $it!") + }.launchIn(this) + + delay(500) + res.cancel() + + val f2 = channelFlow { + repeat(10){ + val value = Random.nextInt() + println("Hot int: $value") + send(value) + } + } + + f2.take(1).collect { + + } + + val sharedFlow = MutableSharedFlow() + sharedFlow.emit(2.0) + } +} \ No newline at end of file