AtomiceInteger 没有像 forjointtask 预期的那样增加

AtomiceInteger is not increased as expected with forjointask

我在 forkjointtask 中使用 AtmoicInteger 来计算它到达开始的次数 condition.But AtomicInteger 从未使用 incrementAndGet() 更新。详细代码如下,我做错了什么?

package Chapter7;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.atomic.AtomicInteger;
@SuppressWarnings("serial")
public class PrcForkJoinTask extends RecursiveAction {
    private int start , end;
    AtomicInteger count = new AtomicInteger(0);
    public PrcForkJoinTask(int start, int end){
        this.start =  start;
        this.end   = end;   
    }
    @Override
    protected void compute() {
        // TODO Auto-generated method stub
        if(end - start <= 2 ){
            System.out.println("close condition reached" + "Start is " +  start +  "end is " +  end) ;
            System.out.println("calculated times"  +  count.getAndIncrement());
            }
    else{ 
        int middle = start + ((end - start) / 2);
      System.out.println("middle is " +  middle) ;
      invokeAll( new PrcForkJoinTask(start,middle),new PrcForkJoinTask(middle,end));

            }       
    }


    public static void main(String[] args){

        ForkJoinTask<?> fs =  new PrcForkJoinTask(0,10);

        ForkJoinPool  pool = new ForkJoinPool();

        pool.invoke(fs);    
    }
}

每次在第

行递归
invokeAll( new PrcForkJoinTask(start,middle),new PrcForkJoinTask(middle,end));

它将创建具有新 AtomicInteger 计数 = 0 的新对象,然后它将在第

行增加 1
 System.out.println("calculated times"  +  count.getAndIncrement());

因此将创建多个新对象 PrcForkJoinTask,每个对象的计数 = 1

如果您想计算摘要,您需要在新的 PrcForkJoinTask 对象中传递计数。