import java.util.*;
import java.util.concurrent.*;
class ExampleThread extends Thread {
private static CountDownLatch latch = new CountDownLatch(1);
private static Semaphore semaphore = new Semaphore(1, true);
private int type;
private String[] letters = { "A", "B", "C", "D", "E" };
ExampleThread(int type) {
super();
this.type = type;
}
public void run() {
try {
if (type == 0) {
semaphore.acquire();
latch.countDown();
} else {
latch.await();
semaphore.acquire();
}
for (int i = 0; i < 5; i++) {
if (type == 0) {
System.out.print(i);
} else {
System.out.print(letters[i]);
}
Thread.sleep(100);
}
} catch (Exception ex) {
System.out.println("Exception 2");
}
semaphore.release();
}
public static void main(String[] args) {
new ExampleThread(1).start();
new ExampleThread(0).start();
}
}
Войдите чтобы поставить Нравится
Войдите чтобы прокомментировать