码迷,mamicode.com
首页 > 其他好文 > 详细

Fork/Join框架

时间:2015-10-26 11:46:45      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

  Fork/Join框架的核心是由下列两个类组成的。

  工作窃取算法

  工作窃取(work-stealing)算法是指某个线程从其他队列里窃取任务来执行。

  那么为什么需要使用工作窃取算法呢?假如我们需要做一个比较大的任务,我们可以把这个任务分割为若干互不依赖的子任务,为了减少线程间的竞争,于是把这些子任务分别放到不同的队列里,并为每个队列创建一个单独的线程来执行队列里的任务,线程和队列一一对应,比如A线程负责处理A队列里的任务。但是有的线程会先把自己队列里的任务干完,而其他线程对应的队列里还有任务等待处理。干完活的线程与其等着,不如去帮其他线程干活,于是它就去其他线程的队列里窃取一个任务来执行。而在这时它们会访问同一个队列,所以为了减少窃取任务线程和被窃取任务线程之间的竞争,通常会使用双端队列,被窃取任务线程永远从双端队列的头部拿任务执行,而窃取任务的线程永远从双端队列的尾部拿任务执行。

  • ForkJoinPool:这个类实现了ExecutorService接口和工作窃取算法(Work-Stealing Algorithm)。它管理工作者线程,并提供任务的状态信息,以及任务的执行信息。
  • ForkJoinTask:这个类是一个将在ForkJoinPool中执行的任务的基类。

  Fork/Join框架提供了在一个任务里执行fork()和join()操作的机制和控制任务状态的方法。通常,为了实现Fork/Join任务,需要实现一个以下两个类之一的子类。

  • RecursiveAction:用于任务没有返回结果的场景。
  • RecursiveTask:用于任务有返回结果的场景。

1. 创建Fork/join线程池

  下面我们将学习如何使用Fork/Join框架的基本元素。它包括:

  • 创建用来执行任务的ForkJoinPool对象;
  • 创建即将在线程池中被执行的任务ForkJoinTask子类。

  本范例中即将使用的Fork/Join框架的主要特性如下:

  • 采用默认的构造器创建ForkJoinPool对象;
  • 在任务中将使用JavaAPI文档推荐的结构。
if (problem size > default size) {
    tasks = divide(task);
    execute(tasks);
} else {
    resolve problem using another algorithm;
}
  • 我们将以同步的方式执行任务。当一个主任务执行两个或者更多的子任务时,这个主任务将等待子任务的完成。用这种方法,执行主任务的线程,称之为工作者线程(Worker Thread),它将寻找其它的子任务来执行,并在子任务执行的时间里利用所有的线程优势。
  • 如果将要实现的任务没有返回任何结果,那么,采用RecursiveAction类作为实现任务的基类。

  在本节,我们将实现一项更新产品价格的任务。最初的任务将负责更新列表中的所有元素。我们使用10来作为参考大小(ReferenceSize),如果一个任务需要更新大于10个元素,它会将这个列表分解成为两部分,然后分别创建两个任务用来更新各自部分的产品价格。

1. 创建一个名为Product的类,用来存储产品的名称和价格。

public class Product {
    
    private String name;
    private double price;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
        
}

2. 创建一个名为ProductListGenerator的类,用来生成一个随机的产品列表。

import java.util.ArrayList;
import java.util.List;

public class ProductListGenerator {
    
    public List<Product> generate(int size){
        List<Product> ret = new ArrayList<Product>();
        for(int i=0;i<size;i++){
            Product product = new Product();
            product.setName("Product " + i);
            product.setPrice(10);
            ret.add(product);
        }
        return ret;
    }
}

3. 创建一个名为Task的类,并继承RecursiveAction类。

import java.util.List;
import java.util.concurrent.RecursiveAction;


public class Task extends RecursiveAction {

    private static final long serialVersionUID = 1L;
    private List<Product> products;
    private int first;
    private int last;
    private double increasement;
    
    public Task(List<Product> products, int first, int last, double increasement){
        this.products = products;
        this.first = first;
        this.last = last;
        this.increasement = increasement;
    }
    
    @Override
    protected void compute() {
        if(last-first<10){
            updatePrices();
        }else{
            int middle = (last+first)/2;
            System.out.printf("Task: Pending tasks: %s\n", this.getQueuedTaskCount());
            Task t1 = new Task(products, first, middle+1,increasement);
            Task t2 = new Task(products, middle+1, last, increasement);
            this.invokeAll(t1, t2);
        }
        
    }
    
    private void updatePrices(){
        for(int i=first;i<last;i++){
            Product product = products.get(i);
            product.setPrice(product.getPrice()*(1+increasement));
        }
    }
    
}

4. 实现范例的主类,创建Main主类,并实现main()方法。

import java.util.List;
import java.util.concurrent.ForkJoinPool;


public class Main {

    public static void main(String[] args) {
        ProductListGenerator generator = new ProductListGenerator();
        List<Product> products = generator.generate(10000);
        Task task = new Task(products, 0, products.size(), 0.20);
        //通过无参的类构造器创建一个ForkJoinPool
        ForkJoinPool pool = new ForkJoinPool();
        //调用execute()方法执行任务
        pool.execute(task);
        //显示关于线程池演变的信息
        try {
            while(!task.isDone()){
                System.out.printf("Main: Thread Count: %d\n", pool.getActiveThreadCount());
                System.out.printf("Main: Thread Steal: %d\n", pool.getStealCount());
                System.out.printf("Main: Parallelism: %d\n", pool.getParallelism());
                Thread.sleep(5);
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //关闭线程池
        pool.shutdown();
        if(task.isCompletedNormally()){
            System.out.println("Main: The process has completed normally.");
        }
        //确认是否所有的价格都已经改变
        for(int i=0;i<products.size();i++){
            Product product = products.get(i);
            if(product.getPrice()!=12)
                System.out.printf("Product %s: %f\n", product.getName(), product.getPrice());
        }
        System.out.println("Main: End of the program.");
    }
}

5. 程序运行结果如下

技术分享
Task: Pending tasks: 0
Main: Thread Count: 1
Main: Thread Steal: 0
Main: Parallelism: 2
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 9
Task: Pending tasks: 10
Task: Pending tasks: 9
Task: Pending tasks: 8
Task: Pending tasks: 9
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 9
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 9
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 9
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 9
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Main: Thread Count: 2
Main: Thread Steal: 0
Main: Parallelism: 2
Task: Pending tasks: 8
Task: Pending tasks: 9
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 6
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Main: Thread Count: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Main: Thread Steal: 0
Main: Parallelism: 2
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Main: Thread Count: 2
Main: Thread Steal: 0
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Main: Parallelism: 2
Task: Pending tasks: 3
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 6
Task: Pending tasks: 4
Main: Thread Count: 2
Task: Pending tasks: 5
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Main: Thread Steal: 0
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Main: Parallelism: 2
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Main: Thread Count: 2
Task: Pending tasks: 5
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Main: Thread Steal: 0
Main: Parallelism: 2
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 8
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 2
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Main: Thread Count: 2
Main: Thread Steal: 0
Main: Parallelism: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 4
Main: Thread Count: 2
Main: Thread Steal: 0
Main: Parallelism: 2
Task: Pending tasks: 3
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 7
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 6
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 5
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 4
Task: Pending tasks: 3
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 3
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 2
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 0
Task: Pending tasks: 1
Task: Pending tasks: 0
Task: Pending tasks: 5
Main: Thread Count: 2
Main: Thread Steal: 7
Main: Parallelism: 2
Main: The process has completed normally.
Main: End of the program.
View Code

   ForkJoinPool类还提供了一下方法用于执行任务。

  • execute(Runnabletask):这是本范例中使用的execute()方法的另一个版本。这个方法发送一个Runnable任务给ForkJoinPool类。需要注意的是,使用Runnable对象时ForkJoinPool类就不采用工作窃取算法,FrokJoinPool类仅在使用ForkJoinTask类时才采用工作窃取算法。
  • invoke(ForkJoinTask<T>task):正如范例所示,ForkJoinPool类的execute()方法是异步调用的,而ForkJoinPool类的invoke()方法则是同步调用的。这个方法传递进来的任务执行结束后才会返回。
  • 也可以使用ExecutorService类中声明的invokeAll()和invokeAny()方法,这些方法接收Callalbe对象作为参数。使用Callable对象时ForkJoinPool类就不采用工作窃取算法,因此,最好使用执行器来执行Callable对象。

  ForkJoinTask类也包含了在范例中使用的invokeAll()方法的其他版本,这些版本如下。

  • invokeAll(ForkJoinTask<?>...tasks):这个版本的方法接收一个可变的参数列表,可以传递尽可能多的ForkJoinTask对象给这个方法作为参数。
  • invokeAll(Collection<T>tasks):这个版本的方法接受一个泛型类型T的对象集合(比如,ArrayList对象、LinkedList对象或者TreeSet对象)。这个泛型类型T必须是ForkJoinTask类或者它的子类。

  虽然ForkJoinPool类是设计用来执行ForkJoinTask对象的,但也可以直接用来执行Runnable和Callable对象。当然,也可以使用ForkJoinTask类的adapt()方法来接收一个Callable对象或者一个Runnable对象,然后将之转换为一个ForkJoinTask对象,然后再去执行。

2. 合并任务的结果

  Fork/join框架提供了执行任务并返回结果的能力。这些类型的任务都是通过RecursiveTask类来实现的。RecursiveTask类继承了ForkJoinTask类,并且实现了由执行器框架提供的Future接口。

  在任务中,必须使用JavaAPI文档推荐的如下结构:

if (problem size > size) {
    tasks = Divide(task);
    execute(tasks);
    groupResults();
    return result;
} else {
    resolve problem;
    return result;
}

  如果任务需要解决的问题大于预先定义的大小,那么就要将这个问题拆分成多个子任务,并使用Fork/Join框架来执行这些子任务。执行完成后,原始任务获取到由所有这些子任务产生的结果,合并这些结果,返回最终的结果。当原始任务在线程池中执行结束后,将高效地获取到整个问题的最终结果。

  下面我们将学习如何使用Fork/Join框架来解决这种问题,开发一个应用程序,在文档中查找一个词。我们将实现以下两种任务:

  • 一个文档任务,它将遍历文档中的每一行来查找这个词;
  • 一个行任务,它将在文档的一部分当中查找这个词。

  所有这些任务将返回文档或行中所出现这个词的次数。

1. 

Fork/Join框架

标签:

原文地址:http://www.cnblogs.com/gaopeng527/p/4910508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!