我在我们的代码中使用以下代码片段。
ExecutorService executor = Executors.newFixedThreadPool(4);
while(loop 50 times){
//In the extreme case I will have 50 threads and only 4 will be active and remaining are in queue
MyThread myThread = new MyThread();
executor.execute(myThread);//Each Thread process 100,000 records and put in a file
}
executor.shutdown();
while (!executor.isTerminated()) {
}
以下是我的问题:
- 第二个while循环卡住了,可能的原因是什么?
- 有没有办法在一定时间间隔后终止整个线程池?
- 我能否在一定时间间隔后终止一个线程?
请帮我解决这个问题。
Runnable
任务中(假设MyThread
是一个Runnable
且命名非常糟糕)。 - Luiggi Mendoza