2017-02-19 8 views
1

AtmIic은 AtomInteger를 forkjointask 내부에서 사용하여 여러 번 계산할 수 있습니다.하지만 AtomicInteger는 incrementAndGet()으로 업데이트되지 않았습니다.foromointask와 함께 AtomiceInteger가 예상대로 증가하지 않습니다.

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);  
    } 
} 
+0

* 그러나 AtomicInteger는 절대로 incrementAndGet() *으로 업데이트되지 않았습니다. 예상되는 결과는 무엇입니까? 실제 생산량은 얼마입니까? – CKing

+0

count.getAndIncrement()이 항목이 업데이트되었습니다. 작업이 6 개의 작업으로 분할되기 때문입니다. 이 '카운트'에 대한 예상 결과는 6입니다. – Sanchelz

+0

그리고 실제 결과는 무엇입니까? – CKing

답변

0

System.out.println("calculated times" + count.getAndIncrement()); 

따라서 다중 라인에서 1만큼 증가한다 그런 다음 새로운 AtomicInteger 카운트 = 0의 새로운 객체를 생성한다

invokeAll(new PrcForkJoinTask(start,middle),new PrcForkJoinTask(middle,end)); 

라인에서 재귀마다 새 객체 PrcForkJoinTask가 생성되고 각각의 개수는 count = 1입니다.

원한다면 새로운 PrcForkJoinTask 객체를 계산하기 위해 필요한 요약을 계산합니다.