JAVAスレッドプール:タイムアウト待ちスレッド呼び出し
13366 ワード
Executors.newSingleThreadExecutor()
Executors.newFixedThreadPool()
Executors.newCachedThreadPool()
Executors.newScheduledThreadPool()
newFixedThreadPool()の適用
よびだし
,
Executors.newFixedThreadPool()
,
Executors.newCachedThreadPool()
, ( OOM), 60
Executors.newScheduledThreadPool()
ScheduledExecutorService
newFixedThreadPool()の適用
/**
* Created by hurf on 2016/8/24.
*
*/
public class ThreadsUtil {
public static ExecutorService executor = Executors.newFixedThreadPool(12);//
/**
*
* @param callable
* @return
* :
* String result = ThreadsUtil.execInTime(new CallableImpl(" "));
* class CallableImpl implements Callable { private String key = ""; public CallableImpl(String key) {this.key = key;} ...}
*/
public static T execInTime(Callable callable) {
Future future = executor.submit(callable);
T t = null;
try {
t = future.get(CallAtomUtil.timeOutLen, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
String cuase = ObjectUtils.toString(e.getCause()).replace(toReplaceStr,",");
String eMsg = " , :【"+cuase+"】";
LogUtil.error(eMsg);
future.cancel(true);
throw new AtomException(-6787,eMsg, FrameworkConstants.BIZ_LVL);
} catch (ExecutionException e) {
String cuase = ObjectUtils.toString(e.getCause()).replace(toReplaceStr,",");
String eMsg = " , :【"+cuase+"】";
LogUtil.error(eMsg);
future.cancel(true);
throw new AtomException(-6787,eMsg, FrameworkConstants.BIZ_LVL);
} catch (TimeoutException e) {
String eMsg = " ";
LogUtil.error(eMsg);
future.cancel(true);
throw new AtomException(-6789, eMsg, FrameworkConstants.BIZ_LVL);
}
return t;
}
/**
*
* @param callable
* @param timeOut 30000 30
* @param
* @return
*/
public static T execInTime(Callable callable,long timeOut){
Future future = executor.submit(callable);
T t = null;
try {
t = future.get(timeOut, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
String cuase = ObjectUtils.toString(e.getCause()).replace(toReplaceStr,",");
String eMsg = " , :【"+cuase+"】";
LogUtil.error(eMsg);
future.cancel(true);
throw new AtomException(-6787,eMsg, FrameworkConstants.BIZ_LVL);
} catch (ExecutionException e) {
String cuase = ObjectUtils.toString(e.getCause()).replace(toReplaceStr,",");
String eMsg = " , :【"+cuase+"】";
LogUtil.error(eMsg);
future.cancel(true);
throw new AtomException(-6787,eMsg, FrameworkConstants.BIZ_LVL);
} catch (TimeoutException e) {
String eMsg = " ";
LogUtil.error(eMsg);
future.cancel(true);
throw new AtomException(-6789, eMsg, FrameworkConstants.BIZ_LVL);
}
return t;
}
/**
*
* @param callable
* @param
* @return
*/
public static T exec(Callable callable){
Future future = executor.submit(callable);
T t = null;
try {
t = future.get();
} catch (InterruptedException e) {
String cuase = ObjectUtils.toString(e.getCause()).replace(toReplaceStr,",");
String eMsg = " , :【"+cuase+"】";
LogUtil.error(eMsg);
future.cancel(true);
throw new AtomException(-6787,eMsg, FrameworkConstants.BIZ_LVL);
} catch (ExecutionException e) {
String cuase = ObjectUtils.toString(e.getCause()).replace(toReplaceStr,",");
String eMsg = " , :【"+cuase+"】";
LogUtil.error(eMsg);
future.cancel(true);
throw new AtomException(-6787,eMsg, FrameworkConstants.BIZ_LVL);
}
return t;
}
/**
*
* @param callable
* @param
* @return
*/
public static T lookDb(Callable callable){
if("1".equals(CallAtomUtil.remoteCallTimeOutFlag)){
long timeOut = CallAtomUtil.timeOutLen+10000;
return execInTime(callable,timeOut);
}else {
return exec(callable);
}
}
}
よびだし
List