大家过年好.新年一帆风顺!
工作中的某个需要多线程操作的功能,记录一下(简略代码)
利用线程池 ExecutorService 产生一个有固定线程数的线程池
其中如果任务多余线程数量进行队列分块进行解决.
public class ProcessListUtil{
public Map<String, Object> manyProcessForCloud(String[] task,XmlRpcClientConfigImpl xc,
XmlRpcClient client,Map<String, Object> logMap,String isUp,
Object[] parambak,Object[] parammkdir,Object[] params,Object[] paramstorestart,HttpSession session,
int successnum , int failnum) throws ExecutionException,
InterruptedException {
StringBuffer sb = new StringBuffer();
System.out.println("----程序开始运行----");
Date date1 = new Date();
// 创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(10);
// 创建多个有返回值的任务
List<Future> list = new ArrayList<Future>();//队列分块
while(task.length>10){
for (int i = 0; i < 10; i++) {
Callable c = new MyCallable(task[i],xc,client,isUp,parambak,parammkdir,params,paramstorestart);
// 执行任务并获取Future对象
Future f = pool.submit(c);
// System.out.println(">>>" + f.get().toString());
list.add(f);
}
task = Arrays.copyOfRange(task, 10, task.length);
}
for (int i = 0; i < task.length; i++) {
Callable c = new MyCallable(task[i],xc,client,isUp,parambak,parammkdir,params,paramstorestart);
// 执行任务并获取Future对象
Future f = pool.submit(c);
// System.out.println(">>>" + f.get().toString());
list.add(f);
}
// 关闭线程池
pool.shutdown();
// 获取所有并发任务的运行结果
for (Future f : list) {
try {
String obj = (String) f.get(1000 * 30, TimeUnit.MILLISECONDS);
System.out.println("the return value from call is :" + obj);
String fget = f.get().toString();
if(fget.endsWith("succ:1")){
successnum++;
}else{
failnum++;
}
logMap.put("SSH",fget.split("#")[0]);
session.setAttribute("complete", successnum+failnum);
sb.append(logMap.get("SSH"));
} catch (TimeoutException ex) {
System.out.println("====================task 30s time out===============");
logMap.put("SSH","====================task 30s time out===============");
boolean isCancel = f.cancel(true);
System.out.println("强制结束:"+isCancel);
continue;
} catch (Exception e) {
System.out.println("failed to handle.");
System.out.println(e.getMessage());
}
}
session.setAttribute("result", sb.toString());
logMap.put("successnum", successnum);
logMap.put("failnum", failnum);
Date date2 = new Date();
System.out.println("----程序结束运行----,程序运行时间【"
+ (date2.getTime() - date1.getTime()) + "毫秒】");
return logMap;
}
/**
* 强制结束某台机器 检查该机器连接状态
* @return
*/
public boolean checkMachineStatus(XmlRpcClientConfigImpl xc,final XmlRpcClient client) {
boolean bdStatus = false;
final ExecutorService exec = Executors.newFixedThreadPool(1);
Callable<String> call = new Callable<String>() {
public String call() throws Exception {
Object[] params = new Object[]{"ls"};
client.execute("swarmagent.executeCommand", params);
return "true";
}
};
Future<String> future = null;
try {
future = exec.submit(call);
// set db connection timeout to 10 seconds
String obj = future.get(1000 * 10, TimeUnit.MILLISECONDS);
bdStatus = Boolean.parseBoolean(obj);
System.out.println("the return value from call is :" + obj);
} catch (TimeoutException ex) {
System.out.println("====================task time out===============");
boolean isCancel = future.cancel(true);
System.out.println("强制结束:"+isCancel);
bdStatus = false;
} catch (Exception e) {
System.out.println("failed to handle.");
System.out.println(e.getMessage());
bdStatus = false;
}
// close thread pool
exec.shutdown();
return bdStatus;
}
}
class MyCallable implements Callable<Object> {
private String isUp;
private String taskNum;
private XmlRpcClientConfigImpl xc;
private XmlRpcClient client;
Object[] parambak = null,parammkdir = null,params,paramstorestart = null;
Object resx,resbak,resmkdir;
Object[] resu;
MyCallable(String taskNum,XmlRpcClientConfigImpl xc,XmlRpcClient client,String isUp,
Object[] parambak,Object[] parammkdir, Object[] params,Object[] paramstorestart) {
this.taskNum = taskNum;
this.xc = xc;
this.client = client;
this.isUp = isUp;
this.parambak= parambak;
this.parammkdir = parammkdir;
this.params= params;
this.paramstorestart=paramstorestart;
}
public Object call() throws Exception {
StringBuffer listresult = new StringBuffer();
xc.setServerURL(new URL("http://"+taskNum+":29001/RPC2"));
client.setConfig(xc);
try {
// if(!new ProcessListUtil().checkMachineStatus(xc,client)){
// listresult.append(taskNum+"--->Connection is time out! \n\n");
// return listresult + "#fail:1";
// }
if("yes".equals(isUp)){
resmkdir = client.execute("swarmagent.executeCommand", parammkdir);
resbak = client.execute("swarmagent.executeCommand", parambak);
Object[] xzmkdir = (Object[]) resmkdir;
Object[] xzbak = (Object[]) resbak;
if(!"0".equals(xzmkdir[0].toString())||!"0".equals(xzbak[0].toString())){
listresult.append("IP:"+taskNum+"BAK Fail,No Update!\r\n");
return listresult + "#fail:1";
}
}
resx = client.execute("swarmagent.executeCommand", params);
if(resx instanceof Object[]){
resu = (Object[]) resx;
if(!"0".equals(resu[0].toString())){
listresult.append("failIP:"+taskNum+"\n");
return listresult + "#fail:1";
}else{
if("yes".equals(isUp)){
try{
resx = client.execute("supervisor.stopProcess",paramstorestart);
resx = client.execute("supervisor.startProcess",paramstorestart);
listresult.append(taskNum+"ReStart:"+resx+"\n");
return listresult + "#succ:1";
}catch(Exception e){
resx = e.getMessage();
listresult.append(taskNum+":"+resx+"\n");
return listresult + "#fail:1";
}
}
}
//System.out.println(taskNum+resu[1]);
listresult.append(taskNum+"--->"+resu[1]+"\n\n");
}else{
listresult.append(taskNum+"--->"+resx+"\n\n");
listresult.append("IP:"+taskNum+"Update Fail!\r\n");
listresult.append("Reason:"+resx.toString()+"\r\n");
return listresult + "#fail:1";
}
} catch (XmlRpcException e) {
//e.printStackTrace();
resx=e.getMessage();
listresult.append(taskNum+"--->"+resx+"\n\n");
return listresult + "#fail:1";
}
return listresult + "#succ:1";
}
}