java Future 接口使用方法詳解
在Java中,如果需要設(shè)定代碼執(zhí)行的最長(zhǎng)時(shí)間,即超時(shí),可以用Java線程池ExecutorService類(lèi)配合Future接口來(lái)實(shí)現(xiàn)。 Future接口是Java標(biāo)準(zhǔn)API的一部分,在java.util.concurrent包中。Future接口是Java線程Future模式的實(shí)現(xiàn),可以來(lái)進(jìn)行異步計(jì)算。
Future模式可以這樣來(lái)描述:我有一個(gè)任務(wù),提交給了Future,F(xiàn)uture替我完成這個(gè)任務(wù)。期間我自己可以去做任何想做的事情。一段時(shí)間之后,我就便可以從Future那兒取出結(jié)果。就相當(dāng)于下了一張訂貨單,一段時(shí)間后可以拿著提訂單來(lái)提貨,這期間可以干別的任何事情。其中Future 接口就是訂貨單,真正處理訂單的是Executor類(lèi),它根據(jù)Future接口的要求來(lái)生產(chǎn)產(chǎn)品。
Future接口提供方法來(lái)檢測(cè)任務(wù)是否被執(zhí)行完,等待任務(wù)執(zhí)行完獲得結(jié)果,也可以設(shè)置任務(wù)執(zhí)行的超時(shí)時(shí)間。這個(gè)設(shè)置超時(shí)的方法就是實(shí)現(xiàn)Java程序執(zhí)行超時(shí)的關(guān)鍵。
Future接口是一個(gè)泛型接口,嚴(yán)格的格式應(yīng)該是Future<V>,其中V代表了Future執(zhí)行的任務(wù)返回值的類(lèi)型。 Future接口的方法介紹如下:
- boolean cancel (boolean mayInterruptIfRunning) 取消任務(wù)的執(zhí)行。參數(shù)指定是否立即中斷任務(wù)執(zhí)行,或者等等任務(wù)結(jié)束
- boolean isCancelled () 任務(wù)是否已經(jīng)取消,任務(wù)正常完成前將其取消,則返回 true
- boolean isDone () 任務(wù)是否已經(jīng)完成。需要注意的是如果任務(wù)正常終止、異常或取消,都將返回true
- V get () throws InterruptedException, ExecutionException 等待任務(wù)執(zhí)行結(jié)束,然后獲得V類(lèi)型的結(jié)果。InterruptedException 線程被中斷異常, ExecutionException任務(wù)執(zhí)行異常,如果任務(wù)被取消,還會(huì)拋出CancellationException
- V get (long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException 同上面的get功能一樣,多了設(shè)置超時(shí)時(shí)間。參數(shù)timeout指定超時(shí)時(shí)間,uint指定時(shí)間的單位,在枚舉類(lèi)TimeUnit中有相關(guān)的定義。如果計(jì)算超時(shí),將拋出TimeoutException
Future的實(shí)現(xiàn)類(lèi)有java.util.concurrent.FutureTask<V>即 javax.swing.SwingWorker<T,V>。通常使用FutureTask來(lái)處理我們的任務(wù)。FutureTask類(lèi)同時(shí)又實(shí)現(xiàn)了Runnable接口,所以可以直接提交給Executor執(zhí)行。使用FutureTask實(shí)現(xiàn)超時(shí)執(zhí)行的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
ExecutorService executor = Executors.newSingleThreadExecutor(); FutureTask<String> future = new FutureTask<String>( new Callable<String>() { //使用Callable接口作為構(gòu)造參數(shù) public String call() { //真正的任務(wù)在這里執(zhí)行,這里的返回值類(lèi)型為String,可以為任意類(lèi)型 }}); executor.execute(future); //在這里可以做別的任何事情 try { result = future.get( 5000 , TimeUnit.MILLISECONDS); //取得結(jié)果,同時(shí)設(shè)置超時(shí)執(zhí)行時(shí)間為5秒。同樣可以用future.get(),不設(shè)置執(zhí)行超時(shí)時(shí)間取得結(jié)果 } catch (InterruptedException e) { futureTask.cancel( true ); } catch (ExecutionException e) { futureTask.cancel( true ); } catch (TimeoutException e) { futureTask.cancel( true ); } finally { executor.shutdown(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
ExecutorService executor = Executors.newSingleThreadExecutor(); FutureTask<String> future = new FutureTask<String>( new Callable<String>() { //使用Callable接口作為構(gòu)造參數(shù) public String call() { //真正的任務(wù)在這里執(zhí)行,這里的返回值類(lèi)型為String,可以為任意類(lèi)型 }}); executor.execute(future); //在這里可以做別的任何事情 try { result = future.get( 5000 , TimeUnit.MILLISECONDS); //取得結(jié)果,同時(shí)設(shè)置超時(shí)執(zhí)行時(shí)間為5秒。同樣可以用future.get(),不設(shè)置執(zhí)行超時(shí)時(shí)間取得結(jié)果 } catch (InterruptedException e) { futureTask.cancel( true ); } catch (ExecutionException e) { futureTask.cancel( true ); } catch (TimeoutException e) { futureTask.cancel( true ); } finally { executor.shutdown(); } |
不直接構(gòu)造Future對(duì)象,也可以使用ExecutorService.submit方法來(lái)獲得Future對(duì)象,submit方法即支持以 Callable接口類(lèi)型,也支持Runnable接口作為參數(shù),具有很大的靈活性。使用示例如下:
1
2
3
4
5
6
7
8
|
ExecutorService executor = Executors.newSingleThreadExecutor(); FutureTask<String> future = executor.submit( new Callable<String>() { //使用Callable接口作為構(gòu)造參數(shù) public String call() { //真正的任務(wù)在這里執(zhí)行,這里的返回值類(lèi)型為String,可以為任意類(lèi)型 }}); //在這里可以做別的任何事情 //同上面取得結(jié)果的代碼 |
1
2
3
4
5
6
7
8
|
ExecutorService executor = Executors.newSingleThreadExecutor(); FutureTask<String> future = executor.submit( new Callable<String>() { //使用Callable接口作為構(gòu)造參數(shù) public String call() { //真正的任務(wù)在這里執(zhí)行,這里的返回值類(lèi)型為String,可以為任意類(lèi)型 }}); //在這里可以做別的任何事情 //同上面取得結(jié)果的代碼 |
利用Future接口實(shí)現(xiàn)程序執(zhí)行超時(shí)大致用法就這么多,改天需要研究下Future接口的內(nèi)部實(shí)現(xiàn),特別是設(shè)定執(zhí)行超時(shí)的實(shí)現(xiàn)。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!