package com.study.forkjoin; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.RecursiveTask; public class ForkJoinTest { static ArrayList<String> urls = new ArrayList<String>() { { add("http://www.baidu.com"); add("http://www.sina.com"); add("http://www.baidu.com"); add("http://www.sina.com"); add("http://www.baidu.com"); add("http://www.sina.com"); add("http://www.baidu.com"); add("http://www.sina.com"); add("http://www.baidu.com"); } }; static ForkJoinPool firkJoinPool = new ForkJoinPool(3, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); public static void main(String args[]) throws ExecutionException, InterruptedException { Job job = new Job(urls, 0, urls.size()); ForkJoinTask<String> forkJoinTask = firkJoinPool.submit(job); String result = forkJoinTask.get(); System.out.println(result); } public static String doRequest(String url) { // 模拟网络请求 return "Kody ... test ... " + url + " "; } static class Job extends RecursiveTask<String> { List<String> urls; int start; int end; public Job(List<String> urls, int start, int end) { this.urls = urls; this.start = start; this.end = end; } @Override protected String compute() { // 计算任务的大小 int count = end - start; // 指定拆分逻辑即可,递归拆分由框架实现 if (count <= 5) { // 直接执行 String result = ""; for (int i = start; i < end; i++) { String response = doRequest(urls.get(i)); result += response; } return result; } else { // 继续拆分任务 int x = (start + end) / 2; Job job1 = new Job(urls, start, x); job1.fork(); Job job2 = new Job(urls, x, end); job2.fork(); // 固定写法 String result = ""; result += job1.join(); result += job2.join(); return result; } } } }