使用示例

public class Test {
    public static void main(String[] args) throws Exception{
        Semaphore semaphore = new Semaphore(3); //信号量
        for(int i=1; i<=6; i++) {
           new Thread(() -> {
               try {
                   semaphore.acquire(); //获取信号量位置
                   System.out.println(Thread.currentThread().getName()+":进入车库!");
                   TimeUnit.SECONDS.sleep(1);
               } catch (Exception e) {
                   e.printStackTrace();
               } finally {
                   semaphore.release(); //释放
                   System.out.println(Thread.currentThread().getName()+":退出车库!");
               }
           }, "Thread-"+i).start();
        }
    }
}