2014-11-25 4 views
0

ForkJoinPools을 이해하려고 노력 중이므로 다음 간단한 테스트 클래스를 만들었습니다. 불행히도 그것은 내가 기대하는 것을하지 않습니다. 아무도 내가 잘못 가고있는 것을 지적 할 수 있습니까?ForkJoinPool이 모든 작업을 실행하지 않는 이유는 무엇입니까?

:

import java.util.*; 
import java.util.concurrent.*; 

public class UtilsShowcase { 

    public static void main(String[] args){ 
      final UtilsShowcase us = new UtilsShowcase(); 
      us.run(); 
    } 

    public void run(){ 
      forkJoinPoolDemo(); 
    } 

    public void forkJoinPoolDemo(){ 
      final ForkJoinPool forkJoinPool = new ForkJoinPool(); 

      Future<String> result = forkJoinPool.submit(new MyTask(8)); 
      try { 
        result.get(); 
      } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } catch (ExecutionException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
    }  
    private class MyTask extends RecursiveTask<String> { 
      final long num; 

      MyTask(long number){ 
        this.num = number; 
      } 

      protected String compute(){ 
        System.out.println("In compute with number of "+num); 
        if (num > 1){ 
          System.out.println("Generating two more tasks with number of "+num/2); 

          (new MyTask(num/2)).fork(); 
          (new MyTask(num/2)).fork(); 

        } 
        return "Returning from a number "+num; 
      } 
    } 
} 
는 나는 그것이 숫자 4에 대한 두 가지 작업을 킥오프 후 숫자 8로 시작하고, 기대 한

는 2 번 4 작업 및 번호 1 8 작업 대신이 출력합니다 이 2 4 번 작업을 출력하지만, (대신 4) 단 3 수 2 초과 1이 개 번호

많은 감사 -

In compute with number of 8 
Generating two more tasks with number of 4 
In compute with number of 4 
Generating two more tasks with number of 2 
In compute with number of 4 
Generating two more tasks with number of 2 
In compute with number of 2 
Generating two more tasks with number of 1 
In compute with number of 2 
Generating two more tasks with number of 1 
In compute with number of 1 
In compute with number of 2 
In compute with number of 1 

그것은 일종의 권리의 일부를 가져옵니다 (대신 8!)!

+1

을 복원 .. –

+1

가입 결과를 사용하면 훨씬 재미 있습니다 http://ideone.com/BIjFjQ – zapl

+0

고마워요! 그래, 그냥 내 머리를 포크 비트 먼저 가져 가려고했다. 이제 조인 부분을 살펴보십시오. – user384842

답변

1

아, 여기에 내 자신의 질문에 대답.

다른 스레드가 모든 작업을 수행하기 전에 기본 스레드가 종료되고 ForkJoinPool이 데몬이기 때문에 자동 종료되었습니다. 내가 방법 forkJoinPoolDemo의 말에

  try { 
        Thread.sleep(10000); 
      } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 

를 추가하면, 나는 내가 기대 한 결과를 얻을 :

In compute with number of 8 
Generating two more tasks with number of 4 
In compute with number of 4 
Generating two more tasks with number of 2 
In compute with number of 2 
Generating two more tasks with number of 1 
In compute with number of 4 
In compute with number of 1 
Generating two more tasks with number of 2 
In compute with number of 2 
Generating two more tasks with number of 1 
In compute with number of 2 
Generating two more tasks with number of 1 
In compute with number of 1 
In compute with number of 1 
In compute with number of 1 
In compute with number of 1 
In compute with number of 1 
In compute with number of 2 
Generating two more tasks with number of 1 
In compute with number of 1 
In compute with number of 1 

정신이 하위이 갈래 만에 합류 결코 얻을 :-)

관련 문제