1 2 3 4 5 6 7 8 9 10 11 12 13 | public class WorkerHello extends AbstractWorker { public WorkerHello() throws RemoteException { super("hello"); } public Warehouse doWork(Work work) throws RemoteException { String name = work.getInputWarehouse().get("name"); System.out.println(String.format("id %s: Hello %s", getId(), name)); Warehouse outputWarehouse = new WarehouseDefault(); outputWarehouse.put("helloInfo", "Hello," + name); return outputWarehouse; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { JobCenter jobCenter = new JobCenterLocal(); for (int i = 0; i < 5; i++) { jobCenter.registerWorker(new WorkerHello()); } Foreman helloForeman = new ForemanSelectOneWorker("hello"); jobCenter.registerForeman(helloForeman); Warehouse inputWarehouse = new WarehouseDefault(); inputWarehouse.put("name", "world"); Work work = new WorkDefault("hello", inputWarehouse); Warehouse outputWarehouse = jobCenter.doWork(work); System.out.println(outputWarehouse.get("helloInfo")); jobCenter.stop(); } |
1 2 | id 46fbffdeb18b45f28cda4617795c2a52: Hello world Hello,world |
1 2 3 4 5 6 7 8 | 职业介绍所:JobCenter,主要用于注册工人,注册包工头,接受或处理任务; 包工头:领取工作并招募工人,完成工作,并返回结果 工人:就是我们常说的民工了,只知道来料加工,处于生态环境的低层,最后还没有得工资 工作:只有工作类型和来料仓库 仓库:用于放各种来料或成品 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static void main(String[] args) throws IOException, ClassNotFoundException { JobCenter jobCenter = new JobCenterLocal(); for (int i = 0; i < 5; i++) { jobCenter.registerWorker(new WorkerHello()); } Foreman helloForeman = new ForemanSelectAllWorker("hello"); jobCenter.registerForeman(helloForeman); Warehouse inputWarehouse = new WarehouseDefault(); inputWarehouse.put("name", "world"); Work work = new WorkDefault("hello", inputWarehouse); jobCenter.doWork(work); jobCenter.stop(); } |
1 2 3 4 5 | id 83274d8f8c194bb89d773c232e867cc4: Hello world id 16fbf219d3cf4ba48eef23c260de509a: Hello world id 9c17a119a4f341d68b589a503712b0f9: Hello world id e7e3b2bdc9444a179ad62abdd35275e1: Hello world id 4b12a1b70f5d43e2bff473382096dfbe: Hello world |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static void main(String[] args) throws IOException, ClassNotFoundException { JobCenter jobCenter = new JobCenterLocal(); for (int i = 0; i < 5; i++) { jobCenter.registerWorker(new WorkerHello()); } Foreman helloForeman = new ForemanSelectAllWorker("hello", new HelloWorkCombiner()); jobCenter.registerForeman(helloForeman); Warehouse inputWarehouse = new WarehouseDefault(); inputWarehouse.put("name", "world"); Work work = new WorkDefault("hello", inputWarehouse); Warehouse outputWarehouse = jobCenter.doWork(work); List<String> result = outputWarehouse.get("helloInfo"); System.out.println(result.size()); jobCenter.stop(); } |
1 2 3 4 5 6 7 8 9 10 11 12 | public class HelloWorkCombiner implements WorkCombiner { public Warehouse combine(List<Warehouse> warehouseList) throws RemoteException { Warehouse warehouse = new WarehouseDefault(); List<String> helloList = new ArrayList<String>(); for (Warehouse w : warehouseList) { helloList.add((String) w.get("helloInfo")); } warehouse.put("helloInfo", helloList); return warehouse; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class WorkerSum extends AbstractWorker { public WorkerSum() throws RemoteException { super("sum"); } public Warehouse doWork(Work work) throws RemoteException { long start = (Long) work.getInputWarehouse().get("start"); long end = (Long) work.getInputWarehouse().get("end"); long sum = 0; for (long i = start; i <= end; i++) { sum += i; } Warehouse outputWarehouse = new WarehouseDefault(); outputWarehouse.put("sum", sum); return outputWarehouse; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class SumSplitterCombiner implements WorkSplitterCombiner { public List<Warehouse> split(Work work, List<Worker> workers) throws RemoteException { List<Warehouse> list = new ArrayList<Warehouse>(); long start = (Long) work.getInputWarehouse().get("start"); long end = (Long) work.getInputWarehouse().get("end"); long count = end - start + 1; long step = count / workers.size(); for (int i = 0; i < workers.size(); i++) { Warehouse subInputWarehouse = new WarehouseDefault(); subInputWarehouse.put("start", step * i + start); if (i == workers.size() - 1) { subInputWarehouse.put("end", end); } else { subInputWarehouse.put("end", step * (i + 1)); } list.add(subInputWarehouse); } return list; } public Warehouse combine(List<Warehouse> warehouseList) throws RemoteException { Warehouse outputWarehouse = new WarehouseDefault(); long sum = 0; for (Warehouse w : warehouseList) { sum += (Long) w.get("sum"); } outputWarehouse.put("sum", sum); return outputWarehouse; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { JobCenter jobCenter = new JobCenterLocal(); JobCenter center = new JobCenterRemote(); for (int i = 0; i < 5; i++) { center.registerWorker(new WorkerSum()); } Foreman helloForeman = new ForemanSelectAllWorker("sum", new SumSplitterCombiner()); center.registerForeman(helloForeman); Warehouse inputWarehouse = new WarehouseDefault(); inputWarehouse.put("start", 1l); inputWarehouse.put("end", 10000l); Work work = new WorkDefault("sum", inputWarehouse); Warehouse outputWarehouse = center.doWork(work); System.out.println(outputWarehouse.get("sum")); jobCenter.stop(); center.stop(); } } |
1 2 | inputWarehouse.put("start", 1l); inputWarehouse.put("end", 10000l); |
1 | 50005000 |
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class WorkerHello extends AbstractWorker { public WorkerHello() throws RemoteException { super("hello"); } public Warehouse doWork(Work work) throws RemoteException { String name = work.getInputWarehouse().get("name"); System.out.println(String.format("id %s: Hello %s", getId(), name)); Warehouse outputWarehouse = new WarehouseDefault(); outputWarehouse.put("name", name + "_1"); return outputWarehouse; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class TestSerialWork { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { JobCenter jobCenter = new JobCenterLocal(); for (int i = 0; i < 5; i++) { jobCenter.registerWorker(new WorkerHello()); } Foreman helloForeman = new ForemanSelectOneWorker("hello"); jobCenter.registerForeman(helloForeman); Warehouse inputWarehouse = new WarehouseDefault(); inputWarehouse.put("name", "world"); Work work = new WorkDefault("hello", inputWarehouse); work.setNextWork(new WorkDefault("hello")).setNextWork(new WorkDefault("hello")); Warehouse warehouse = jobCenter.doWork(work); System.out.println(warehouse.get("name")); jobCenter.stop(); } } |
1 | work.setNextWork(new WorkDefault("hello")).setNextWork(new WorkDefault("hello")); |
1 2 3 4 | id 2a53a967e3b84289beb3dbaf12a7d8be: Hello world id e3d471c27e264a1a87cf263605bfe9bd: Hello world_1 id 2a53a967e3b84289beb3dbaf12a7d8be: Hello world_1_1 world_1_1_1 |
1 2 3 4 5 6 7 8 | public static void main(String[] args) { double pi=0.0; for(double i=1.0;i<1000000001d;i++){ pi += Math.pow(-1,i+1)/(2*i-1); } System.out.println(4*pi); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class PiWorker extends AbstractWorker { public PiWorker() throws RemoteException { super("pi"); } @Override protected Warehouse doWork(Work work) throws RemoteException { long m = (Long) work.getInputWarehouse().get("start"); long n = (Long) work.getInputWarehouse().get("end"); double pi = 0.0d; for (double i = m; i < n; i++) { pi += Math.pow(-1, i + 1) / (2 * i - 1); } work.getInputWarehouse().put("pi", 4 * pi); return work.getInputWarehouse(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class PiSplitterCombiner implements WorkSplitterCombiner { public List<Warehouse> split(Work work, List<Worker> workers) throws RemoteException { List<Warehouse> list = new ArrayList<Warehouse>(); long start = (Long) work.getInputWarehouse().get("start"); long end = (Long) work.getInputWarehouse().get("end"); long count = end - start + 1; long step = count / workers.size(); for (int i = 0; i < workers.size(); i++) { Warehouse subInputWarehouse = new WarehouseDefault(); subInputWarehouse.put("start", step * i + start); if (i == workers.size() - 1) { subInputWarehouse.put("end", end); } else { subInputWarehouse.put("end", step * (i + 1)); } list.add(subInputWarehouse); } return list; } public Warehouse combine(List<Warehouse> warehouseList) throws RemoteException { Warehouse outputWarehouse = new WarehouseDefault(); double pi = 0d; for (Warehouse w : warehouseList) { pi += (Double) w.get("pi"); } outputWarehouse.put("pi", pi); return outputWarehouse; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { JobCenter jobCenter = new JobCenterLocal(); for (int i = 0; i < 10; i++) { jobCenter.registerWorker(new PiWorker()); } Foreman helloForeman = new ForemanSelectAllWorker("pi", new PiSplitterCombiner()); jobCenter.registerForeman(helloForeman); Warehouse inputWarehouse = new WarehouseDefault(); inputWarehouse.put("start", 1l); inputWarehouse.put("end", 1000000001l); Work work = new WorkDefault("pi", inputWarehouse); Warehouse outputWarehouse = jobCenter.doWork(work); System.out.println("pi:"+outputWarehouse.get("pi")); jobCenter.stop(); } } |
1 2 3 4 | 并行计算运行结果: time:10326ms pi:3.141592694075038 单线程计算运行结果 time:24857ms pi:3.1415926525880504 |