Semaphore详解

主程序

package lock;

import java.util.concurrent.Semaphore;

public class TestLock {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);
        for (int j = 0; j < 10; j++) {
            new Thread(() -> {
                try {
                    semaphore.acquire(1);
                    Thread.sleep(2000);
                    System.out.println(111);
                    semaphore.release(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                }
            }).start();
        }
    }
}

流程图

Semaphore详解_第1张图片

你可能感兴趣的:(并发,并发编程,semaphore)