}
/** Multiple threads call remove(), one threads then adds an element. Only 1 thread should actually terminate
* (the one that has the element) */
public static void testBarrierWithTimeOut() throws QueueClosedException {
final Queue queue=new Queue();
RemoveOneItemWithTimeout[] removers=new RemoveOneItemWithTimeout[10];
int num_dead=0;
for(int i=0; i < removers.length; i++) {
removers[i]=new RemoveOneItemWithTimeout(i, 15000, queue);
removers[i].start();
}
System.out.println("-- adding element 99");
queue.add(new Long(99));
System.out.println("-- adding element 100");
queue.add(new Long(100));
long target_time=System.currentTimeMillis() + 10000L;
do {
int num_rsps=0;
for(int i=0; i < removers.length; i++) {
if(removers[i].getRetval() != null)
num_rsps++;
}
if(num_rsps == 2)
break;
Util.sleep(500);
}
while(target_time > System.currentTimeMillis());
Util.sleep(3000);
for(int i=0; i < removers.length; i++) {
System.out.println("remover #" + i + " is " + (removers[i].isAlive() ? "alive" : "terminated"));
if(!removers[i].isAlive()) {
num_dead++;
}
}
assert num_dead == 2 : "num_dead should have been 2 but was " + num_dead;
System.out.println("closing queue - causing all remaining threads to terminate");
queue.close(false); // will cause all threads still blocking on remove() to return
Util.sleep(500);
num_dead=0;
for(int i=0; i < removers.length; i++) {
System.out.println("remover #" + i + " is " + (removers[i].isAlive()? "alive" : "terminated"));