Semaphore

干饭达人GoodLucy發表於2024-08-15
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();
        }
    }

}

相關文章