-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumCallable.java
More file actions
61 lines (50 loc) · 1.73 KB
/
SumCallable.java
File metadata and controls
61 lines (50 loc) · 1.73 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
package com.cat.multi.sum;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
/**
* Created by cat on 2018/1/21.
*/
public class SumCallable implements Callable<Integer> {
private static final boolean DEBUG = true;
private final int start;
private final int end;
public SumCallable(int start, int end) {
this.start = start;
this.end = end;
}
public SumCallable(int end) {
this(0, end);
}
@Override
public Integer call() throws Exception {
int result = 0;
if (DEBUG) {
for (long x = start; x <= end; x++) {
result += x;
Thread.sleep(10);
if (x % 20 == 0) {
System.out.println("----x=" + x + " , start==" + start);
}
}
} else {
// 1+2+3+4
// (2+4)*(5-2)/2 = 6*3/2= 9 = 2+3+4;
result = (start + end) * (1 + end - start) / 2;
}
System.err.println(Thread.currentThread().getThreadGroup().getName() + " , " +
Thread.currentThread().getThreadGroup().activeCount() + " , " +
Thread.currentThread().getThreadGroup().activeGroupCount() + " ### " +
Thread.currentThread().getName() + " , " + Thread.currentThread().getState().name() + " ..."
);
System.out.println("\n##########\n");
Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
Set<Thread> threads = traces.keySet();
for (Thread th : threads) {
System.err.println(th + ":" + Arrays.toString(traces.get(th)));
}
System.out.println("==============");
return result;
}
}