Java ForkJoinPool cancel a task

Description

Java ForkJoinPool cancel a task


import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;

class SearchTask extends RecursiveTask<Integer> {

  private final static int NOT_FOUND = -1;

  private int numbers[];

  private int start, end;

  private int number;

  private TaskManager manager;

  public SearchTask(int numbers[], int start, int end, int number, TaskManager manager) {
    this.numbers = numbers;
    this.start = start;
    this.end = end;
    this.number = number;
    this.manager = manager;
  }/*ww w. j av  a2  s .  com*/

  @Override
  protected Integer compute() {
    System.out.println("Task: " + start + ":" + end);
    int ret;
    if (end - start > 10) {
      ret = launchTasks();
    } else {
      ret = lookForNumber();
    }
    return ret;
  }

  private int lookForNumber() {
    for (int i = start; i < end; i++) {
      if (numbers[i] == number) {
        System.out.printf("Number %d found in position %d\n", number, i);
        manager.cancelTasks(this);
        return i;
      }
      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    return NOT_FOUND;
  }

  private int launchTasks() {
    int mid = (start + end) / 2;

    SearchTask task1 = new SearchTask(numbers, start, mid, number, manager);
    SearchTask task2 = new SearchTask(numbers, mid, end, number, manager);

    manager.addTask(task1);
    manager.addTask(task2);

    task1.fork();
    task2.fork();
    int returnValue;

    returnValue = task1.join();
    if (returnValue != -1) {
      return returnValue;
    }

    returnValue = task2.join();
    return returnValue;
  }

  public void writeCancelMessage() {
    System.out.printf("Task: Cancelled task from %d to %d\n", start, end);
  }

}

class TaskManager {
  private List<ForkJoinTask<Integer>>   tasks = new ArrayList<>();

  public void addTask(ForkJoinTask<Integer> task) {
    tasks.add(task);
  }

  public void cancelTasks(ForkJoinTask<Integer> cancelTask) {
    for (ForkJoinTask<Integer> task : tasks) {
      if (task != cancelTask) {
        task.cancel(true);
        ((SearchTask) task).writeCancelMessage();
      }
    }
  }
}

public class Main {
  static int[] generateArray(int size) {
    int array[] = new int[size];
    Random random = new Random();
    for (int i = 0; i < size; i++) {
      array[i] = random.nextInt(10);
    }
    return array;
  }
  public static void main(String[] args) {
    int array[] = generateArray(1000);
    TaskManager manager = new TaskManager();
    ForkJoinPool pool = new ForkJoinPool();
    SearchTask task = new SearchTask(array, 0, 1000, 5, manager);
    pool.execute(task);
    pool.shutdown();
    try {
      pool.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.printf("The program has finished\n");
  }
}



PreviousNext

Related