0

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

我需要一种在本地重新创建此错误的方法。只有当两个线程以相同的千分之一秒同时处理时,它才会浮出水面。我想知道有没有办法在本地测试这个而不必在调试模式下进行钓鱼。

我们的过程很简单。

它从主题过程中获取对象并丰富然后发送要发布到主题的新对象。我们有两个监听线程。

使用的技术

  • IDE日食
  • LDAP 主题
  • 代码 Java/Spring
4

1 回答 1

0

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

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

}
于 2012-05-10T16:35:51.937 回答