Simon Stahl
09/29/2023, 3:07 AMMutex
runs waiting tasks in a first come, first serve order as promised, but having some problemsSimon Stahl
09/29/2023, 3:07 AMMutex
class mentions: "The mutex created is fair: lock is granted in first come, first served order."Simon Stahl
09/29/2023, 3:07 AMSimon Stahl
09/29/2023, 3:09 AMclass MutexTest {
@Test
fun test2() = runTest {
val subject = MyTestClass()
val jobs1 = (1..10).map {
launch(<http://Dispatchers.Default.io|Dispatchers.Default.io>) {
subject.test("test1-$it")
}
}
// Make sure all coroutines have finished
jobs1.forEach { it.join() }
// List are expected to be the same
println(subject.hitList)
println(subject.execList)
}
}
private class MyTestClass {
private val mutex = Mutex()
val hitList = mutableListOf<String>()
val execList = mutableListOf<String>()
suspend fun test(param: String) {
hitList.add(param)
mutex.lock()
// mutex.withLock {
Thread.sleep(100)
execList.add(param)
// }
mutex.unlock()
}
}
Simon Stahl
09/29/2023, 3:10 AMtest()
method gets hit, the other one records the order in which the functionality is executed within the mutex lock. I would expect both lists to be equal at the end of the test, but unfortunately they are notSimon Stahl
09/29/2023, 3:11 AM[test1-1, test1-2, test1-3, test1-4, test1-5, test1-6, test1-7, test1-8, test1-9, test1-10]
[test1-1, test1-4, test1-2, test1-3, test1-10, test1-9, test1-7, test1-8, test1-6, test1-5]
Simon Stahl
09/29/2023, 3:12 AMMutex
actually runs the task in the wrong order?kevin.cianfarini
09/29/2023, 3:35 AMSimon Stahl
09/29/2023, 3:43 AMJoffrey
09/29/2023, 7:09 AMSimon Stahl
09/29/2023, 5:55 PMSimon Stahl
09/29/2023, 5:57 PMclass MutexTest {
@Test
fun test() = runTest {
val subject = MyTestClass()
val jobs1 = (1..10).map {
launch {
subject.test1("test1-$it")
}
// I know i only map to the second one, but that's fine
launch {
subject.test2("test2-$it")
}
}
// Make sure all coroutines have finished
jobs1.forEach { it.join() }
}
}
private class MyTestClass {
private val mutex = Mutex()
suspend fun test1(param: String) {
println("Hitting test1: $param")
mutex.withLock {
println("Executing test1: $param")
delay(100)
Thread.sleep(100)
println("Done test1: $param")
}
}
suspend fun test2(param: String) {
println("Hitting test2: $param")
mutex.withLock {
println("Executing test2: $param")
delay(100)
println("Done test2: $param")
}
}
}
Simon Stahl
09/29/2023, 5:58 PMHitting test1: test1-1
Executing test1: test1-1
Hitting test2: test2-1
Hitting test1: test1-2
Hitting test2: test2-2
Hitting test1: test1-3
Hitting test2: test2-3
Hitting test1: test1-4
Hitting test2: test2-4
Hitting test1: test1-5
Hitting test2: test2-5
Hitting test1: test1-6
Hitting test2: test2-6
Hitting test1: test1-7
Hitting test2: test2-7
Hitting test1: test1-8
Hitting test2: test2-8
Hitting test1: test1-9
Hitting test2: test2-9
Hitting test1: test1-10
Hitting test2: test2-10
Done test1: test1-1
Executing test2: test2-1
Done test2: test2-1
Executing test1: test1-2
Done test1: test1-2
Executing test2: test2-2
Done test2: test2-2
Executing test1: test1-3
Done test1: test1-3
Executing test2: test2-3
Done test2: test2-3
Executing test1: test1-4
Done test1: test1-4
Executing test2: test2-4
Done test2: test2-4
Executing test1: test1-5
Done test1: test1-5
Executing test2: test2-5
Done test2: test2-5
Executing test1: test1-6
Done test1: test1-6
Executing test2: test2-6
Done test2: test2-6
Executing test1: test1-7
Done test1: test1-7
Executing test2: test2-7
Done test2: test2-7
Executing test1: test1-8
Done test1: test1-8
Executing test2: test2-8
Done test2: test2-8
Executing test1: test1-9
Done test1: test1-9
Executing test2: test2-9
Done test2: test2-9
Executing test1: test1-10
Done test1: test1-10
Executing test2: test2-10
Done test2: test2-10
Joffrey
09/29/2023, 10:32 PMCoroutineStart.UNDISPATCHED
, so you guarantee the order in which they reach the suspension point.Joffrey
09/29/2023, 10:33 PM