/** Has multiple threads add(), remove() and peek() elements to/from the queue */
public static void testConcurrentAccess() {
final Queue queue=new Queue();
final int NUM_THREADS=10;
final int INTERVAL=5000;
Writer[] writers=new Writer[NUM_THREADS];
Reader[] readers=new Reader[NUM_THREADS];
int[] writes=new int[NUM_THREADS];
int[] reads=new int[NUM_THREADS];
long total_reads=0, total_writes=0;
for(int i=0; i < writers.length; i++) {
readers[i]=new Reader(i, reads, queue);
readers[i].start();
writers[i]=new Writer(i, writes, queue);
writers[i].start();
}
Util.sleep(INTERVAL);
System.out.println("current queue size=" + queue.size());
for(int i=0; i < writers.length; i++) {
writers[i].stopThread();
}
for(int i=0; i < readers.length; i++) {
readers[i].stopThread();
}
queue.close(false); // will cause all threads still blocking on peek() to return
System.out.println("current queue size=" + queue.size());
for(int i=0; i < writers.length; i++) {
try {
writers[i].join(300);
readers[i].join(300);