Hound mode call interface


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

/****
* target The method has no return value. Use this method to call

* @param task
* calling code
* @param unit
* timeout class
* @param timeout
* time
* @throws TimeoutException
* The call exceeds the specified time and throws this exception
*/
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);
}

}

/*****
* This is used when the target method has a return value Method call

* @param <T>
* @param task
* call code
* @param unit
* timeout type
* @param timeout
* time
* @return the return value of the called function
* @throws TimeoutException
* thrown when the call exceeds the specified time This exception occurs
*/
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();
}




}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326932346&siteId=291194637