-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallableTest.java
More file actions
42 lines (32 loc) · 1 KB
/
Copy pathCallableTest.java
File metadata and controls
42 lines (32 loc) · 1 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
package ThreadTest.others;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class CallableTest {
public static void main(String[] args) {
NumCount numCount = new NumCount();
FutureTask<Integer> futureTask = new FutureTask<Integer>(numCount);
Thread t1 = new Thread(futureTask);
t1.setName("counter 1");
t1.start();
try {
int sum = futureTask.get();
System.out.println("total sum is : "+ sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class NumCount implements Callable {
public int sum = 0;
@Override
public Object call() throws Exception {
for(int i = 0; i<= 100; i++){
sum += i;
System.out.println(Thread.currentThread().getName() + ": " + i);
}
return sum;
}
}