I'm trying to create an object which can execute some tasks sequentially in its own thread like it is a queue.
The following sample is just for demonstrating my setup and may be completely wrong.
class CoroutinesTest {
fun a() {
GlobalScope.launch {
println("a started")
delay(1000)
println("a completed")
}
}
fun b() {
GlobalScope.launch {
println("b started")
delay(2000)
println("b completed")
}
}
fun complex() {
a()
b()
}
}
fun main() {
runBlocking {
val coroutinesTest = CoroutinesTest()
coroutinesTest.complex()
delay(10000)
}
}
For now this code prints the following
a started
b started
a completed
b completed
which means a
and b
executed in parallel. Methods a
, b
and complex
can be called from different threads. Of course, the complex
method should also support this concept. For now, I need a mechanism that allows me to execute only one task at a moment, so I could get the following output:
a started
a completed
b started
b completed
I did some research and think that actor
with a Channel
can do what needed, but actor
for now is marked as obsolete (issue #87). I don't like the idea of using API that is subject to change, so I would like to do the thing in a common way.