boundedexecutor.java

来自「java concurrency in practice 源码. JAVA」· Java 代码 · 共 42 行

JAVA
42
字号
package net.jcip.examples;import java.util.concurrent.*;import net.jcip.annotations.*;/** * BoundedExecutor * <p/> * Using a Semaphore to throttle task submission * * @author Brian Goetz and Tim Peierls */@ThreadSafepublic class BoundedExecutor {    private final Executor exec;    private final Semaphore semaphore;    public BoundedExecutor(Executor exec, int bound) {        this.exec = exec;        this.semaphore = new Semaphore(bound);    }    public void submitTask(final Runnable command)            throws InterruptedException {        semaphore.acquire();        try {            exec.execute(new Runnable() {                public void run() {                    try {                        command.run();                    } finally {                        semaphore.release();                    }                }            });        } catch (RejectedExecutionException e) {            semaphore.release();        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?