public classSemaphoreOneHundred {static final Semaphore sem = new Semaphore(1);static int state = 0;static int count = 0;static class ThreadA implementsRunnable {
@Overridepublic voidrun() {try{while (count <= 100) {while (state % 3 != 0) {
sem.release();
}
sem.acquire();for (int j = 0; j < 3 && count<100; j++) {
count++;
System.out.println("A " +count);
}
state++;
sem.release();
}
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}static class ThreadB implementsRunnable {
@Overridepublic voidrun() {try{while (count <= 100) {while (state % 3 != 1) {
sem.release();
}
sem.acquire();for (int j = 0; j < 3 && count<100; j