1
我需要画出两个并发运行的线程的写入访问图。将这些访问的时间戳值对写入数组的最佳方式是什么,而不会干扰线程本身?正在写入的队列看起来像这样:分析Java中的线程行为
import java.util.concurrent.atomic.AtomicInteger;
class IQueue<T> {
AtomicInteger head = new AtomicInteger(0);
AtomicInteger tail = new AtomicInteger(0);
T[] items = (T[]) new Object[100];
public void enq(T x) {
int slot;
do {
slot = tail.get();
} while (! tail.compareAndSet(slot, slot+1));
items[slot] = x;
}
public T deq() throws EmptyException {
T value;
int slot;
do {
slot = head.get();
value = items[slot];
if (value == null)
throw new EmptyException();
} while (! head.compareAndSet(slot, slot+1));
return value;
}
public String toString() {
String s = "";
for (int i = head.get(); i < tail.get(); i++) {
s += items[i].toString() + "; ";
}
return s;
}
}
我想记录线程何时开始/停止写入。
使用'LinkedBlockingQueue' /'ArrayBlockingQueue'有什么问题? – pjp 2009-09-03 11:47:13
btrace可以作为代理程序预编译并附加到程序的开始部分。 – VonC 2009-09-03 12:46:24
这是一个教育演习。 – pypmannetjies 2009-09-03 12:52:33