use*_*707 5 java nullpointerexception executorservice
我正在使用ExecutorService线程来执行不同的任务.在submit(Callable<T>)制作方法时,它应该返回一个Future<T>对象.相反,它返回null.因此,当Future<T>.get()调用方法时,它将失败NullPointerException.
有人遇到过这个问题吗?或者,我做错了什么?
ArrayList<Boolean> resultsList = new ArrayList<Boolean> ();
ExecutorService excutorService = Executors.newCachedThreadPool();
for(ClassImplementingCallable myClassImplementingCallable : listOfClassesImplementingCallable) {
resultsList.add( excutorService.submit( myClassImplementingCallable ) );
}
excutorService.shutdown();
for ( Future< Boolean > result : resultList ) {
result.get(); // getting exception here..
}
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,您可以编译代码:您正在尝试将一些代码添加Future<Boolean>到List<Boolean>...
resultsList应声明为:
List<Future<Boolean>> resultsList = new ArrayList<Future<Boolean>> ();
Run Code Online (Sandbox Code Playgroud)
除此之外,您不应按原样收到带有代码的 NPE,除非其中之一myClassImplementingCallable为listOfClassesImplementingCallable空。
| 归档时间: |
|
| 查看次数: |
2821 次 |
| 最近记录: |