Skip to content

Commit

Permalink
add coroutine/synchronized test
Browse files Browse the repository at this point in the history
  • Loading branch information
hg42 committed Jul 14, 2023
1 parent 414da0f commit 454ec5d
Showing 1 changed file with 80 additions and 22 deletions.
102 changes: 80 additions & 22 deletions app/src/androidTest/kotlin/research/Try_concurrent.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package research

import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.junit.Assert.assertEquals
Expand All @@ -9,26 +12,28 @@ import kotlin.system.measureTimeMillis

class Try_concurrent {

val n = 10 // number of coroutines to launch
val t = 100L // times an action is repeated by each coroutine
val n = 10 // number of jobs to launch
val t = 100L // time to wait in each coroutine

suspend fun massiveCoroutines(action: suspend () -> Unit) {
private var theMap = mutableMapOf<Int, Int>()

fun massiveCoroutines(dispatcher: CoroutineDispatcher, action: suspend () -> Unit) {
val time = measureTimeMillis {
coroutineScope { // scope for coroutines
runBlocking(dispatcher) {
repeat(n) {
launch {
action()
}
}
}
}
println("Completed $n actions in $time ms")
println("----------> Completed actions in $n coroutines in $time ms")
}

fun massiveThreads(action: () -> Unit) {
val jobs = mutableListOf<Thread>()
val time = measureTimeMillis {
repeat(n) {
repeat(n) { index ->
jobs.add(
Thread {
action()
Expand All @@ -37,40 +42,93 @@ class Try_concurrent {
}
jobs.forEach { it.join() }
}
println("Completed $n actions in $time ms")
println("----------> Completed actions in $n threads in $time ms")
}

fun theAction() {
val x = counter
//theMap[x] = (theMap[x] ?: 0) + 1
Thread.sleep(t)
counter = x+1
}

fun theAssertions() {
assertEquals(n, counter)
//repeat(n) {
// assertEquals(1, theMap[it])
//}
}

var mutex = Mutex()
object lock
var counter = 0

@Test
fun test_coroutine_withLock() = runBlocking {
fun test_coroutine_withLock(){
counter = 0
withContext(Dispatchers.Default) {
massiveCoroutines {
// protect each increment
massiveCoroutines(Dispatchers.Default) {
mutex.withLock {
val x = counter
delay(1000)
counter = x+1
}
theAction()
}
}
assertEquals(n, counter)
}

@Test
fun test_coroutine_io_withLock() {
counter = 0
massiveCoroutines(Dispatchers.IO) {
mutex.withLock {
theAction()
}
}
assertEquals(n, counter)
}

@Test
fun test_thread_synchronized() = runBlocking {
fun test_thread_withLock() {
counter = 0
massiveThreads {
// protect each increment
synchronized(counter) {
val x = counter
Thread.sleep(t)
counter = x+1
runBlocking {
mutex.withLock {
theAction()
}
}
}
assertEquals(n, counter)
}

@Test
fun test_thread_synchronized(){
counter = 0
massiveThreads {
synchronized(lock) {
theAction()
}
}
theAssertions()
}

@Test
fun test_coroutine_synchronized(){
counter = 0
massiveCoroutines(Dispatchers.Default) {
synchronized(lock) {
theAction()
}
}
theAssertions()
}

@Test
fun test_coroutine_io_synchronized(){
counter = 0
massiveCoroutines(Dispatchers.IO) {
synchronized(lock) {
theAction()
}
}
theAssertions()
}

}

0 comments on commit 454ec5d

Please sign in to comment.