Thread Semaphore

1 信号量

2 实现代码。 

public class SemapDemo implements Runnable{

    public final Semaphore semaphore = new Semaphore(5);//once 5 thread

    @Override
    public void run() {
        try {
            semaphore.acquire();
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName());
            semaphore.release();
        }catch (InterruptedException e){
        }
    }
    public static void main(String[] args){
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        final SemapDemo semapDemo = new SemapDemo();
        for (int i = 0; i < 20; i++){
            executorService.submit(semapDemo);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_28197211/article/details/80931524