出现了一个问题,我们一直无法确定。它看起来像是共享单例bean的并发问题,但这只是假设。
我需要一种在本地重新创建此错误的方法。只有当两个线程以千分之一秒的速度在一起处理时,它才会出现。我想知道是否有一种方法可以在本地测试这一点,而不必在调试模式中进行筛选。
我们的过程很简单。
它从一个主题中获取一个对象,然后丰富它,然后发送要发布到某个主题的新对象。我们有两个侦听器线程。
科技用
发布于 2012-05-10 16:35:51
细节太少,无法给出准确的答案。我将重构代码,以便将所有同步代码与业务逻辑分离开来。然后,在测试期间,您可以用调用yeld()方法的代码替换业务逻辑,并使用易失性/原子变量来检查在此代码点和特定时间是否存在预期线程数。然后使用任何并发测试框架(我喜欢multithreadedtc)。在下面,您可以找到我用来测试应该在队列上进行并发操作的算法的piority队列实现。
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();
}
}https://stackoverflow.com/questions/10411817
复制相似问题