How to do it...

Follow these steps to implement the example:

  1. Create a class named Task. Specify that it implements the RecursiveTask class, parameterized with the Integer class:
        public class Task extends RecursiveTask<Integer> { 
  1. Declare a private int array named array. It will simulate the array of data you are going to process in this example:
        private int array[]; 
  1. Declare two private int attributes named start and end. These attributes will determine the elements of the array this task has to process:
        private int start, end; 
  1. Implement the constructor of the class that initializes its attributes:
        public Task(int array[], int start, int end){ 
this.array=array;
this.start=start;
this.end=end;
}
  1. Implement the compute() method of the task. As you have parameterized the RecursiveTask class with the Integer class, this method has to return an Integer object. First, write a message to the console with the value of the start and end attributes:
        @Override 
protected Integer compute() {
System.out.printf("Task: Start from %d to %d ",start,end);
  1. If the block of elements that this task has to process, determined by the start and end attributes, has a size smaller than 10, check if the element in the fourth position in the array (index number three) is in that block. If that is the case, throw RuntimeException. Then, put the task to sleep for a second:
        if (end-start<10) { 
if ((3>start)&&(3<end)){
throw new RuntimeException("This task throws an"+
"Exception: Task from "+start+" to "+end);
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
  1. Otherwise (the block of elements that this task has to process has a size of 10 or bigger), divide the block of elements in two, create two Task objects to process those blocks, and execute them in the pool using the invokeAll() method. Then, we write the results of these tasks to the console:
        } 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.printf("Task: Result form %d to %d: %d ",
start,mid,task1.join());
System.out.printf("Task: Result form %d to %d: %d ",
mid,end,task2.join());
}
  1. Write a message to the console indicating the end of the task, writing the value of the start and end attributes:
        System.out.printf("Task: End form %d to %d
",start,end); 
  1. Return the number 0 as the result of the task:
        return 0; 
  1. Implement the main class of the example by creating a class named Main with a main() method:
        public class Main { 
public static void main(String[] args) {
  1. Create an array of 100 integer numbers:
        int array[]=new int[100]; 
  1. Create a Task object to process that array:
        Task task=new Task(array,0,100); 
  1. Create a ForkJoinPool object using the default constructor:
        ForkJoinPool pool=new ForkJoinPool(); 
  1. Execute the task in the pool using the execute() method:
        pool.execute(task); 
  1. Shut down the ForkJoinPool class using the shutdown() method:
        pool.shutdown(); 
  1. Wait for the finalization of the task using the awaitTermination() method. As you want to wait for the finalization of the task however long it takes to complete, pass the values 1 and TimeUnit.DAYS as parameters to this method:
        try { 
pool.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
  1. Check if the task, or one of its subtasks, has thrown an exception using the isCompletedAbnormally() method. In such a case, write a message to the console with the exception that was thrown. Get that exception with the getException() method of the ForkJoinTask class:
        if (task.isCompletedAbnormally()) { 
System.out.printf("Main: An exception has ocurred ");
System.out.printf("Main: %s ",task.getException());
}
System.out.printf("Main: Result: %d",task.join());
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.205.205