1. 程式人生 > >Java程序執行超時——Future接口介紹

Java程序執行超時——Future接口介紹

-m throw 最長 結果 方法 cell pub 異步 取消

在Java中,如果需要設定代碼執行的最長時間,即超時,可以用Java線程池ExecutorService類配合Future接口來實現。 Future接口是Java標準API的一部分,在java.util.concurrent包中。Future接口是Java線程Future模式的實 現,可以來進行異步計算。

Future模式可以這樣來描述:我有一個任務,提交給了Future,Future替我完成這個任務。期間我自己可以去做任何想做的事情。一段時 間之後,我就便可以從Future那兒取出結果。就相當於下了一張訂貨單,一段時間後可以拿著提訂單來提貨,這期間可以幹別的任何事情。其中Future 接口就是訂貨單,真正處理訂單的是Executor類,它根據Future接口的要求來生產產品。

Future接口提供方法來檢測任務是否被執行完,等待任務執行完獲得結果,也可以設置任務執行的超時時間。這個設置超時的方法就是實現Java程 序執行超時的關鍵。

Future接口是一個泛型接口,嚴格的格式應該是Future<V>,其中V代表了Future執行的任務返回值的類型。 Future接口的方法介紹如下:

  • boolean cancel (boolean mayInterruptIfRunning) 取消任務的執行。參數指定是否立即中斷任務執行,或者等等任務結束
  • boolean isCancelled () 任務是否已經取消,任務正常完成前將其取消,則返回 true
  • boolean isDone
    () 任務是否已經完成。需要註意的是如果任務正常終止、異常或取消,都將返回true
  • V get () throws InterruptedException, ExecutionException 等待任務執行結束,然後獲得V類型的結果。InterruptedException 線程被中斷異常, ExecutionException任務執行異常,如果任務被取消,還會拋出CancellationException
  • V get (long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException 同上面的get功能一樣,多了設置超時時間。參數timeout指定超時時間,uint指定時間的單位,在枚舉類TimeUnit中有相關的定義。如果計 算超時,將拋出TimeoutException

Future的實現類有java.util.concurrent.FutureTask<V>即 javax.swing.SwingWorker<T,V>。通常使用FutureTask來處理我們的任務。FutureTask類同時又 實現了Runnable接口,所以可以直接提交給Executor執行。使用FutureTask實現超時執行的代碼如下:

Java代碼 技術分享
  1. ExecutorService executor = Executors.newSingleThreadExecutor();
  2. FutureTask<String> future =
  3. new FutureTask<String>(new Callable<String>() {//使用Callable接口作為構造參數
  4. public String call() {
  5. //真正的任務在這裏執行,這裏的返回值類型為String,可以為任意類型
  6. }});
  7. executor.execute(future);
  8. //在這裏可以做別的任何事情
  9. try {
  10. result = future.get(5000, TimeUnit.MILLISECONDS); //取得結果,同時設置超時執行時間為5秒。同樣可以用future.get(),不設置執行超時時間取得結果
  11. } catch (InterruptedException e) {
  12. futureTask.cancel(true);
  13. } catch (ExecutionException e) {
  14. futureTask.cancel(true);
  15. } catch (TimeoutException e) {
  16. futureTask.cancel(true);
  17. } finally {
  18. executor.shutdown();
  19. }

不直接構造Future對象,也可以使用ExecutorService.submit方法來獲得Future對象,submit方法即支持以 Callable接口類型,也支持Runnable接口作為參數,具有很大的靈活性。使用示例如下:

Java代碼 技術分享
  1. ExecutorService executor = Executors.newSingleThreadExecutor();
  2. FutureTask<String> future = executor.submit(
  3. new Callable<String>() {//使用Callable接口作為構造參數
  4. public String call() {
  5. //真正的任務在這裏執行,這裏的返回值類型為String,可以為任意類型
  6. }});
  7. //在這裏可以做別的任何事情
  8. //同上面取得結果的代碼

實現Future接口:

[java] view plain copy
  1. private class FutureTest<V> implements Future<V>{
  2. @Override
  3. public boolean cancel(boolean arg0) {
  4. // TODO Auto-generated method stub
  5. return false;
  6. }
  7. @Override
  8. public V get() throws InterruptedException, ExecutionException {
  9. // TODO Auto-generated method stub
  10. return null;
  11. }
  12. @Override
  13. public V get(long arg0, TimeUnit arg1) throws InterruptedException,
  14. ExecutionException, TimeoutException {
  15. // TODO Auto-generated method stub
  16. return null;
  17. }
  18. @Override
  19. public boolean isCancelled() {
  20. // TODO Auto-generated method stub
  21. return false;
  22. }
  23. @Override
  24. public boolean isDone() {
  25. // TODO Auto-generated method stub
  26. return false;
  27. }
  28. }

實現Callbale接口:

[java] view plain copy
  1. private class CallableTest<V> implements Callable<V>{
  2. @Override
  3. public V call() throws Exception {
  4. // TODO Auto-generated method stub
  5. return null;
  6. }
  7. }

Java程序執行超時——Future接口介紹