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