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!)!
을 복원 .. –
가입 결과를 사용하면 훨씬 재미 있습니다 http://ideone.com/BIjFjQ – zapl
고마워요! 그래, 그냥 내 머리를 포크 비트 먼저 가져 가려고했다. 이제 조인 부분을 살펴보십시오. – user384842