03-Semaphore
使用示例
public class Demo_07_03_1_Semaphore {
private static Semaphore semaphore = new Semaphore(3);
public static void main(String[] args) {
for (int i = 0; i < 30; i++) {
new Thread(() -> {
try {
semaphore.acquire(); // 相当于获取锁
System.out.println(Thread.currentThread().getName() + "执行了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release(); // 释放锁
}
}).start();
}
}
}其他API
最后更新于