1 Star 0 Fork 0

雨翔河/CommonTool

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CountableThreadPool.java 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
雨翔河 提交于 2018-01-25 17:31 +08:00 . add CountableThreadPool
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* 弥补JDK线程池
* 限制 activeThreadNum 个活跃线程运行,新来的进行阻塞
* Created by hxy on 2018/1/25.
*/
public class CountableThreadPool {
private Semaphore semaphore;
private ExecutorService executorService;
public CountableThreadPool(int activeThreadNum, ExecutorService executorService) {
this.executorService = executorService;
this.semaphore = new Semaphore(activeThreadNum);
}
public CountableThreadPool(int activeThreadNum) {
executorService = Executors.newFixedThreadPool(activeThreadNum);
this.semaphore = new Semaphore(activeThreadNum);
}
public void run(Runnable runnable) {
try {
semaphore.acquire();
executorService.execute(() -> {
try {
runnable.run();
} finally {
semaphore.release();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Count/CommonTool.git
git@gitee.com:Count/CommonTool.git
Count
CommonTool
CommonTool
master

搜索帮助