为线程执行设置timeout

来源:互联网 发布:淘宝店铺怎么申请 编辑:程序博客网 时间:2024/06/05 16:38
转自 http://623deyingxiong.iteye.com/blog/1753975
Java代码  收藏代码
  1. package com.test.threads;  
  2.   
  3. import java.util.concurrent.ExecutionException;  
  4. import java.util.concurrent.ExecutorService;  
  5. import java.util.concurrent.Executors;  
  6. import java.util.concurrent.Future;  
  7. import java.util.concurrent.TimeUnit;  
  8. import java.util.concurrent.TimeoutException;  
  9.   
  10. /** 
  11.  * @author Hawkins 
  12.  * 
  13.  * 
  14.  *在指定时间内执行某个task,超时则退出执行。 
  15.  */  
  16. public class TimedCall {  
  17.     public static void main(String[] args) throws InterruptedException,  
  18.             ExecutionException, TimeoutException {  
  19.         long timeout = 1000;// 任务必须在设定时间内完成,否则任务将被强制关闭  
  20.         long timeNeed = 2000;// 任务完成需要的时长。  
  21.         TimeUnit timeUnit = TimeUnit.MILLISECONDS;// 时间单位  
  22.         ExecutorService executor = Executors.newSingleThreadExecutor();// 高级并发API  
  23.   
  24.         Runnable task = new MyThread(timeNeed, timeUnit);  
  25.   
  26.         while (!timedCall(executor, task, timeout, timeUnit))  
  27.             ;// 在某些场景下,需要不断尝试执行任务,直到能够在限定时间内执行完毕。  
  28.   
  29.     }  
  30.   
  31.     private static boolean timedCall(ExecutorService executor, Runnable c,  
  32.             long timeout, TimeUnit timeUnit) throws InterruptedException,  
  33.             ExecutionException {  
  34.         // FutureTask<?> task = new FutureTask<Object>(c, null);  
  35.         // executor.execute(task);  
  36.         //        
  37.         // task.get(timeout, timeUnit);  
  38.         Future<?> future = executor.submit(c);  
  39.         try {  
  40.             future.get(timeout, timeUnit);  
  41.             return true;  
  42.         } catch (TimeoutException e) {  
  43.             future.cancel(true);// 参数设为true,向执行线程发送中断通知。否则,允许已经启动的线程继续执行直到完成任务。  
  44.             System.err.println("任务执行超时,强制退出");  
  45.             return false;  
  46.         }  
  47.     }  
  48. }  
  49.   
  50. class MyThread implements Runnable {  
  51.     long timeLong = 0;// how long thread running will cost  
  52.     TimeUnit timeUnit;  
  53.   
  54.     public MyThread() {  
  55.     }  
  56.   
  57.     public MyThread(long milli, TimeUnit timeUnit) {  
  58.         this.timeLong = milli;  
  59.         this.timeUnit = timeUnit;  
  60.     }  
  61.   
  62.     @Override  
  63.     public void run() {  
  64.         System.out.println("---------" + Thread.currentThread().getName()  
  65.                 + "开始执行,时长[" + timeLong + "]------");  
  66.         try {  
  67.             Thread.sleep(timeLong);  
  68.         } catch (InterruptedException e) {  
  69.             System.err.println("线程中断,退出");  
  70.             return;// 必须响应中断,否则无法退出线程。在退出之前你可能还需做一些资源回收等等。  
  71.         }  
  72.         System.out.println("---------" + Thread.currentThread().getName()  
  73.                 + "执行完毕,时长[" + timeLong + "]------");  
  74.   
  75.     }  
  76. }  
0 0
原创粉丝点击