package com.glassthought.sandboxclass RaceConditionInducer { private var value = 0 // @Synchronized // would fix this example race condition. fun incrementValue() { val storedValue = value Thread.sleep(10) println("Incrementing value from storedValue=$storedValue to ${storedValue + 1}") value = storedValue + 1 } fun getValue() = value}fun main() { val raceConditionInducer = RaceConditionInducer() val threads = (1..10).map { Thread { raceConditionInducer.incrementValue() } } threads.forEach { it.start() } threads.forEach { it.join() } println("Value: ${raceConditionInducer.getValue()}")}
Command to reproduce:
gt.sandbox.checkout.commit 0b65495f3299ff186efc \&& cd "${GT_SANDBOX_REPO}" \&& cmd.run.announce "./gradlew run"
Recorded output of command:
> Task :app:checkKotlinGradlePluginConfigurationErrors SKIPPED> Task :app:processResources NO-SOURCE> Task :app:compileKotlin> Task :app:compileJava NO-SOURCE> Task :app:classes UP-TO-DATE> Task :app:runIncrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Value: 1BUILD SUCCESSFUL in 1s2 actionable tasks: 2 executed
package com.glassthought.sandboximport kotlinx.coroutines.*import kotlin.random.Randomclass RaceConditionInducer { private var value = 0 // Suspending function to simulate some work and induce a race condition suspend fun incrementValue() { val storedValue = value // Simulate delay to increase the likelihood of race conditions delay(10) println("Incrementing value from storedValue=$storedValue to ${storedValue + 1}") value = storedValue + 1 } fun getValue() = value}fun main() = runBlocking { val raceConditionInducer = RaceConditionInducer() // Launch multiple coroutines to increment the value val jobs = List(10) { GlobalScope.launch { raceConditionInducer.incrementValue() } } // Wait for all coroutines to complete jobs.forEach { it.join() } println("Final Value: ${raceConditionInducer.getValue()}")}
Command to reproduce:
gt.sandbox.checkout.commit a82a70441214360361c9 \&& cd "${GT_SANDBOX_REPO}" \&& cmd.run.announce "./gradlew run"
Recorded output of command:
> Task :app:checkKotlinGradlePluginConfigurationErrors SKIPPED> Task :app:compileKotlin UP-TO-DATE> Task :app:compileJava NO-SOURCE> Task :app:processResources NO-SOURCE> Task :app:classes UP-TO-DATE> Task :app:runIncrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Incrementing value from storedValue=0 to 1Final Value: 1BUILD SUCCESSFUL in 704ms2 actionable tasks: 1 executed, 1 up-to-date