Java ForkJoinPool throw exceptions from tasks
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; import java.util.concurrent.TimeUnit; class Task extends RecursiveTask<Integer> { private int array[]; private int start, end; public Task(int array[], int start, int end) { this.array = array; this.start = start; this.end = end; }/*from w w w. jav a2 s . co m*/ @Override protected Integer compute() { System.out.println("Start"+" "+ start+" "+ end); if (end - start < 10) { if ( start %3 == 0) { throw new RuntimeException("This task throws an Exception: Task from " + start + " to " + end); } try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } else { int mid = (end + start) / 2; Task task1 = new Task(array, start, mid); Task task2 = new Task(array, mid, end); invokeAll(task1, task2); System.out.println(start+" "+ mid+" "+ task1.join()); System.out.println(mid+" "+ end+" "+ task2.join()); } System.out.println("Task: End "+ start+" "+ end); return 0; } } public class Main { public static void main(String[] args) { int array[] = new int[100]; Task task = new Task(array, 0, 100); ForkJoinPool pool = new ForkJoinPool(); pool.execute(task); pool.shutdown(); try { pool.awaitTermination(1, TimeUnit.DAYS); } catch (InterruptedException e) { e.printStackTrace(); } if (task.isCompletedAbnormally()) { System.out.println("Main: An exception has ocurred\n"); System.out.println("Main: "+ task.getException()); } System.out.println("Main: Result: "+ task.join()); } }