How to do it...

Follow these steps to implement the example:

  1. Create a class named TaskFJ and specify that it extends the RecursiveAction class:
        public class TaskFJ extends RecursiveAction {
  1. Declare a private array of int numbers named array:
        private final int array[];
  1. Declare two private int attributes, named start and end:
        private final int start, end;
  1. Implement the constructor of the class to initialize its attributes:
        public TaskFJ(int array[], int start, int end) { 
this.array=array;
this.start=start;
this.end=end;
}
  1. Implement the compute() method. If this task has to process a block of more than 1,000 elements (determined by the start and end attributes), create two TaskFJ objects, send them to the ForkJoinPool class using the fork() method, and wait for their finalization using the join() method:
        @Override 
protected void compute() {
if (end-start>1000) {
int mid=(start+end)/2;
TaskFJ task1=new TaskFJ(array,start,mid);
TaskFJ task2=new TaskFJ(array,mid,end);
task1.fork();
task2.fork();
task1.join();
task2.join();
  1. Otherwise, increment the elements that this task has to process. After every increment operation, put the thread to sleep for 1 millisecond:
        } else { 
for (int i=start; i<end; i++) {
array[i]++;
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
  1. Create a class named Task and specify that it implements the Runnable interface:
        public class Task implements Runnable {
  1. Declare a private array of int number named array:
        private final int array[];
  1. Implement the constructor of the class to initialize its attribute:
        public Task(int array[]) { 
this.array=array;
}
  1. Implement the run() method. Increment all the elements of the array. After every increment operation, put the thread to sleep for 1 millisecond:
        @Override 
public void run() {
for (int i=0; i<array.length; i++ ){
array[i]++;
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
  1. Implement the main class of the example by creating a class named Main and adding the main() method to it:
        public class Main { 

public static void main(String[] args) {
  1. Create an int array with 100,000 elements:
        int array[]=new int[100000];
  1. Create a Task object and a ThreadPoolExecutor object and execute them. Execute the task by controlling the time during which the task is running:
        Task task=new Task(array); 
ExecutorService executor=Executors.newCachedThreadPool();

Date start,end;
start=new Date();
executor.execute(task);
executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
end=new Date();
System.out.printf("Main: Executor: %d ",
(end.getTime()-start.getTime()));
  1. Create a TaskFJ object and a ForkJoinPool object and execute them. Execute the task by controlling the time during which the task is running:
          TaskFJ taskFJ=new TaskFJ(array,1,100000); 
ForkJoinPool pool=new ForkJoinPool();
start=new Date();
pool.execute(taskFJ);
pool.shutdown();
try {
pool.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
end=new Date();
System.out.printf("Core: Fork/Join: %d ",
(end.getTime()-start.getTime()));
}
..................Content has been hidden....................

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