Skip to content

Commit b350b02

Browse files
author
shibo
committed
作业2 future实现
1 parent 38f7242 commit b350b02

File tree

1 file changed

+34
-0
lines changed
  • 03concurrency/0301/src/main/java/java0/conc0303/homework03

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package java0.conc0303.homework03;
2+
3+
4+
import java.util.concurrent.Callable;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.FutureTask;
7+
8+
public class Homework1 {
9+
10+
public static void main(String[] args) throws InterruptedException, ExecutionException {
11+
long start = System.currentTimeMillis();
12+
13+
Homework1 demo = new Homework1();
14+
15+
FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
16+
@Override
17+
public Integer call() throws Exception {
18+
return demo.sum();
19+
}
20+
});
21+
new Thread(task).start();
22+
System.out.println(Thread.currentThread().getName() + "异步计算的结果为:" + task.get());
23+
System.out.println(Thread.currentThread().getName() + "使用时间:" + (System.currentTimeMillis() - start) + " ms");
24+
}
25+
26+
private static int sum() {
27+
return fibo(36);
28+
}
29+
30+
private static int fibo(int a) {
31+
if (a < 2)
32+
return 1;
33+
return fibo(a - 1) + fibo(a - 2);
34+
}}

0 commit comments

Comments
 (0)