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();
}
}
}
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();
}
}
}