/Users/lyon/j4p/src/javagroup/process/ProcessGarbageCollector.java

1    /* 
2     * Copyright (C) 1997 Luke Gorrie 
3     * 
4     * This library is free software; you can redistribute it and/or 
5     * modify it under the terms of the GNU Library General Public 
6     * License as published by the Free Software Foundation; either 
7     * version 2 of the License, or (at your option) any later version. 
8     * 
9     * This library is distributed in the hope that it will be useful, 
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of 
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12    * Library General Public License for more details. 
13    * 
14    * You should have received a copy of the GNU Library General Public 
15    * License along with this library; if not, write to the 
16    * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
17    * MA 02139, USA. 
18    */ 
19    
20   package javagroup.process; 
21    
22   /** 
23    * Garbage collector class.  A simple thread that calls doGarbageCollect() 
24    * on the ProcessManager every so often (every 5 secs by default). 
25    * <p/> 
26    * TODO: fine-tune timings 
27    * 
28    * @author Luke Gorrie 
29    */ 
30   public class ProcessGarbageCollector implements Runnable { 
31    
32       // default time between collections 
33       public static final long DEFAULT_INTERVAL = 5000; 
34    
35       /** 
36        * ProcessManager to call garbage collection on. * 
37        */ 
38       protected ProcessManager _manager; 
39       /** 
40        * time between calls. * 
41        */ 
42       protected long _waitTime; 
43    
44       public ProcessGarbageCollector(ProcessManager manager) { 
45           this(manager, DEFAULT_INTERVAL); 
46       } 
47    
48       public ProcessGarbageCollector(ProcessManager manager, 
49                                      long interval) { 
50    
51           _manager = manager; 
52           _waitTime = interval; 
53    
54           Thread main_thread = new Thread(this, 
55                   "Process garbage collector"); 
56           main_thread.setPriority(Thread.NORM_PRIORITY + 1); 
57           main_thread.setDaemon(true); 
58           main_thread.start(); 
59    
60       } 
61    
62       public synchronized void wakeUp() { 
63           notifyAll(); 
64       } 
65    
66       public void run() { 
67    
68           while (true) { 
69               try { 
70                   synchronized (this) { 
71                       wait(_waitTime); 
72                   } 
73               } catch (InterruptedException e) { 
74               } 
75               _manager.doGarbageCollect(); 
76           } 
77    
78       } 
79    
80   } 
81    
82