Semaphore

import java.util.concurrent.Semaphore;

public class T {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);
        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    Thread.sleep(2000);
                    System.out.println(Thread.currentThread().getName() + " ......");
                } catch (Exception e) {

                } finally {
                    semaphore.release();
                }
            }, "线程" + i).start();
        }
    }

}
posted @ 2024-08-15 17:10  干饭达人GoodLucy  阅读(9)  评论(0编辑  收藏  举报