package com.itheima.mysemaphore; public class MySemaphoreDemo { public static void main(String[] args) { MyRunnable mr=new MyRunnable(); for (int i = 0; i < 100; i++) { new Thread(mr).start(); } } } package com.itheima.mysemaphore; import java.util.concurrent.Semaphore; public class MyRunnable implements Runnable{ private Semaphore semaphore=new Semaphore(2); @Override public void run() { try { semaphore.acquire(); System.out.println("获得了同行证"); Thread.sleep(2000); System.out.println("归还通行证"); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } } }