猎狗方式调用接口

package boce.hit.dog;

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;

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);
System.out.println("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;
}




public static <T> T callRunnable(Runnable task, TimeUnit unit, long timeout)
throws TimeoutException {
Object result ="sucess";
Object obj = null;
Future<?> futureResult = executor.submit(task,result);
try {
obj = futureResult.get(timeout, unit);
System.out.println(obj+"====="+result);
} catch (Exception e) {
if (e instanceof TimeoutException) {
throw new TimeoutException("invoke timeout!");
}
throw new RuntimeException(e);
}
return (T)obj;

}

public static void main(String[] args) {

// //调用程序
// Callable<String> task = new Callable<String>(){
// @Override
// public String call() throws Exception {
// System.out.println("================="+Thread.currentThread().getName());
// Thread.sleep(2500);
// return "back";
// }
//
// };
//
// try {
// for(int i=0;i<1;i++){
// System.out.println(""+i);
// String str = FuncCourser.call(task, TimeUnit.SECONDS, 2);
// System.out.println("show:::"+str);
// }
// } catch (TimeoutException e) {
// e.printStackTrace();
// }



Runnable ru = new Runnable(){
@Override
public void run() {
try {
Thread.sleep(2500);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("test123456");
}
};


try {
for(int i=0;i<1;i++){
System.out.println(""+i);
System.out.println(FuncCourser.callRunnable(ru, TimeUnit.SECONDS, 3));
}
} catch (TimeoutException e) {
e.printStackTrace();
}




}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值