multithreading - Exception handling in call method of Java executor framework -
wondering if there customized exception happening in call
method, wondering best practice client exception? shall catch exception when call get
method? or before call get
method, exception thrown call
method (from thread pool)? thanks.
i referring 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; (long = 0; <= 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>>(); (int = 0; < 20000; i++) { callable<long> worker = new mycallable(); future<long> submit = executor.submit(worker); list.add(submit); } long sum = 0; system.out.println(list.size()); // retrieve result (future<long> future : list) { try { sum += future.get(); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } } system.out.println(sum); executor.shutdown(); } }