forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelMax.java
More file actions
executable file
·66 lines (64 loc) · 2.49 KB
/
ParallelMax.java
File metadata and controls
executable file
·66 lines (64 loc) · 2.49 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* */ package ch32;
/* */
/* */ import java.util.concurrent.ForkJoinPool;
/* */ import java.util.concurrent.RecursiveTask;
/* */
/* */ public class ParallelMax {
/* */ public static void main(String[] args) {
/* 8 */ int N = 9000000;
/* 9 */ int[] list = new int[9000000];
/* 10 */ for (int i = 0; i < list.length; i++) {
/* 11 */ list[i] = i;
/* */ }
/* 13 */ long startTime = System.currentTimeMillis();
/* 14 */ System.out.println("\nThe maximal number is " + max(list));
/* 15 */ long endTime = System.currentTimeMillis();
/* 16 */ System.out.println("Number of processors is " +
/* 17 */ Runtime.getRuntime().availableProcessors());
/* 18 */ System.out.println("Time with " + (endTime - startTime) + " milliseconds");
/* */ }
/* */
/* */
/* */ public static int max(int[] list) {
/* 23 */ RecursiveTask<Integer> task = new MaxTask(list, 0, list.length);
/* 24 */ ForkJoinPool pool = new ForkJoinPool();
/* 25 */ return ((Integer)pool.<Integer>invoke(task)).intValue();
/* */ }
/* */
/* */ private static class MaxTask extends RecursiveTask<Integer> {
/* */ private static final int THRESHOLD = 1000;
/* */ private int[] list;
/* */ private int low;
/* */ private int high;
/* */
/* */ public MaxTask(int[] list, int low, int high) {
/* 35 */ this.list = list;
/* 36 */ this.low = low;
/* 37 */ this.high = high;
/* */ }
/* */
/* */
/* */ public Integer compute() {
/* 42 */ if (this.high - this.low < 1000) {
/* 43 */ int max = this.list[0];
/* 44 */ for (int i = this.low; i < this.high; i++) {
/* 45 */ if (this.list[i] > max)
/* 46 */ max = this.list[i];
/* 47 */ } return new Integer(max);
/* */ }
/* */
/* 50 */ int mid = (this.low + this.high) / 2;
/* 51 */ RecursiveTask<Integer> left = new MaxTask(this.list, this.low, mid);
/* 52 */ RecursiveTask<Integer> right = new MaxTask(this.list, mid, this.high);
/* */
/* 54 */ right.fork();
/* 55 */ left.fork();
/* 56 */ return new Integer(Math.max(((Integer)left.join()).intValue(), ((Integer)right
/* 57 */ .join()).intValue()));
/* */ }
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch32/ParallelMax.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/