0

Wondering if there are customized exception happening in call method, wondering what is the best practice for the client to get the exception? Shall we catch exception when call the get method? Or before we call get method, exception will be thrown from call method (from thread pool)? Thanks.

I am referring to the sample below,

http://www.vogella.com/tutorials/JavaConcurrency/article.html

package de.vogella.concurrency.callables;

import java.util.concurrent.Callable;

public class MyCallable implements Callable<Long> {
  @Override
  public Long call() throws Exception {
    long sum = 0;
    for (long i = 0; i <= 100; i++) {
      sum += i;
    }
    return sum;
  }

} 

package de.vogella.concurrency.callables;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableFutures {
  private static final int NTHREDS = 10;

  public static void main(String[] args) {

    ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
    List<Future<Long>> list = new ArrayList<Future<Long>>();
    for (int i = 0; i < 20000; i++) {
      Callable<Long> worker = new MyCallable();
      Future<Long> submit = executor.submit(worker);
      list.add(submit);
    }
    long sum = 0;
    System.out.println(list.size());
    // now retrieve the result
    for (Future<Long> future : list) {
      try {
        sum += future.get();
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }
    }
    System.out.println(sum);
    executor.shutdown();
  }
} 
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 3
    The documentation should be pretty clear on this: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html#get%28long,%20java.util.concurrent.TimeUnit%29 Read about the ExecutionException. – Nathan Hughes Jun 01 '16 at 20:17
  • 1
    `call()` throws it, the `Future` catches it, wraps it in a `ExecutionException` and gives it to you once you call `get()`. Standard practice is to catch `ExecutionException` & then to get it's cause and do something with it, like in http://stackoverflow.com/a/2248166/995891 - cause is the original exception thrown by call. – zapl Jun 01 '16 at 20:36
  • @NathanHughes, vote up and thanks for pointing the exact part. Wondering if there is an application customized exception thrown in `call`, how to handle that specific exception type? Since from `get`, we can only get `ExecutionException`, not a specific type. Thanks. – Lin Ma Jun 01 '16 at 21:12
  • @zapl, vote up, read the SO post you mentioned and very helpful. But I think it does not answer the question for my specific situation. In my specific situation, there could be an application customized exception (extends the general Exception class), and wondering how to handle that application specific exception type (in a more general case, I want to handle differently according to different Exception type returned)? Since from `get`, we can only get `ExecutionException`, not a specific type. Thanks. – Lin Ma Jun 01 '16 at 21:15
  • 1
    You can either use `instanceof` to check the type, or re-throw it like http://ideone.com/Eu2HSG – zapl Jun 01 '16 at 23:51
  • 2
    Look at the *cause* on the exception, that's the original exception, nested inside the ExecutionException. that's what @zapl and I have been trying to tell you about. – Nathan Hughes Jun 02 '16 at 00:43
  • @NathanHughes, thanks and vote up. If you could add a reply, I will mark it as answer to benefit other people. Thanks. – Lin Ma Jul 27 '16 at 23:18

0 Answers0