java – ExecutorService. Submit (callable) returns a null value for the future object
•
Java
I am using executorservice to generate threads that perform different tasks When submitting the (callable < T >) method, it should return future < T > Object Instead, it returns null As a result, when calling future < T > Get() method, it will fail with NullPointerException
Has anyone ever encountered this problem? Or, what did I do wrong?
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.. }
Solution
Surprisingly, you can compile code: you are trying to add some future < Boolean > to the list < Boolean >
ResultsList should be declared as:
List<Future<Boolean>> resultsList = new ArrayList<Future<Boolean>> ();
In addition, you should not use your code to receive NPE unless one of the myclassimplementingcallable in listofclassesimplementingcallable is null
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码