1 package com.explosion.utilities.process;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import com.explosion.utilities.process.threads.ProcessThread;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 /***
30 * @author Stephen Created on Apr 23, 2004
31 */
32 public class ProcessMonitor
33 {
34 private static List allProcesses = new ArrayList();
35 private static List userProcesses = new ArrayList();
36
37 /***
38 * Add a process to the list of processes
39 * @param processThread
40 * @param isUserProcess - this parameter allows for a disticntion to be made between processes that the
41 * user has kicked off and processes that the system has kicked off. This is essentially for listing user processes
42 * as a separate group so that the user can be shown a pretty list and also so that they can
43 * be deterred from killing processes that they shouldn't
44 */
45 public static synchronized void registerProcess(ProcessThread processThread, boolean isUserProcess)
46 {
47 update();
48 allProcesses.add(processThread);
49 if (processThread.getProcess() != null && processThread.getProcess().isUserProcess())
50 userProcesses.add(processThread);
51 }
52
53 /***
54 * Get a list of currently active processes
55 * @return
56 */
57 public static List getAllProcessThreads()
58 {
59 update();
60 return allProcesses;
61 }
62
63 /***
64 * Get a list of currently active User processes
65 * @return
66 */
67 public synchronized static List getUserProcessThreads()
68 {
69 update();
70 return userProcesses;
71 }
72
73 /***
74 * Update the list of currently active processes and remove dead ones
75 *
76 */
77 private synchronized static void update()
78 {
79 List keepList1 = new ArrayList();
80 List keepList2 = new ArrayList();
81 for (Iterator it = allProcesses.iterator(); it.hasNext();)
82 {
83 ProcessThread p = (ProcessThread) it.next();
84 if (p.getProcess().getPercentComplete() != 100)
85 {
86 keepList1.add(p);
87 if (p.getProcess().isUserProcess())
88 keepList2.add(p);
89 }
90 }
91
92 allProcesses = keepList1;
93 userProcesses = keepList2;
94 }
95
96 }