Semaphore实现多线程交叉顺序执行

public class SemaphoreExample {
    private static Semaphore s1 = new Semaphore(1);
    private static Semaphore s2 = new Semaphore(1);
    private static Semaphore s3 = new Semaphore(1);
    static Semaphore[] signals = {s1, s2, s3};
    public static void foo(int count) {
        while (true) {
            try {
                signals[count - 1].acquire();
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程名:" + count);
            signals[(count) % 3].release();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> foo(1));
        Thread t2 = new Thread(() -> foo(2));
        Thread t3 = new Thread(() -> foo(3));
        s1.acquire();
        s2.acquire();
        t1.start();
        t2.start();
        t3.start();
    }
}