我试图让线程池执行器工作,只是想知道我是否在以下代码的某个地方出错了:
public class testPool implements Runnable {
static Executor pooledExecutor = null;
private Threat thread = null;
private testPool(int minThreadPoolSize,
int initThreadPoolSize,
int maxThreadPoolSize,
int threadKeepAliveTime,
int queueCapacity) throws Exception {
pooledExecutor = new ThreadPoolExecutor(initThreadPoolSize,
maxThreadPoolSize,
(long) (1000 * threadKeepAliveTime),
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(queueCapacity));
this.thread = new Thread(this);
this.thread.setName("testThread");
try {
this.thread.setDaemon(true);
} catch (Exception e) {
// DO Something
}
}
public void run() {
while(true){
try {
// code to get a testobject
pooledExecutor.execute(testObject);
} catch (Exception e) {
//Do something
} finally {
//if shutdown parameter is true
break
}
}
}
}
基本上,我不确定这个实现是否真的会创建线程?还是我需要使用线程工厂?在我使用 pooledexecuter 之前,它有一个 createThreads() 方法,但我看不到这样的东西。
还有任何理由为什么有人想要设置最小线程池大小
任何帮助/建议将不胜感激。