我有一些代码:
public class MyTask implements Runnable {
@Override
public void run() {
// Some code
Thread.sleep();
// Some more code.
}
}
ExecutorService executor = Executors.newCachedThreadPool();
List<MyTask> tasks = getTasks();
for(MyTask t : tasks)
executor.execute(t);
executor.shutdownNow()
if(!executor.awaitTermination(30, TimeUnit.MINUTES)) {
TimeoutException toExc = new TimeoutException("MyAPp hung after the 30 minutes timeout was reached.") // TODO
log.error(toExc)
throw toExc
}
当我使用MyTask
来自 的多个实例运行它时getTasks()
,我变得非常神秘:
[pool-3-thread-3] INFO me.myapp.MyTask - java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
...etc.
这里的问题是没有根本原因:线程睡眠只是在某个时候“中断”。
所以我问:什么时候/为什么会被Thread.sleep()
中断,我该怎么做才能找到异常的根本原因?