Can Lincheck check that 2 function use the same synchronized key ? Write working example
- User
Can Lincheck check that 2 function use the same synchronized key ? Write working example
- Assistant
Lincheck is a library for testing concurrent Java code for the correctness of its execution. It doesn't directly check if two functions use the same synchronized key. However, you can test the concurrent correctness of your code even if it uses synchronized methods or blocks.
Below is a working example of two synchronized methods that utilize the same object lock and can be tested with Lincheck:
import org.jetbrains.kotlinx.lincheck.LinChecker; import org.jetbrains.kotlinx.lincheck.annotations.Operation; import org.jetbrains.kotlinx.lincheck.strategy.stress.StressCTest; import org.jetbrains.kotlinx.lincheck.strategy.stress.StressOptions; import org.jetbrains.kotlinx.lincheck.verifier.VerifierState; @StressCTest public class SynchronizedExampleTest { public static void main(String[] args) { LinChecker.check(SynchronizedExampleTest.class, new StressOptions().iterations(100).invocationsPerIteration(50)); } private SynchronizedExample example = new SynchronizedExample(); @Operation public int function1() { return example.function1(); } @Operation public int function2() { return example.function2_globalLock(); } }