下面这段代码试图帮助这个.
代码永远循环并检查是否有任何待处理的请求要处理.如果有,则创建一个新线程来处理请求并将其提交给执行程序.完成所有线程后,它会休眠60秒并再次检查待处理的请求.
public static void main(String a[]){
//variables init code omitted
ExecutorService service = Executors.newFixedThreadPool(15);
ExecutorCompletionService<Long> comp = new ExecutorCompletionService<Long>(service);
while(true){
List<AppRequest> pending = service.findPendingRequests();
int noPending = pending.size();
if (noPending > 0) {
for (AppRequest req : pending) {
Callable<Long> worker = new RequestThread(something, req);
comp.submit(worker);
}
}
for (int i = 0; i < noPending; i++) {
try {
Future<Long> f = comp.take();
long name;
try {
name = f.get();
LOGGER.debug(name + " got completed");
} catch (ExecutionException …Run Code Online (Sandbox Code Playgroud)