I am creating a threadpool and since I don’t need any output from my threads, I am not using Futures&Callables. But the problem is that if I don’t use Futures, I am seeing that threadpool is loosing tasks that I am submitting.
Here is my code structure which is loosing tasks-
while(hasWorkToDo){
ThreadPoolExecutor.execute(new Worker());
System.out.println("Work submitted");
}
Within Worker(), I am printing “got work” at the very first line of run() implementation.
When I run above code the number of “got work” is always less than “Work submitted”.
But, when I change my code structure to below-
ArrayList<Future<String>> list = new ArrayList<Future<String>>();
while(hasWorkToDo){
Future<String> submit=ThreadPoolExecutor.submit(new Worker());
list.add(submit)
}
for (Future<String> f : list) {
f.get();
}
I then see that number of times “got work” is equal to “Work submitted”
This leads me to believe that unless I am using Futures&Callables, the threadpool is using tasks that I am submitting.
Have you seen such behavior before? Is there a better way to debug this?
You con’t need to keep the Future tasks, less you need to wait for them to complete before shutting down the executor. I suspect you are using shutdownNow() somewhere. Try using shutdown() instead.