简体   繁体   中英

Not calling get on list of futures

I'm using a global Executor service with some fixed thread pool size. We have bunch of related tasks that we submit for execution and wait on list of futures.

Recently, we faced a high CPU utilization issue and on debugging I found that an exception occurred while calling get() on one of the item in list of futures. Current, we iterate over the list and there is a try catch surrounding the whole loop.

try{
    List<Result> results = new ArrayList<>()
    for(Future<Result> futureResult: futureResults{
        Result result = futureResult.get();
        results.add(result);
    }
} catch(Exception e){
  throw new InternalServiceException(e);
}

//Do something with results

Wanted to know the behaviour of other threads if get is never called on some of the items in future. I tried searching but was not able to find anything.

Also, can this behaviour trigger high CPU utilization ?

http://www.journaldev.com/1650/java-futuretask-example-program

I would still check if the future isDone as in the example above.

If you need to run other operations or want to utilize the CPU better then I would put the collector in a separate thread and perhaps just poll for results every minute or so.

Could be scheduled or handled by Thread.sleep.

Executors class provides various methods to execute Callable in a thread pool. Since callable tasks run in parallel, we have to wait for the returned Object.

Callable tasks return java.util.concurrent.Future object. Using Future we can find out the status of the Callable task and get the returned Object.

It provides get() method that can wait for the Callable to finish and then return the result.

There is an overloaded version of get() method where we can specify the time to wait for the result, it's useful to avoid current thread getting blocked for longer time.

Future provides cancel() method to cancel the associated Callable task. There are isDone() and isCancelled() methods to find out the current status of associated Callable task.

Here is a simple example of Callable task that returns the name of thread executing the task after some random time. We are using Executor framework to execute 10 tasks in parallel and use Future to get the result of the submitted tasks.

public class FutureObjectTest implements Callable<String>{

    @Override
    public String call() throws Exception {
        long waitTime = (long) (Math.random()*10000);
        System.out.println(Thread.currentThread().getName() + " waiting time in MILISECONDS " + waitTime);
        Thread.sleep(waitTime);
        return Thread.currentThread().getName() + " exiting call method.";
    }

    public static void main(String [] args){
        List<Future<String>> futureObjectList = new ArrayList<Future<String>>();
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Callable<String> futureObjectTest = new FutureObjectTest();
        for(int i=0; i<10; i++){
            Future<String> futureResult = executorService.submit(futureObjectTest);
            futureObjectList.add(futureResult);
        }
        for(Future<String> futureObj : futureObjectList){
            try {
                System.out.println(futureObj.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }

        System.out.println("Starting get method of wait");
        ////////////get(Timeout) method///////
        futureObjectList.clear();
        for(int i=0; i<10; i++){
            Future<String> futureResult = executorService.submit(futureObjectTest);
            futureObjectList.add(futureResult);
        }
        executorService.shutdown();
        for(Future<String> futureObj : futureObjectList){
            try {
                System.out.println(futureObj.get(2000,TimeUnit.MILLISECONDS));
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                e.printStackTrace();
            }
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM