列出系统中所有的线程信息的方法


import java.io.IOException;
import java.util.Scanner;

public class JavaWindowsCommandUtil {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        // startTask("E:\\Fetion\\Fetion.exe");
//        killTask("javaw");
    	showTaskList();
    	
    }

    /**
     * 杀死一个进程
     * 
     * @param task
     */
    public static void killTask(String task) {

        try {
            Process process = Runtime.getRuntime().exec("taskList");
            Scanner in = new Scanner(process.getInputStream());
            int count = 0;
            while (in.hasNextLine()) {
                count++;
                String temp = in.nextLine();

                if (temp.contains(task)) {
                    String[] t = temp.split(" ");
                    // 判断该进程所占内存是否大于20M
                    if (Integer.parseInt(t[t.length - 2].replace(",", "")) > 20000) {
                        temp = temp.replaceAll(" ", "");
                        // 获得pid
                        String pid = temp.substring(9, temp.indexOf("Console"));
                        Runtime.getRuntime().exec("tskill " + pid);

                        // dos下开cmd窗口 ntsd -c q -p PID
                        // Runtime.getRuntime().exec("ntsd -c q -p 1528");
                    }
                }
                // System.out.println(count + ":" + temp);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 显示当前机器的所有进程
     */
    public static void showTaskList() {

        try {
            Process process = Runtime.getRuntime().exec("taskList");
            Scanner in = new Scanner(process.getInputStream());
            int count = 0;
            while (in.hasNextLine()) {
                count++;
                System.out.println(count + ":" + in.nextLine());
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 启动一个进程
     * 
     * @param task
     */
    public static void startTask(String task) {
        try {
            Runtime.getRuntime().exec(task);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


你可能感兴趣的:(列出系统中所有的线程信息的方法)