Fork/Join框架介绍IV-异常处理及取消任务【在数组中找指定数值,一旦找到立马停止任务的运行】

Fork/Join-异常处理及取消任务

在ForkJoinPool类中执行ForkJoinTask对象时,在任务开始执行前可以取消它。ForkJoinTask类提供了cancel()方法来达到取消任务的目的。在取消一个任务时必须要注意以下两点:

1、 ForkJoinPool类不提供任何方法来取消线程池中正在运行或者等待运行的所有任务;
2、 取消任务时,不能取消已经被执行的任务。

实例

在本节,我们将实现一个取消ForkJoinTask对象的范例。该范例将寻找数组中某个数字所处的位置。第一个任务是寻找可以被取消的剩余任务数。由于Fork/Join框架没有提供取消功能,我们将创建一个辅助类来实现取消任务的操作。

注意:下面的代码,是在有1000个整数的数组中,查找数值等于5的数组位置,一经找到,就立马停止任务,但已经在运行的任务则无测取消。所以,数组中可能有95个位置有5,但是找出来的只有30多个,例子只是说明,一经找到就会停止任务。其实我觉得这种情况只适合在众多的数据中找一个的情况。

代码如下:
Main.java

package com.packtpub.java7.concurrency.chapter5.recipe05.core;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

import com.packtpub.java7.concurrency.chapter5.recipe05.task.SearchNumberTask;
import com.packtpub.java7.concurrency.chapter5.recipe05.util.ArrayGenerator;
import com.packtpub.java7.concurrency.chapter5.recipe05.util.TaskManager;

/**
 * Main class of the program. 
 */
public class Main {

    /**
     * Main method of the example
     * @param args
     */
    public static void main(String[] args) {

        // Generate an array of 1000 integers
        ArrayGenerator generator=new ArrayGenerator();
        int array[]=generator.generateArray(1000);

        // Create a TaskManager object
        TaskManager manager=new TaskManager();

        // Create a ForkJoinPool with the default constructor
        ForkJoinPool pool=new ForkJoinPool();

        // Create a Task to process the array
        SearchNumberTask task=new SearchNumberTask(array,0,1000,5,manager);

        // Execute the task
        pool.execute(task);

        // Shutdown the pool
        pool.shutdown();


        // Wait for the finalization of the task
        try {
            pool.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Write a message to indicate the end of the program
        System.out.printf("Main: The program has finished\n");
    }

}

ArrayGenerator.java

package com.packtpub.java7.concurrency.chapter5.recipe05.util;

import java.util.Random;

/**
 * Class that generates an array of integer numbers between 0 and 10
 * with a size specified as parameter
 *
 */
public class ArrayGenerator {

    /**
     * Method that generates an array of integer numbers between 0 and 10
     * with the specified size
     * @param size The size of the array
     * @return An array of random integer numbers between 0 and 10
     */
    public int[] generateArray(int size) {
        int count = 0;
        int array[]=new int[size];
        Random random=new Random();
        for (int i=0; i<size; i++){
            array[i]=random.nextInt(10);
            if(array[i]==5){
                System.out.print(i+", ");
                count++;
            }
        }
        System.out.println("------------------");
        System.out.println("count: "+count);
        return array;
    }

}

TaskManager.java

package com.packtpub.java7.concurrency.chapter5.recipe05.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinTask;

import com.packtpub.java7.concurrency.chapter5.recipe05.task.SearchNumberTask;

/**
 * Class that stores all the tasks that have been sent to
 * a ForkJoinPool. Provides a method for the cancellation of
 * all the tasks
 *
 */
public class TaskManager {

    /**
     * List of tasks
     */
    private List<ForkJoinTask<Integer>> tasks;

    /**
     * Constructor of the class. Initializes the list of tasks
     */
    public TaskManager(){
        tasks=new ArrayList<>();
    }

    /**
     * Method to add a new Task in the list
     * @param task The new task
     */
    public void addTask(ForkJoinTask<Integer> task){
        tasks.add(task);
    }

    /**
     * Method that cancel all the tasks in the list
     * @param cancelTask 
     */
    public void cancelTasks(ForkJoinTask<Integer> cancelTask){
        for (ForkJoinTask<Integer> task  :tasks) {
            if (task!=cancelTask) {
                task.cancel(true);
                ((SearchNumberTask)task).writeCancelMessage();
            }
        }
    }
}

SearchNumberTask.java

package com.packtpub.java7.concurrency.chapter5.recipe05.task;

import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;

import com.packtpub.java7.concurrency.chapter5.recipe05.util.TaskManager;

/**
 * This task look for a number in an array of integer numbers.
 * If the part of the array it has to process has more than
 * 10 elements, it creates two subtasks and executes then asynchronously
 * with the fork method. Otherwise, look for the number in the block
 * it has to process.
 * 
 * If the task found the number, return the position where the number has
 * been found. Else, return the -1 value. If a subtask found the number,
 * the tasks suspend the other subtask and return the position where the number
 * has been found. If none of the two subtasks found the number, return the -1
 * value.
 *
 */
public class SearchNumberTask extends RecursiveTask<Integer> {

    /**
     * Serial Version UID
     */
    private static final long serialVersionUID = 1L;

    /**
     * Valued returned when the number has not been found by the task
     */
    private final static int NOT_FOUND=-1;

    /**
     * Array of numbers
     */
    private int numbers[];

    /**
     * Start and end positions of the block of numbers
     * this task has to process
     */
    private int start, end;

    /**
     * Number this task is going to look for
     */
    private int number;

    /**
     * Object that allows the cancellation of all the tasks
     */
    private TaskManager manager;

    /**
     * Constructor of the class
     * @param array Array of numbers
     * @param start Start position of the block of numbers this task has to process 
     * @param end End position of the block of numbers this task has to process
     * @param number Number this task is going to look for
     * @param manager 
     */
    public SearchNumberTask(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;
    }

    /**
     * If the block of number this task has to process has more than
     * ten elements, divide that block in two parts and create two
     * new Tasks using the launchTasks() method.
     * Else, looks for the number in the block assigned to it using
     * the lookForNumber() method
     */
    @Override
    protected Integer compute() {
        System.out.println("Task: "+start+":"+end);
        int ret;
        if (end-start>10) {
            ret=launchTasks();
        } else {
            ret=lookForNumber();
        }
        return new Integer(ret);
    }

    /**
     * Looks for the number in the block of numbers assigned to this task
     * @return The position where it found the number or -1 if it doesn't find it
     */
    private int lookForNumber() {
        for (int i=start; i<end; i++){
            if (numbers[i]==number) {
                System.out.printf("Task: 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;
    }

    /**
     * Divide the block of numbers assigned to this task in two and 
     * execute to new Task objects to process that blocks 
     * @return The position where the number has been found of -1
     * if the number haven't been found in the subtasks
     */
    private int launchTasks() {
        int mid=(start+end)/2;

        SearchNumberTask task1=new SearchNumberTask(numbers,start,mid,number,manager);
        SearchNumberTask task2=new SearchNumberTask(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);
    }

}

结果

0, 4, 14, 45, 67, 73, 77, 79, 102, 107, 115, 118, 119, 134, 165, 180, 183, 188, 192, 219, 228, 242, 253, 258, 270, 271, 273, 282, 304, 327, 328, 333, 348, 362, 394, 405, 411, 420, 424, 431, 452, 461, 477, 486, 494, 498, 505, 510, 537, 547, 550, 564, 568, 569, 573, 591, 620, 631, 634, 646, 654, 659, 723, 725, 735, 736, 739, 746, 753, 762, 768, 779, 783, 802, 810, 835, 836, 838, 844, 849, 854, 857, 867, 878, 888, 892, 912, 923, 948, 973, 974, 992, 998, ——————
count: 93
Task: 0:1000
Task: 500:1000
Task: 0:500
Task: 500:750
Task: 0:250
Task: 0:125
Task: 125:250
Task: 0:62
Task: 62:125
Task: 0:31
Task: 500:625
Task: 31:62
Task: 625:750
Task: 31:46
Task: 31:38
Task: 46:62
Task: 62:93
Task: 38:46
Task: 500:562
Task: Number 5 found in position 45
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: 93:125
Task: 750:1000
Task: Cancelled task from 500 to 750
Task: 0:15
Task: 93:109
Task: 125:187
Task: 625:687
Task: 46:54
Task: 15:31
Task: 54:62
Task: 15:23
Task: 625:656
Task: 23:31
Task: 109:125
Task: 500:531
Task: 500:515
Task: 187:250
Task: 0:7
Task: Number 5 found in position 0
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: 250:500
Task: Cancelled task from 250 to 500
Task: 515:531
Task: 7:15
Task: 93:101
Task: 62:77
Task: 187:218
Task: 125:156
Task: Number 5 found in position 14
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: 500:507
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: 625:640
Task: Cancelled task from 562 to 625
Task: 250:375
Task: 562:625
Task: 515:523
Task: 101:109
Task: 156:187
Task: Number 5 found in position 102
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: 687:750
Task: Cancelled task from 625 to 687
Task: 109:117
Task: 531:562
Task: Number 5 found in position 505
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: Cancelled task from 625 to 687
Task: Cancelled task from 687 to 750
Task: Cancelled task from 31 to 38
Task: Cancelled task from 38 to 46
Task: Cancelled task from 46 to 54
Task: Cancelled task from 54 to 62
Task: Cancelled task from 62 to 77
Task: 77:93
Task: Cancelled task from 77 to 93
Task: 625:632
Task: 507:515
Task: Number 5 found in position 115
Task: 523:531
Task: 750:875
Task: Cancelled task from 0 to 500
Task: 125:140
Task: 117:125
Task: Number 5 found in position 510
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: Cancelled task from 625 to 687
Task: Cancelled task from 687 to 750
Task: Cancelled task from 31 to 38
Task: Cancelled task from 38 to 46
Task: Cancelled task from 46 to 54
Task: Cancelled task from 54 to 62
Task: Cancelled task from 62 to 77
Task: Cancelled task from 77 to 93
Task: Cancelled task from 500 to 531
Task: Cancelled task from 531 to 562
Task: Cancelled task from 93 to 109
Task: Cancelled task from 109 to 125
Task: Cancelled task from 750 to 875
Task: 875:1000
Task: Cancelled task from 875 to 1000
Task: 140:156
Task: 156:171
Task: 632:640
Task: Number 5 found in position 118
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: Cancelled task from 625 to 687
Task: Cancelled task from 687 to 750
Task: Cancelled task from 31 to 38
Task: Cancelled task from 38 to 46
Task: Cancelled task from 46 to 54
Task: Cancelled task from 54 to 62
Task: Cancelled task from 62 to 77
Task: Cancelled task from 77 to 93
Task: Cancelled task from 500 to 531
Task: Cancelled task from 531 to 562
Task: Cancelled task from 93 to 109
Task: Cancelled task from 109 to 125
Task: Cancelled task from 750 to 875
Task: Cancelled task from 875 to 1000
Task: Cancelled task from 0 to 7
Task: Cancelled task from 7 to 15
Task: Cancelled task from 93 to 101
Task: Cancelled task from 101 to 109
Task: Cancelled task from 125 to 156
Task: Cancelled task from 156 to 187
Task: Cancelled task from 625 to 656
Task: Cancelled task from 656 to 687
Task: Cancelled task from 15 to 23
Task: Cancelled task from 23 to 31
Task: Cancelled task from 625 to 640
Task: Cancelled task from 640 to 656
Task: Cancelled task from 109 to 117
Task: Cancelled task from 500 to 515
Task: Cancelled task from 515 to 531
Task: Cancelled task from 500 to 507
Task: Cancelled task from 507 to 515
Task: Cancelled task from 187 to 218
Task: Cancelled task from 218 to 250
Task: 218:250
Task: 375:500
Task: Cancelled task from 250 to 375
Task: 62:69
Task: Number 5 found in position 634
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: Cancelled task from 625 to 687
Task: Cancelled task from 687 to 750
Task: Cancelled task from 31 to 38
Task: Cancelled task from 38 to 46
Task: Cancelled task from 46 to 54
Task: Cancelled task from 54 to 62
Task: Cancelled task from 62 to 77
Task: Cancelled task from 77 to 93
Task: Cancelled task from 500 to 531
Task: Cancelled task from 531 to 562
Task: Cancelled task from 93 to 109
Task: Cancelled task from 109 to 125
Task: Cancelled task from 750 to 875
Task: Cancelled task from 875 to 1000
Task: Cancelled task from 0 to 7
Task: Cancelled task from 7 to 15
Task: Cancelled task from 93 to 101
Task: Cancelled task from 101 to 109
Task: Cancelled task from 125 to 156
Task: Cancelled task from 156 to 187
Task: Cancelled task from 625 to 656
Task: Cancelled task from 656 to 687
Task: Cancelled task from 15 to 23
Task: Cancelled task from 23 to 31
Task: Cancelled task from 625 to 640
Task: Cancelled task from 640 to 656
Task: Cancelled task from 109 to 117
Task: Cancelled task from 117 to 125
Task: Cancelled task from 500 to 515
Task: Cancelled task from 515 to 531
Task: Cancelled task from 500 to 507
Task: Cancelled task from 507 to 515
Task: Cancelled task from 187 to 218
Task: Cancelled task from 218 to 250
Task: Cancelled task from 250 to 375
Task: Cancelled task from 375 to 500
Task: Cancelled task from 515 to 523
Task: Cancelled task from 523 to 531
Task: Cancelled task from 62 to 69
Task: Cancelled task from 69 to 77
Task: Cancelled task from 187 to 202
Task: 202:218
Task: Cancelled task from 202 to 218
Task: 531:546
Task: 875:937
Task: 562:593
Task: 140:148
Task: Number 5 found in position 631
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: Cancelled task from 625 to 687
Task: Cancelled task from 687 to 750
Task: Cancelled task from 31 to 38
Task: Cancelled task from 38 to 46
Task: Cancelled task from 46 to 54
Task: Cancelled task from 54 to 62
Task: Cancelled task from 62 to 77
Task: Cancelled task from 77 to 93
Task: Cancelled task from 500 to 531
Task: Cancelled task from 531 to 562
Task: Cancelled task from 93 to 109
Task: Cancelled task from 109 to 125
Task: Cancelled task from 750 to 875
Task: Cancelled task from 875 to 1000
Task: Cancelled task from 0 to 7
Task: Cancelled task from 7 to 15
Task: Cancelled task from 93 to 101
Task: Cancelled task from 101 to 109
Task: Cancelled task from 125 to 156
Task: Cancelled task from 156 to 187
Task: Cancelled task from 625 to 656
Task: Cancelled task from 656 to 687
Task: Cancelled task from 15 to 23
Task: Cancelled task from 23 to 31
Task: Cancelled task from 625 to 640
Task: Cancelled task from 640 to 656
Task: Cancelled task from 109 to 117
Task: Cancelled task from 117 to 125
Task: Cancelled task from 500 to 515
Task: Cancelled task from 515 to 531
Task: Cancelled task from 500 to 507
Task: Cancelled task from 507 to 515
Task: Cancelled task from 187 to 218
Task: Cancelled task from 218 to 250
Task: Cancelled task from 250 to 375
Task: Cancelled task from 375 to 500
Task: Cancelled task from 515 to 523
Task: Cancelled task from 523 to 531
Task: Cancelled task from 62 to 69
Task: Cancelled task from 69 to 77
Task: Cancelled task from 187 to 202
Task: Cancelled task from 202 to 218
Task: Cancelled task from 125 to 140
Task: Cancelled task from 140 to 156
Task: Cancelled task from 632 to 640
Task: Cancelled task from 250 to 312
Task: Cancelled task from 312 to 375
Task: Cancelled task from 562 to 593
Task: Cancelled task from 593 to 625
Task: Cancelled task from 156 to 171
Task: Cancelled task from 171 to 187
Task: Cancelled task from 687 to 718
Task: Cancelled task from 718 to 750
Task: Cancelled task from 531 to 546
Task: Cancelled task from 546 to 562
Task: Cancelled task from 77 to 85
Task: Cancelled task from 85 to 93
Task: Cancelled task from 750 to 812
Task: Cancelled task from 812 to 875
Task: Cancelled task from 125 to 132
Task: Cancelled task from 132 to 140
Task: Cancelled task from 875 to 937
Task: Cancelled task from 937 to 1000
Task: Cancelled task from 140 to 148
Task: Cancelled task from 148 to 156
Task: Cancelled task from 156 to 163
Task: Cancelled task from 163 to 171
Task: Cancelled task from 218 to 234
Task: Cancelled task from 234 to 250
Task: Cancelled task from 375 to 437
Task: Cancelled task from 437 to 500
Task: Cancelled task from 202 to 210
Task: Cancelled task from 210 to 218
Task: Cancelled task from 531 to 538
Task: Cancelled task from 538 to 546
Task: Cancelled task from 875 to 906
Task: Cancelled task from 906 to 937
Task: Cancelled task from 562 to 577
Task: Cancelled task from 577 to 593
Task: 312:375
Task: 577:593
Task: 906:937
Task: 538:546
Task: 210:218
Task: 437:500
Task: 234:250
Task: 163:171
Task: 148:156
Task: 937:1000
Task: 132:140
Task: 812:875
Task: 546:562
Task: 718:750
Task: 593:625
Task: Number 5 found in position 165
Task: Number 5 found in position 134
Task: Cancelled task from 0 to 500
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 15 to 31
Task: Cancelled task from 562 to 625
Task: Cancelled task from 500 to 562
Task: Cancelled task from 31 to 46
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: Cancelled task from 46 to 62
Task: Cancelled task from 625 to 687
Task: Cancelled task from 625 to 687
Task: Cancelled task from 687 to 750
Task: Cancelled task from 687 to 750
Task: Cancelled task from 31 to 38
Task: Cancelled task from 31 to 38
Task: Cancelled task from 38 to 46
Task: Cancelled task from 46 to 54
Task: Cancelled task from 38 to 46
Task: Cancelled task from 54 to 62
Task: Cancelled task from 46 to 54
Task: Cancelled task from 62 to 77
Task: Cancelled task from 54 to 62
Task: Cancelled task from 77 to 93
Task: Cancelled task from 62 to 77
Task: Cancelled task from 500 to 531
Task: Cancelled task from 77 to 93
Task: Cancelled task from 531 to 562
Task: Cancelled task from 500 to 531
Task: Cancelled task from 93 to 109
Task: Cancelled task from 531 to 562
Task: Cancelled task from 109 to 125
Task: Cancelled task from 93 to 109
Task: Cancelled task from 750 to 875
Task: Cancelled task from 109 to 125
Task: Cancelled task from 875 to 1000
Task: Cancelled task from 750 to 875
Task: Cancelled task from 0 to 7
Task: Cancelled task from 875 to 1000
Task: Cancelled task from 7 to 15
Task: Cancelled task from 0 to 7
Task: Cancelled task from 93 to 101
Task: Cancelled task from 101 to 109
Task: Cancelled task from 125 to 156
Task: Cancelled task from 156 to 187
Task: Cancelled task from 625 to 656
Task: Cancelled task from 656 to 687
Task: Cancelled task from 7 to 15
Task: Cancelled task from 93 to 101
Task: Cancelled task from 101 to 109
Task: Cancelled task from 15 to 23
Task: Cancelled task from 125 to 156
Task: Cancelled task from 23 to 31
Task: Cancelled task from 156 to 187
Task: Cancelled task from 625 to 640
Task: Cancelled task from 625 to 656
Task: Cancelled task from 640 to 656
Task: Cancelled task from 656 to 687
Task: Cancelled task from 109 to 117
Task: Cancelled task from 15 to 23
Task: Cancelled task from 117 to 125
Task: Cancelled task from 23 to 31
Task: Cancelled task from 500 to 515
Task: Cancelled task from 625 to 640
Task: Cancelled task from 515 to 531
Task: Cancelled task from 640 to 656
Task: Cancelled task from 500 to 507
Task: Cancelled task from 109 to 117
Task: Cancelled task from 507 to 515
Task: Cancelled task from 117 to 125
Task: Cancelled task from 187 to 218
Task: Cancelled task from 500 to 515
Task: Cancelled task from 218 to 250
Task: Cancelled task from 515 to 531
Task: Cancelled task from 250 to 375
Task: Cancelled task from 500 to 507
Task: Cancelled task from 375 to 500
Task: Cancelled task from 507 to 515
Task: Cancelled task from 187 to 218
Task: Cancelled task from 218 to 250
Task: Cancelled task from 250 to 375
Task: Cancelled task from 375 to 500
Task: Cancelled task from 515 to 523
Task: Cancelled task from 523 to 531
Task: Cancelled task from 62 to 69
Task: Cancelled task from 69 to 77
Task: Cancelled task from 187 to 202
Task: Cancelled task from 202 to 218
Task: Cancelled task from 125 to 140
Task: Cancelled task from 140 to 156
Task: Cancelled task from 625 to 632
Task: Cancelled task from 632 to 640
Task: Cancelled task from 250 to 312
Task: Cancelled task from 312 to 375
Task: Cancelled task from 562 to 593
Task: Cancelled task from 593 to 625
Task: Cancelled task from 156 to 171
Task: Cancelled task from 171 to 187
Task: Cancelled task from 687 to 718
Task: Cancelled task from 718 to 750
Task: Cancelled task from 515 to 523
Task: Cancelled task from 531 to 546
Task: Cancelled task from 523 to 531
Task: Cancelled task from 546 to 562
Task: Cancelled task from 62 to 69
Task: Cancelled task from 77 to 85
Task: Cancelled task from 69 to 77
Task: Cancelled task from 85 to 93
Task: Cancelled task from 187 to 202
Task: Cancelled task from 750 to 812
Task: Cancelled task from 202 to 218
Task: Cancelled task from 812 to 875
Task: Cancelled task from 125 to 140
Task: Cancelled task from 125 to 132
Task: Cancelled task from 140 to 156
Task: Cancelled task from 132 to 140
Task: Cancelled task from 875 to 937
Task: Cancelled task from 937 to 1000
Task: Cancelled task from 140 to 148
Task: Cancelled task from 148 to 156
Task: Cancelled task from 625 to 632
Task: Cancelled task from 632 to 640
Task: Cancelled task from 250 to 312
Task: Cancelled task from 312 to 375
Task: Cancelled task from 562 to 593
Task: Cancelled task from 593 to 625
Task: Cancelled task from 156 to 171
Task: Cancelled task from 171 to 187
Task: Cancelled task from 687 to 718
Task: Cancelled task from 718 to 750
Task: Cancelled task from 531 to 546
Task: Cancelled task from 546 to 562
Task: Cancelled task from 77 to 85
Task: Cancelled task from 85 to 93
Task: Cancelled task from 750 to 812
Task: Cancelled task from 812 to 875
Task: Cancelled task from 125 to 132
Task: Cancelled task from 156 to 163
Task: Cancelled task from 218 to 234
Task: Cancelled task from 875 to 937
Task: Cancelled task from 937 to 1000
Task: Cancelled task from 234 to 250
Task: Cancelled task from 140 to 148
Task: Cancelled task from 375 to 437
Task: Cancelled task from 437 to 500
Task: Cancelled task from 202 to 210
Task: Cancelled task from 210 to 218
Task: Cancelled task from 148 to 156
Task: Cancelled task from 531 to 538
Task: Cancelled task from 538 to 546
Task: Cancelled task from 156 to 163
Task: Cancelled task from 163 to 171
Task: Cancelled task from 218 to 234
Task: Cancelled task from 234 to 250
Task: Cancelled task from 375 to 437
Task: Cancelled task from 437 to 500
Task: Cancelled task from 875 to 906
Task: Cancelled task from 906 to 937
Task: Cancelled task from 562 to 577
Task: Cancelled task from 577 to 593
Task: Cancelled task from 312 to 343
Task: Cancelled task from 343 to 375
Task: Cancelled task from 577 to 585
Task: Cancelled task from 585 to 593
Task: Cancelled task from 202 to 210
Task: 921:937
Task: Cancelled task from 906 to 921
Task: 585:593
Task: 343:375
Task: Cancelled task from 210 to 218
Task: Number 5 found in position 67
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: Cancelled task from 625 to 687
Task: Cancelled task from 687 to 750
Task: Cancelled task from 31 to 38
Task: Cancelled task from 38 to 46
Task: Cancelled task from 46 to 54
Task: Cancelled task from 54 to 62
Task: Cancelled task from 62 to 77
Task: Cancelled task from 77 to 93
Task: Cancelled task from 500 to 531
Task: Cancelled task from 531 to 562
Task: Cancelled task from 93 to 109
Task: Cancelled task from 109 to 125
Task: Cancelled task from 750 to 875
Task: Cancelled task from 875 to 1000
Task: Cancelled task from 0 to 7
Task: Cancelled task from 7 to 15
Task: Cancelled task from 93 to 101
Task: Cancelled task from 101 to 109
Task: Cancelled task from 125 to 156
Task: Cancelled task from 156 to 187
Task: Cancelled task from 625 to 656
Task: Cancelled task from 656 to 687
Task: Cancelled task from 15 to 23
Task: Cancelled task from 23 to 31
Task: Cancelled task from 625 to 640
Task: Cancelled task from 640 to 656
Task: Cancelled task from 109 to 117
Task: Cancelled task from 117 to 125
Task: Cancelled task from 500 to 515
Task: Cancelled task from 515 to 531
Task: Cancelled task from 500 to 507
Task: Cancelled task from 507 to 515
Task: Cancelled task from 187 to 218
Task: Cancelled task from 218 to 250
Task: Cancelled task from 250 to 375
Task: Cancelled task from 375 to 500
Task: Cancelled task from 515 to 523
Task: Cancelled task from 523 to 531
Task: Cancelled task from 69 to 77
Task: Cancelled task from 187 to 202
Task: Cancelled task from 202 to 218
Task: Cancelled task from 125 to 140
Task: Cancelled task from 140 to 156
Task: Cancelled task from 625 to 632
Task: Cancelled task from 632 to 640
Task: Cancelled task from 250 to 312
Task: Cancelled task from 312 to 375
Task: Cancelled task from 562 to 593
Task: Cancelled task from 593 to 625
Task: Cancelled task from 156 to 171
Task: Cancelled task from 171 to 187
Task: Cancelled task from 687 to 718
Task: Cancelled task from 718 to 750
Task: Cancelled task from 531 to 546
Task: Cancelled task from 546 to 562
Task: Cancelled task from 77 to 85
Task: Cancelled task from 85 to 93
Task: Cancelled task from 750 to 812
Task: Cancelled task from 812 to 875
Task: Cancelled task from 125 to 132
Task: Cancelled task from 132 to 140
Task: Cancelled task from 875 to 937
Task: Cancelled task from 937 to 1000
Task: Cancelled task from 140 to 148
Task: Cancelled task from 148 to 156
Task: Cancelled task from 156 to 163
Task: Cancelled task from 163 to 171
Task: Cancelled task from 218 to 234
Task: Cancelled task from 234 to 250
Task: Cancelled task from 375 to 437
Task: Cancelled task from 437 to 500
Task: Cancelled task from 202 to 210
Task: Cancelled task from 210 to 218
Task: Cancelled task from 531 to 538
Task: Cancelled task from 538 to 546
Task: Cancelled task from 875 to 906
Task: Cancelled task from 906 to 937
Task: Cancelled task from 562 to 577
Task: Cancelled task from 577 to 593
Task: Cancelled task from 312 to 343
Task: Cancelled task from 343 to 375
Task: Cancelled task from 577 to 585
Task: Cancelled task from 585 to 593
Task: Cancelled task from 906 to 921
Task: Cancelled task from 921 to 937
Task: Cancelled task from 437 to 468
Task: Cancelled task from 468 to 500
Task: Cancelled task from 234 to 242
Task: Cancelled task from 242 to 250
Task: Cancelled task from 937 to 968
Task: 968:1000
Task: 242:250
Task: Number 5 found in position 242
Task: Cancelled task from 0 to 500
Task: Cancelled task from 968 to 1000
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: Cancelled task from 625 to 687
Task: Cancelled task from 687 to 750
Task: Cancelled task from 31 to 38
Task: Cancelled task from 38 to 46
Task: Cancelled task from 46 to 54
Task: Cancelled task from 54 to 62
Task: Cancelled task from 62 to 77
Task: Cancelled task from 77 to 93
Task: Cancelled task from 500 to 531
Task: Cancelled task from 531 to 562
Task: Cancelled task from 93 to 109
Task: Cancelled task from 109 to 125
Task: Cancelled task from 750 to 875
Task: Cancelled task from 875 to 1000
Task: Cancelled task from 0 to 7
Task: Cancelled task from 7 to 15
Task: Cancelled task from 93 to 101
Task: Cancelled task from 101 to 109
Task: Cancelled task from 125 to 156
Task: Cancelled task from 156 to 187
Task: Cancelled task from 625 to 656
Task: Cancelled task from 656 to 687
Task: Cancelled task from 15 to 23
Task: Cancelled task from 23 to 31
Task: Cancelled task from 625 to 640
Task: Cancelled task from 640 to 656
Task: Cancelled task from 109 to 117
Task: Cancelled task from 117 to 125
Task: Cancelled task from 500 to 515
Task: Cancelled task from 515 to 531
Task: Cancelled task from 500 to 507
Task: Cancelled task from 507 to 515
Task: Cancelled task from 187 to 218
Task: Cancelled task from 218 to 250
Task: Cancelled task from 250 to 375
Task: Cancelled task from 375 to 500
Task: Cancelled task from 515 to 523
Task: Cancelled task from 523 to 531
Task: Cancelled task from 62 to 69
Task: Cancelled task from 69 to 77
Task: Cancelled task from 187 to 202
Task: Cancelled task from 202 to 218
Task: Cancelled task from 125 to 140
Task: Cancelled task from 140 to 156
Task: Cancelled task from 625 to 632
Task: Cancelled task from 632 to 640
Task: Cancelled task from 250 to 312
Task: Cancelled task from 312 to 375
Task: Cancelled task from 562 to 593
Task: Cancelled task from 593 to 625
Task: Cancelled task from 156 to 171
Task: Cancelled task from 171 to 187
Task: Cancelled task from 687 to 718
Task: Cancelled task from 718 to 750
Task: Cancelled task from 531 to 546
Task: Cancelled task from 546 to 562
Task: Cancelled task from 77 to 85
Task: Cancelled task from 85 to 93
Task: Cancelled task from 750 to 812
Task: Cancelled task from 812 to 875
Task: Cancelled task from 125 to 132
Task: Cancelled task from 132 to 140
Task: Cancelled task from 875 to 937
Task: Cancelled task from 937 to 1000
Task: Cancelled task from 140 to 148
Task: Cancelled task from 148 to 156
Task: Cancelled task from 156 to 163
Task: Cancelled task from 163 to 171
Task: Cancelled task from 218 to 234
Task: Cancelled task from 234 to 250
Task: Cancelled task from 375 to 437
Task: Cancelled task from 437 to 500
Task: Cancelled task from 202 to 210
Task: Cancelled task from 210 to 218
Task: Cancelled task from 531 to 538
Task: Cancelled task from 538 to 546
Task: Cancelled task from 875 to 906
Task: Cancelled task from 906 to 937
Task: Cancelled task from 562 to 577
Task: Cancelled task from 577 to 593
Task: Cancelled task from 312 to 343
Task: Cancelled task from 343 to 375
Task: Cancelled task from 577 to 585
Task: Cancelled task from 585 to 593
Task: Cancelled task from 906 to 921
Task: Cancelled task from 921 to 937
Task: Cancelled task from 437 to 468
Task: Cancelled task from 468 to 500
Task: Cancelled task from 234 to 242
Task: Cancelled task from 937 to 968
Task: Cancelled task from 968 to 1000
Task: Cancelled task from 812 to 843
Task: Cancelled task from 843 to 875
Task: Cancelled task from 546 to 554
Task: Cancelled task from 554 to 562
Task: Cancelled task from 718 to 734
Task: Cancelled task from 734 to 750
Task: Cancelled task from 593 to 609
Task: Cancelled task from 609 to 625
Task: Cancelled task from 921 to 929
Task: Cancelled task from 929 to 937
Task: Cancelled task from 343 to 359
Task: Cancelled task from 359 to 375
Task: Cancelled task from 968 to 984
Task: Cancelled task from 984 to 1000
Task: Number 5 found in position 591
Task: Cancelled task from 0 to 500
Task: Cancelled task from 500 to 1000
Task: Cancelled task from 500 to 750
Task: Cancelled task from 750 to 1000
Task: Cancelled task from 0 to 250
Task: Cancelled task from 250 to 500
Task: Cancelled task from 500 to 625
Task: Cancelled task from 625 to 750
Task: Cancelled task from 0 to 125
Task: Cancelled task from 125 to 250
Task: Cancelled task from 0 to 62
Task: Cancelled task from 62 to 125
Task: Cancelled task from 125 to 187
Task: Cancelled task from 187 to 250
Task: Cancelled task from 0 to 31
Task: Cancelled task from 31 to 62
Task: Cancelled task from 62 to 93
Task: Cancelled task from 93 to 125
Task: Cancelled task from 0 to 15
Task: Cancelled task from 15 to 31
Task: Cancelled task from 500 to 562
Task: Cancelled task from 562 to 625
Task: Cancelled task from 31 to 46
Task: Cancelled task from 46 to 62
Task: Cancelled task from 625 to 687
Task: Cancelled task from 687 to 750
Task: Cancelled task from 31 to 38
Task: Cancelled task from 38 to 46
Task: Cancelled task from 46 to 54
Task: Cancelled task from 54 to 62
Task: Cancelled task from 62 to 77
Task: Cancelled task from 77 to 93
Task: Cancelled task from 500 to 531
Task: Cancelled task from 531 to 562
Task: Cancelled task from 93 to 109
Task: Cancelled task from 109 to 125
Task: Cancelled task from 750 to 875
Task: Cancelled task from 875 to 1000
Task: Cancelled task from 0 to 7
Task: Cancelled task from 7 to 15
Task: Cancelled task from 93 to 101
Task: Cancelled task from 101 to 109
Task: Cancelled task from 125 to 156
Task: Cancelled task from 156 to 187
Task: Cancelled task from 625 to 656
Task: Cancelled task from 656 to 687
Task: Cancelled task from 15 to 23
Task: Cancelled task from 23 to 31
Task: Cancelled task from 625 to 640
Task: Cancelled task from 640 to 656
Task: Cancelled task from 109 to 117
Task: Cancelled task from 117 to 125
Task: Cancelled task from 500 to 515
Task: Cancelled task from 515 to 531
Task: Cancelled task from 500 to 507
Task: Cancelled task from 507 to 515
Task: Cancelled task from 187 to 218
Task: Cancelled task from 218 to 250
Task: Cancelled task from 250 to 375
Task: Cancelled task from 375 to 500
Task: Cancelled task from 515 to 523
Task: Cancelled task from 523 to 531
Task: Cancelled task from 62 to 69
Task: Cancelled task from 69 to 77
Task: Cancelled task from 187 to 202
Task: Cancelled task from 202 to 218
Task: Cancelled task from 125 to 140
Task: Cancelled task from 140 to 156
Task: Cancelled task from 625 to 632
Task: Cancelled task from 632 to 640
Task: Cancelled task from 250 to 312
Task: Cancelled task from 312 to 375
Task: Cancelled task from 562 to 593
Task: Cancelled task from 593 to 625
Task: Cancelled task from 156 to 171
Task: Cancelled task from 171 to 187
Task: Cancelled task from 687 to 718
Task: Cancelled task from 718 to 750
Task: Cancelled task from 531 to 546
Task: Cancelled task from 546 to 562
Task: Cancelled task from 77 to 85
Task: Cancelled task from 85 to 93
Task: Cancelled task from 750 to 812
Task: Cancelled task from 812 to 875
Task: Cancelled task from 125 to 132
Task: Cancelled task from 132 to 140
Task: Cancelled task from 875 to 937
Task: Cancelled task from 937 to 1000
Task: Cancelled task from 140 to 148
Task: Cancelled task from 148 to 156
Task: Cancelled task from 156 to 163
Task: Cancelled task from 163 to 171
Task: Cancelled task from 218 to 234
Task: Cancelled task from 234 to 250
Task: Cancelled task from 375 to 437
Task: Cancelled task from 437 to 500
Task: Cancelled task from 202 to 210
Task: Cancelled task from 210 to 218
Task: Cancelled task from 531 to 538
Task: Cancelled task from 538 to 546
Task: Cancelled task from 875 to 906
Task: Cancelled task from 906 to 937
Task: Cancelled task from 562 to 577
Task: Cancelled task from 577 to 593
Task: Cancelled task from 312 to 343
Task: Cancelled task from 343 to 375
Task: Cancelled task from 577 to 585
Task: Cancelled task from 906 to 921
Task: Cancelled task from 921 to 937
Task: Cancelled task from 437 to 468
Task: Cancelled task from 468 to 500
Task: Cancelled task from 234 to 242
Task: Cancelled task from 242 to 250
Task: Cancelled task from 937 to 968
Task: Cancelled task from 968 to 1000
Task: Cancelled task from 812 to 843
Task: Cancelled task from 843 to 875
Task: Cancelled task from 546 to 554
Task: Cancelled task from 554 to 562
Task: Cancelled task from 718 to 734
Task: Cancelled task from 734 to 750
Task: Cancelled task from 593 to 609
Task: Cancelled task from 609 to 625
Task: Cancelled task from 921 to 929
Task: Cancelled task from 929 to 937
Task: Cancelled task from 343 to 359
Task: Cancelled task from 359 to 375
Task: Cancelled task from 968 to 984
Task: Cancelled task from 984 to 1000
Main: The program has finished

工作原理

工作原理

ForkJoinTask类提供的cancel()方法允许取消一个仍没有被执行的任务,这是非常重要的一点。如果任务已经开始执行,那么调用cancel()方法也无法取消。这个方法接收一个名为mayInterruptIfRunning的boolean值参数。顾名思义,如果传递true值给这个方法,即使任务正在运行也将被取消。JavaAPI文档指出,ForkJoinTask类的默认实现,这个属性没有起作用。如果任务还没有开始执行,那么这些任务将被取消。任务的取消对于已发送到线程池中的任务没有影响,它们将继续执行。

Fork/Join框架的局限性在于,ForkJoinPool线程池中的任务不允许被取消。为了克服这种局限性,我们实现了TaskManager类,它存储发送到线程池中的所有任务,可以用一个方法来取消存储的所有任务。如果任务正在运行或者已经执行结束,那么任务就不能被取消,cancel()方法返回false值,因此可以尝试去取消所有的任务而不用担心可能带来的间接影响。

这个范例实现在数字数组中寻找一个数字。根据Fork/Join框架的推荐,我们将问题拆分为更小的子问题。由于我们仅关心数字的一次出现,因此,当找到它时,就会取消其他的所有任务。

转自ifeve:http://ifeve.com/java7-concurrency-cookbook-4/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值