首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >本地测试并发性

本地测试并发性
EN

Stack Overflow用户
提问于 2012-05-02 10:21:27
回答 1查看 110关注 0票数 0

出现了一个问题,我们一直无法确定。它看起来像是共享单例bean的并发问题,但这只是假设。

我需要一种在本地重新创建此错误的方法。只有当两个线程以千分之一秒的速度在一起处理时,它才会出现。我想知道是否有一种方法可以在本地测试这一点,而不必在调试模式中进行筛选。

我们的过程很简单。

它从一个主题中获取一个对象,然后丰富它,然后发送要发布到某个主题的新对象。我们有两个侦听器线程。

科技用

  • IDE eclipse
  • ldap主题
  • 代码Java/Spring
EN

回答 1

Stack Overflow用户

发布于 2012-05-10 16:35:51

细节太少,无法给出准确的答案。我将重构代码,以便将所有同步代码与业务逻辑分离开来。然后,在测试期间,您可以用调用yeld()方法的代码替换业务逻辑,并使用易失性/原子变量来检查在此代码点和特定时间是否存在预期线程数。然后使用任何并发测试框架(我喜欢multithreadedtc)。在下面,您可以找到我用来测试应该在队列上进行并发操作的算法的piority队列实现。

代码语言:javascript
复制
class YeldingHeap implements PriorityQueue<Integer> {

private AtomicInteger concurrentReads = new AtomicInteger();
private AtomicInteger concurrentWrites = new AtomicInteger();

@Override
public int size() {
    read();
    return 0;
}

@Override
public void insert(Integer element) {
    write();
}

@Override
public Integer popMax() {
    write();
    return null;
}

private void write() {
    int writes = concurrentWrites.incrementAndGet();
    int reads = concurrentReads.incrementAndGet();
    assertEquals(writes, 1, "more than 1 thread is writing");
    assertEquals(reads, 1, "other thread is reading while this thread is writing");
    Thread.yield();
    writes = concurrentWrites.decrementAndGet();
    reads = concurrentReads.decrementAndGet();
    assertEquals(writes, 0, "more than 1 thread is writing");
    assertEquals(reads, 0, "other thread is reading while this thread is writing");
}

private void read() {
    concurrentReads.incrementAndGet();
    int writes = concurrentWrites.get();
    assertEquals(writes, 0, "other thread is writing while this thread is reading");
    Thread.yield();
    writes = concurrentWrites.get();
    assertEquals(writes, 0, "other thread is writing while this thread is reading");
    concurrentReads.decrementAndGet();
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10411817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档