Jad Chahine
Jad Chahine

Reputation: 7149

Process Management using JAVA

I am developing a swing application in java that shows the memory usage of each process on my pc

The result should appear in a JTable

The JTable contains 4 columns :

The JTable appear like

enter image description here

The JTable refresh itself each period using ProcessBuilder class

The problem that i felt that is method is a little bit slow and boring when the user see it

And my question:

Is there any other powerful method ? or what can i do in my code to enhance this application?

In the code :

  • The "processTable" field is the JTable dragged from palette (of type javax.swing.JTable)
  • The "status" field is the JLabel dragged also from palette (of type javax.swing.JLabel)

    The method that i am used to display all the process is :

    @SuppressWarnings("empty-statement")
        private void getProcessList() throws InterruptedException { 
            private DefaultTableModel table;        
            String filename = System.getProperty("java.io.tmpdir") + "\\list.txt", h;//to get %tmp%
            Scanner sc1 = null;
            int i, j, k = 0;
            table = (javax.swing.table.DefaultTableModel) processTable.getModel();
            processTable.editingStopped(null);
            table.setRowCount(0);
            try {
                ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "tasklist > %tmp%\\list.txt");
                builder.redirectErrorStream(true);
                Process p = builder.start();
                p.waitFor();
                try {
                    sc1 = new Scanner(new File(filename));
                    while (sc1.hasNext()) {
                        h = sc1.nextLine();
                        k++;
                    }
                    k = k - 5;
                } catch (FileNotFoundException ex) {
                    System.out.println("File " + filename + " Not Found");
                };
                status.setText(" Processes: " + k);
                Object o[] = new Object[4];
                i = 0;
                try {
                    j = 0;
                    sc1 = new Scanner(new File(filename));
                    while (sc1.hasNext()) {
                        st = new StringTokenizer(sc1.nextLine(), "\n");
                        i++;
                        if (i > 5) {
                            while (st.hasMoreTokens()) {
                                st1 = new StringTokenizer(st.nextToken(), " ");
                                if (st1.hasMoreTokens()) {
                                    o[0] = st1.nextToken();
                                }
                                if (st1.hasMoreTokens()) {
                                    h = st1.nextToken();
                                    if (h.contains(".exe")) {
                                        o[0] = o[0] + " " + h;
                                        o[1] = st1.nextToken();
                                    } else {
                                        o[1] = h;
                                    }
                                }
                                if (st1.hasMoreTokens()) {
                                    o[2] = st1.nextToken();
                                }
                                if (st1.hasMoreTokens()) {
                                    h = st1.nextToken();
                                }
                                if (st1.hasMoreTokens()) {
                                    o[3] = st1.nextToken();
                                }
                                if (st1.hasMoreTokens()) {
                                    h = st1.nextToken();
                                }
                                table.addRow(o);
                                j++;
                            }
                        } else {
                            if (st.hasMoreTokens()) {
                                h = st.nextToken();
                            }
                        }
                    }
                } catch (FileNotFoundException ex) {
                    System.out.println("File " + filename + " Not Found");
                } catch (Exception ex) {
                    System.out.println("UnknownError: " + ex.getMessage());
                } finally {
                    if (sc1 != null) {
                        sc1.close();
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(ProcessMan.class.getName()).log(Level.SEVERE, null, ex);
            }
    
        }
    

    Upvotes: 1

    Views: 1031

  • Answers (1)

    tucuxi
    tucuxi

    Reputation: 17935

    You can speed things up without too many changes by:

    • Using a separate thread to poll running processes (instead of using the EDT); and writing any changes to the JTable's TableModel.
    • Avoiding an intermediate file when calling tasklist, and instead reading all output to a string, which you would immediately parse.

    Otherwise, you could go native: see https://stackoverflow.com/a/13478716/15472 for a example.

    Upvotes: 1

    Related Questions