java线程池:获取运行线程数并控制线程启动速度

在java里, 我们可以使用Executors.newFixedThreadPool 来创建线程池, 然后就可以不停的创建新任务,并用线程池来执行了。

在提交任务时,如果线程池已经被占满,任务会进到一个队列里等待执行。

这种机制在一些特定情况下会有些问题。今天我就遇到一种情况:创建线程比线程执行的速度要快的多,而且单个线程占用的内存又多,所以很快内存就爆了。

想了一个办法,就是在提交任务之前,先检查目前正在执行的线程数目,只有没把线程池占满的时候在去提交任务。

代码很简单:

1
2
3
4
5
6
7
8
9
          int threadCount = ((ThreadPoolExecutor)executor).getActiveCount();
// System.out.println("running : " + threadCount);
while (threadCount == POOL_SIZE) {
TimeUnit.MILLISECONDS.sleep(1);
threadCount = ((ThreadPoolExecutor)executor).getActiveCount();
// System.out.println("running : " + threadCount);
}

executor.execute