File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
03concurrency/0301/src/main/java/java0/conc0303/homework03 Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ }}
You can’t perform that action at this time.
0 commit comments