猎狗模式~~~ 学习了 .

 
  • import java.util.concurrent.Callable;  
  • import java.util.concurrent.ExecutorService;  
  • import java.util.concurrent.Executors;  
  • import java.util.concurrent.Future;  
  • import java.util.concurrent.ThreadFactory;  
  • import java.util.concurrent.TimeUnit;  
  • import java.util.concurrent.TimeoutException;  
  •   
  • /**** 
  •  * 用于进黑洞抓取东西的函数猎狗 
  •  *  
  •  * @author  
  •  *  
  •  */  
  • public class FuncCourser {  
  •     private FuncCourser() {  
  •     }  
  •   
  •     private static ExecutorService executor = Executors  
  •             .newCachedThreadPool(new ThreadFactory() {  
  •                 int nCount = 0;  
  •   
  •                 public Thread newThread(Runnable task) {  
  •                     nCount++;  
  •                     Thread invokeThread = new Thread(task);  
  •                     invokeThread.setName("Invoker-thread-" + nCount);  
  •                     invokeThread.setDaemon(true);  
  •                     return invokeThread;  
  •                 }  
  •             });  
  •   
  •     /**** 
  •      * 目标方法无返回值使用该方法调用 
  •      *  
  •      * @param task 
  •      *            调用代码 
  •      * @param unit 
  •      *            超时时间类 
  •      * @param timeout 
  •      *            时间 
  •      * @throws TimeoutException 
  •      *             调用超过指定的时间抛出此异常 
  •      */  
  •     public static void call(Runnable task, TimeUnit unit, long timeout)  
  •             throws TimeoutException {  
  •         Future<?> futureResult = executor.submit(task);  
  •         try {  
  •             futureResult.get(timeout, unit);  
  •         } catch (Exception e) {  
  •             if (e instanceof TimeoutException) {  
  •                 throw new TimeoutException("invoke timeout!");  
  •             }  
  •             throw new RuntimeException(e);  
  •         }  
  •   
  •     }  
  •   
  •     /***** 
  •      * 目标方法有返回值时使用该方法调用 
  •      *  
  •      * @param <T> 
  •      * @param task 
  •      *            调用代码 
  •      * @param unit 
  •      *            超时时间类型 
  •      * @param timeout 
  •      *            时间 
  •      * @return 被调用函数的返回值 
  •      * @throws TimeoutException 
  •      *             调用超过指定时间时抛出此异常 
  •      */  
  •     public static <T> T call(Callable<?> task, TimeUnit unit, long timeout)  
  •             throws TimeoutException {  
  •         Future<?> futureResult = executor.submit(task);  
  •         Object callRet = null;  
  •         try {  
  •             callRet = futureResult.get(timeout, unit);  
  •         } catch (Exception e) {  
  •             if (e instanceof TimeoutException) {  
  •                 throw new TimeoutException("invoke timeout!");  
  •             }  
  •             throw new RuntimeException(e);  
  •         }  
  •         return (T) callRet;  
  •     }  
  •   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值