Java ForkJoinPool join results from tasks

Description

Java ForkJoinPool join results from tasks


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;

class BiggerTask extends RecursiveTask<Integer> {

  private String document[][];

  private int start, end;


  public BiggerTask(String document[][], int start, int end) {
    this.document = document;
    this.start = start;
    this.end = end;
  }/*from   www .j  a v  a 2 s  .  c  om*/

  @Override
  protected Integer compute() {
    Integer result = null;
    if (end - start < 10) {
      result = processLines(document, start, end);
    } else {
      int mid = (start + end) / 2;
      BiggerTask task1 = new BiggerTask(document, start, mid);
      BiggerTask task2 = new BiggerTask(document, mid, end);
      invokeAll(task1, task2);
      try {
        result = groupResults(task1.get(), task2.get());
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
    return result;
  }

  private Integer processLines(String[][] document, int start, int end) {
    List<ChildTask> tasks = new ArrayList<ChildTask>();

    for (int i = start; i < end; i++) {
      ChildTask task = new ChildTask(document[i], 0, document[i].length);
      tasks.add(task);
    }
    invokeAll(tasks);

    int result = 0;
    for (int i = 0; i < tasks.size(); i++) {
      ChildTask task = tasks.get(i);
      try {
        result = result + task.get();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return result;
  }

  private Integer groupResults(Integer number1, Integer number2) {
    Integer result;

    result = number1 + number2;
    return result;
  }

}

class ChildTask extends RecursiveTask<Integer> {

  private String line[];

  private int start, end;


  public ChildTask(String line[], int start, int end) {
    this.line = line;
    this.start = start;
    this.end = end;
  }

  @Override
  protected Integer compute() {
    Integer result = null;
    if (end - start < 100) {
      result = count(line, start, end);
    } else {
      int mid = (start + end) / 2;
      ChildTask task1 = new ChildTask(line, start, mid);
      ChildTask task2 = new ChildTask(line, mid, end);
      invokeAll(task1, task2);
      try {
        result = groupResults(task1.get(), task2.get());
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
    return result;
  }

  private Integer groupResults(Integer number1, Integer number2) {
    Integer result;

    result = number1 + number2;
    return result;
  }

  private Integer count(String[] line, int start, int end) {
    int counter;
    counter = 0;
    for (int i = start; i < end; i++) {
      if (line[i].length() >3) {
        counter++;
      }
    }
    try {
      TimeUnit.MILLISECONDS.sleep(10);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return counter;
  }

}

public class Main {
  public static String[][] generateDocument() {
    String document[][] = new String[100][1000];
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 1000; j++) {
        document[i][j] =""+(i+j);
      }
    }
    return document;
  }

  public static void main(String[] args) {
    String[][] document = generateDocument();

    BiggerTask task = new BiggerTask(document, 0, 100);

    ForkJoinPool pool = new ForkJoinPool();

    pool.execute(task);

    do {
      System.out.printf("Parallelism: %d\n", pool.getParallelism());
      System.out.printf("Active Threads: %d\n", pool.getActiveThreadCount());
      System.out.printf("Task Count: %d\n", pool.getQueuedTaskCount());
      System.out.printf("Steal Count: %d\n", pool.getStealCount());
      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

    } while (!task.isDone());

    pool.shutdown();

    try {
      pool.awaitTermination(1, TimeUnit.DAYS);

      System.out.printf("Main: The word appears %d in the document", task.get());
    } catch (InterruptedException | ExecutionException e) {
      e.printStackTrace();
    }
  }

}



PreviousNext

Related