public class ABCPrint { public static void main(String[] args){ Semaphore sa = new Semaphore(1); Semaphore sb = new Semaphore(0); Semaphore sc = new Semaphore(0); int count = 10;
ExecutorService service = Executors.newFixedThreadPool(3); service.execute(new PrintTask("A", sa, sb, count)); service.execute(new PrintTask("B", sb, sc, count)); service.execute(new PrintTask("C\n", sc, sa, count)); service.shutdown(); }
static class PrintTask implements Runnable{ private String str;