1.接口的定義:
1
2
3
4
|
public interface callable<v> { v call() throws exception; } |
2.callable和runnable的異同
先看下runnable接口的定義
1
2
3
|
public interface runnable { public abstract void run(); } |
callable的call()方法類似于runnable接口中run()方法,都定義任務要完成的工作,實現這兩個接口時要分別重寫這兩個方法,主要的不同之處是call()方法是有返回值的(其實還有一些區別,例如call方法可以拋出異常,run方法不可以),運行callable任務可以拿到一個future對象,表示異步計算的結果。它提供了檢查計算是否完成的方法,以等待計算的完成,并檢索計算的結果。通過future對象可以了解任務執行情況,可取消任務的執行,還可獲取執行結果。
3. callable類型的任務可以有兩種執行方式:
我們先定義一個callable任務mycallabletask:
1
2
3
4
5
6
7
8
9
10
11
|
class mycallabletask implements callable<integer>{ @override public integer call() throws exception { system.out.println( "線程在進行計算" ); thread.sleep( 3000 ); int sum = 0 ; for ( int i= 0 ;i< 100 ;i++) sum += i; return sum; } } |
①借助futuretask執行
futuretask類同時實現了兩個接口,future和runnable接口,所以它既可以作為runnable被線程執行,又可以作為future得到callable的返回值。
借助futuretask執行的大體流程是:
1
2
3
|
callable<integer> mycallabletask = new mycallabletask(); futuretask<integer> futuretask= new futuretask<integer>(mycallabletask); new thread(futuretask).start(); |
通過futuretask可以得到mycallabletask的call()的運行結果: futuretask.get();
②借助線程池來運行
線程池中執行callable任務的原型例如:
1
2
3
4
5
6
7
|
public interface executorservice extends executor { //提交一個callable任務,返回值為一個future類型 <t> future<t> submit(callable<t> task); //other methods... } |
借助線程池來運行callable任務的一般流程為:
1
2
|
executorservice exec = executors.newcachedthreadpool(); future<integer> future = exec.submit( new mycallabletask()); |
通過future可以得到mycallabletask的call()的運行結果: future.get();
在網上看到了幾個比較好的代碼例子:
a.callable任務借助futuretask運行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class callableandfuturetask { public static void main(string[] args) { callable<integer> callable = new callable<integer>() { public integer call() throws exception { return new random().nextint( 100 ); } }; futuretask<integer> future = new futuretask<integer>(callable); new thread(future).start(); try { thread.sleep( 5000 ); system.out.println(future.get()); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } } } |
b.callable任務和線程池一起使用,然后返回值是future:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class callableandfuture { public static void main(string[] args) { executorservice threadpool = executors.newsinglethreadexecutor(); future<integer> future = threadpool.submit( new callable<integer>() { public integer call() throws exception { return new random().nextint( 100 ); } }); try { thread.sleep( 5000 ); // 可能做一些事情 system.out.println(future.get()); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } } } |
c.當執行多個callable任務,有多個返回值時,我們可以創建一個future的集合,例如:
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
|
class mycallabletask implements callable<string> { private int id; public onetask( int id){ this .id = id; } @override public string call() throws exception { for ( int i = 0 ;i< 5 ;i++){ system.out.println( "thread" + id); thread.sleep( 1000 ); } return "result of callable: " +id; } } public class test { public static void main(string[] args) { //callable<string> mycallabletask = new mycallabletask(1); executorservice exec = executors.newcachedthreadpool(); arraylist<future<string>> results = new arraylist<future<string>>(); for ( int i = 0 ; i < 5 ; i++) { results.add(exec.submit( new mycallabletask(i))); } for (future<string> fs : results) { if (fs.isdone()) { try { system.out.println(fs.get()); } catch (exception e) { e.printstacktrace(); } } else { system.out.println( "mycallabletask任務未完成!" ); } } exec.shutdown(); } } |
那么引入callable接口具有哪些好處呢?
①可以獲得任務執行返回值;
②通過與future的結合,可以實現利用future來跟蹤異步計算的結果。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/sunp823/article/details/51569314