sema = new Semaphore(0);
final int NTHREADS = 10;
Blocker ta[] = new Blocker[NTHREADS];
for (int i = 0; i < NTHREADS; i++) {
ta[i] = new Blocker(sema);
}
for (int i = 0; i < NTHREADS; i++) {
assertTrue("blocked initially", !ta[i].isBlocked);
}
for (int i = 0; i < NTHREADS; i++) {
ta[i].start();
}
sleep(100);
for (int i = 0; i < NTHREADS; i++) {
assertTrue("not blocked after start", ta[i].isBlocked);
}
sema.release();
sema.release();
sema.release();
sleep(100);
int count = 0;
for (int i = 0; i < NTHREADS; i++) {
if (ta[i].isBlocked)
++count;
}
assertEquals("blocked " + count + "instead of 7", 7, count);
sema.release();
sema.release();
sema.release();
sema.release();
sema.release();
sema.release();
sema.release();
sleep(100);
count = 0;
for (int i = 0; i < NTHREADS; i++) {
if (ta[i].isBlocked)
++count;
}
assertEquals("all not unblocked", 0, count);
for (int i = 0; i < NTHREADS; i++) {
try {
ta[i].join();
} catch (InterruptedException ignore) { }
}