Setting the context switching frequency in Java on Linux

I am considering a potential concurrency problem in some binary Java code. Sometimes the code has strange behavior, but I'm not sure if this is really due to concurrency problems in the code itself, or if it is something else. I myself could not reproduce the strange behavior, but I saw how this happens in our log files.

Is there a way to increase the frequency of the JVM context switch so that concurrency surface potential problems are more likely? It is preferable to do this without explicitly inserting calls into Thread :: yeild or Thread :: sleep in the code.

+4
source share
1 answer

, Thread-weaver . , , . , .

( )

public class MyListTest {
    private MyList<String> list;

    @Test
    public void testThreading() {
        AnnotatedTestRunner runner = new AnnotatedTestRunner();
        // Run all Weaver tests in this class, using MyList as the Class Under Test.
        runner.runTests(this.getClass(), MyList.class);
    }

    @ThreadedBefore
    public void before() {
        list = new MyList<>();
    }

    @ThreadedMain
    public void mainThread() {
        list.putIfAbsent("A");
    }

    @ThreadedSecondary
    public void secondThread() {
        list.putIfAbsent("A");
    }

    @ThreadedAfter
    public void after() {
        assertThat(list).hasSize(1);
    }
}
+1

Source: https://habr.com/ru/post/1652298/


All Articles