当我使用ExecutorCompletionService提交线程时,我收到了java.lang.IllegalMonitorStateException
Exception in thread "pool-1-thread-1"
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:127)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1239
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:431)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:406)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:957)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:917)
at java.lang.Thread.run(Thread.java:662)
代码段
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(executor);
List<Future<Object>> futures = new ArrayList<Future<Object>>();
for (ThreadClass mt : threadList) {
futures.add(completionService.submit(mt));
}
while (futures.size() > 0) {
Future<Object> f = completionService.take();
futures.remove(f);
try {
Object result = f.get();
} catch (Exception e) {
//fetches threadlist here
killThread(threadList);
}
}
再次在其子线程中,创建了线程列表,并且该线程中的实际处理已关闭。
更新: 我注意到如果我评论killThread(threadList);
,则不会出现上述异常public void killThread(ArrayList<Long> threadIdList)
{
for(long threadId : threadIdList)
{
Set<Thread> setOfThread = Thread.getAllStackTraces().keySet();
for(Thread thread : setOfThread){
if(thread.getId() == threadId){
thread.stop();
}
}
}
}
我知道我应该中断使用executor.shutdownNow()的线程,但是这两个都不会终止所有线程。 因为我不想在孩子中添加中断处理,因为我不想在循环中执行子逻辑。这是一次长时间运行的活动。