• 1.3.4 Fork/Join框架


    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;
    			}
    		}
    	}
    
    }
    
  • 相关阅读:
    SP3871 GCDEX
    P2424 约数和
    P6561 [SBCOI2020] 人
    POJ
    约数之和(acwing)
    Codeforces Round #677 (Div. 3)EF
    P1516 青蛙的约会
    VJ的MNNUrank的E
    K. Birdwatching(2019-2020 ICPC Southwestern European Regional Programming Contest (SWERC 2019-20))
    友情提示,本博客仅用于博主自己复习,不适合学习者进行学习
  • 原文地址:https://www.cnblogs.com/yfzhou528/p/11337189.html
Copyright © 2020-2023  润新知