我有一个线程池执行程序的列表,每个线程的大小为50,我希望当某些执行程序的队列中没有任何待处理的作业,或者运行的任务少于池大小时,他们可以从另一个执行程序队列中执行任务的执行者有更多任务并执行。
因此,我创建了另一个线程,该线程在某个时间间隔内运行以下方法并轮班工作,但运行不正常。 问题
还有其他方法可以做到吗?我认为我做的方法不是很好的方法,而且效果也不好。
请提出一种解决方法。
public void shiftTaskInIdleThreadPool() {
for(ThreadPoolExecutor executor : threadPoolExecutorsList){
if(executor.getCorePoolSize() > executor.getActiveCount()){
for (ThreadPoolExecutor executor1 : threadPoolExecutorList){
while(executor1.getQueue().size() > 0 && executor.getActiveCount() < executor.getCorePoolSize()){
executor.execute(executor1.getQueue().remove());
}
}
}
}
}