I need to draw a graph of write accesses of two concurrently running threads. What is the best way to write a timestamp value pair of these accesses to an array, without interfering with the threads themselves? The queue that is being written to looks like this:
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;
}
}
I'd like to record whenever a thread starts/stops writing.