简体   繁体   English

在Linux上调整Java中的上下文切换频率

[英]Adjust context switch frequency in Java on Linux

I'm looking into a potential concurrency issue in some binary Java code. 我正在研究一些二进制Java代码中的潜在并发问题。 The code sometimes has a strange behavior, but I'm not sure if it's actually due to concurrency issues in the code itself, or if it's something else. 代码有时会有一个奇怪的行为,但我不确定它是否真的是由于代码本身的并发问题,或者它是否是其他东西。 I haven't been able to reproduce the strange behavior myself, but only seen it happening in our log files. 我自己无法重现这种奇怪的行为,但只看到它发生在我们的日志文件中。

Is there a way to up the context switch frequency of the JVM, in order to surface potential concurrency issues with higher probability? 有没有办法提高JVM的上下文切换频率,以便以更高的概率表现出潜在的并发问题? Preferably doing this without explicitly inserting calls to Thread::yeild or Thread::sleep in the code. 最好不要在代码中显式插入对Thread :: yeild或Thread :: sleep的调用。

I think the Thread-weaver test framework can help you. 我认为Thread-weaver测试框架可以帮助您。 It's been a while since I've experimented with it, but it does allow you to force a particular scheduling. 我已经尝试过它已经有一段时间了,但它确实允许你强制进行特定的调度。 It can also be run in a sort of default mode where a lot of interleavings between two methods are tested. 它也可以在某种默认模式下运行,其中测试两种方法之间的大量交错。

This is a simple example test (using default interleaving) 这是一个简单的示例测试(使用默认交错)

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM