-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
125 lines (110 loc) · 3.66 KB
/
Test.java
File metadata and controls
125 lines (110 loc) · 3.66 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package fork_join;
import java.io.FileOutputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.channels.ServerSocketChannel;
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;
/**
* @author : 张勇杰
* @date : 2019/9/23 16:19
* @Version : v1.0
* @description
**/
public class Test {
private static int threshold = 100;
private static int sum = 0;
static class MyRecursive extends RecursiveTask<Long>{
Long sum = Long.valueOf(0);
private int left;
private int right;
public MyRecursive(int left, int right) {
this.left = left;
this.right = right;
}
@Override
protected Long compute() {
if(right - left <= threshold){
for(int i = left; i <= right ; i++){
sum += i;
}
}else{
int mid = (left + right) / 2;
MyRecursive left = new MyRecursive(this.left,mid);
left.fork();
MyRecursive right = new MyRecursive(mid,this.right);
right.fork();
Long l = left.join();
Long r = right.join();
sum = l + r;
}
return sum;
}
}
public static void main(String[] args) throws Throwable {
// ForkJoinPool commonpool = new ForkJoinPool();
// MyRecursive myRecursive = new MyRecursive(1,100);
// ForkJoinTask<Long> result = commonpool.submit(myRecursive);
// System.out.println(result.get());
// commonpool.shutdown();
// ForkJoinPool commonpool = new ForkJoinPool();
// List<User> userList = new ArrayList<>();
//
// for(int i = 0 ;i< 100;i++){
// userList.add(new User());
// }
// UserRecursiveAction userRecursiveAction = new UserRecursiveAction(userList);
// commonpool.submit(userRecursiveAction);
// System.out.println(userList);
// commonpool.shutdown();
// System.out.println(sum(100));
// fanxing<String,Integer> fanxin = new fanxing<>("1",1);
// Object object = new String("11");
//// getPrintlnMH(object).invokeExact("avcc");
// FileOutputStream out = new FileOutputStream("D:\\a.txt");
// out.write(97);
// out.close();
// List<Integer> list = new ArrayList<>();
// list.add(1);
// list.add(2);
// list.add(1);
// list.add(3);
// list.add(4);
// list.add(5);
// list.add(6);
// list.add(7);
// list.add(8);
// for(Integer temp : list){
// if(temp.equals(1)){
// list.remove(temp);
// }
//// list.removeAll(list);
// }
System.out.println(TestStatic.date);
System.out.println(TestStatic.date);
System.out.println(TestStatic.date);
}
public static MethodHandle getPrintlnMH(Object reveiver) throws NoSuchMethodException, IllegalAccessException {
MethodType mt = MethodType.methodType(void.class,String.class);
return MethodHandles.lookup().findVirtual(reveiver.getClass(),"println",mt).bindTo(reveiver);
}
public static int sum(int n){
if(n == 1){
return 1;
}
return n+sum(n-1);
}
static class fanxing<S,D>{
S data;
D data2;
public fanxing(S data, D data2) {
this.data = data;
this.data2 = data2;
}
}
}