I am new to java.util.concurrent package. I’m trying to write some samples.
Here is my some sample code snippet:
for (MyTask task : tasks) {
Future<Boolean> result = task.getResult();
try {
if (result.isCancelled()) {
logger.info("processResults(): cancelled: "+ ((MyTask) task).getName());
} else if (result.get() == false) {
logger.info("processResults(): Failed : "+ ((MyTask) task).getName());
} else {
logger.info("processResults(): successful: "+ ((MyTask) task).getName());
tasks.remove(task);
continue;
}
} catch (Exception e) {
logger.warn("processResults(): Excepiton: " + e.getMessage());
}
}
I have one question regarding the handling the Future object from worker thread. in my for loop the handling of tasks is in sequential manner; lets say, I need to process the return results from the worker thread, if my 2nd worker thread is completed before the first one, in my code there is no way to process the 2nd worker thread first.
Can anybody suggest the best way to handle current threds.
Thanks,
Venkat Papana
If you want to perform some work as soon as the task finishes you can do that in the same thread. i.e. change the task to do everything and have it return a value after everything is done.