日期:2014-05-17 浏览次数:20901 次
package org.zzuli.xmsb;
/**
* 封装一个进程的信息。
*
* @author 刘飞
*
*/
public class WindowsTask {
/**
* 映像名称
*/
private String name;
/**
* PID
*/
private Integer pid;
/**
* 会话名
*/
private String sessionName;
/**
* 会话编号
*/
private Integer sessionId;
/**
* 内存使用
*/
private long mem;
@Override
public String toString() {
return "映像名称:\t" + name + ", PID:\t" + pid + ", 会话名:\t" + sessionName
+ ", 会话编号:\t" + sessionId + ", 内存使用:\t" + mem + " K\n";
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the pid
*/
public Integer getPid() {
return pid;
}
/**
* @param pid the pid to set
*/
public void setPid(Integer pid) {
this.pid = pid;
}
/**
* @return the sessionName
*/
public String getSessionName() {
return sessionName;
}
/**
* @param sessionName the sessionName to set
*/
public void setSessionName(String sessionName) {
this.sessionName = sessionName;
}
/**
* @return the sessionId
*/
public Integer getSessionId() {
return sessionId;
}
/**
* @param sessionId the sessionId to set
*/
public void setSessionId(Integer sessionId) {
this.sessionId = sessionId;
}
/**
* @return the mem
*/
public long getMem() {
return mem;
}
/**
* @param mem the mem to set
*/
public void setMem(long mem) {
this.mem = mem;
}
}package org.zzuli.xmsb;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Set;
import java.util.Map.Entry;
/**
* 进程管理
*
* @author 刘飞
*
*/
public class JavaWindowsTaskManager {
private static final String X = " ";
private static final String TSKILL_CMD = "tskill";
private static final String REPLACEMENT = "";
private static final String STRING = ",";
private static final String K = "K";
private static final String UTF_8 = "UTF-8";
private static final String TASK_LIST_CMD = "taskList";
/**
* 杀死一个进程
*
* @param task
* 进程号
* @throws IOException
*
*/
public static void killTask(Integer pid) throws IOException {
/**
* 取得计算机所有进程任务列表
*/
Hashtable<Integer, WindowsTask> tasks = getTaskList();
WindowsTask task = tasks.get(pid);
if (task != null) {
Runtime.getRuntime().exec(TSKILL_CMD + X + pid);
}
}
/**
* 杀死指定名称的所有进程
*
* @param taskName
* 进程服务名称
* @throws IOException
*/
public static void killTask(String taskName) throws IOException {
Hashtable<Integer, WindowsTask> tasks = getTaskList();
Collection<WindowsTask> allTasks = tasks.values();
for (WindowsTask task : allTasks) {
if (task.getName().equals(taskName)) {
killTask(task.getPid());
}
}
}
/**
* 返回当前机器的所有进程
*
* @return
* @throws IOException
*/
public static Hashtable<Integer, WindowsTask> getTaskList()
throws IOException {
Hashtable<Integer, WindowsTask> tasks = new Hashtable<Integer, WindowsTask>();
Process process = Runtime.getRuntime().exec(TASK_LIST_CMD);
InputStreamReader in = new InputStreamReader(process.getInputStream(),
UTF_8);
BufferedReader reader = new BufferedReader(in);
String taskInfo = null;
String tmp = null;
while ((taskInfo = reader.readLine()) != null) {
if (taskInfo.trim().length() > 0
&& taskInfo.toUpperCase().indexOf(K) != -1) {
WindowsTask task = new WindowsTask();
tmp = taskInfo.substring(0, 26);
task.setName(tmp.trim());
tmp = taskInfo.substring(26, 35);
task.setPid(Integer.valueOf(tmp.trim()));
tmp = taskInfo.substring(35, 52);
task.setSessionName(tmp.trim());
tmp = taskInfo.substring(52, 64);
task.setSessionId(Integer.valueOf(tmp.trim()));
tmp = taskInfo.substring(6