KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > CleanerThread


1 /*
2
3    Copyright 2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util;
19
20 import java.lang.ref.Reference JavaDoc;
21 import java.lang.ref.ReferenceQueue JavaDoc;
22 import java.lang.ref.SoftReference JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.lang.ref.PhantomReference JavaDoc;
25
26 /**
27  * One line Class Desc
28  *
29  * Complete Class Desc
30  *
31  * @author <a HREF="mailto:deweese@apache.org">l449433</a>
32  * @version $Id: CleanerThread.java,v 1.7 2005/03/27 08:58:36 cam Exp $
33  */

34 public class CleanerThread extends Thread JavaDoc {
35
36     static ReferenceQueue JavaDoc queue = null;
37     static CleanerThread thread = null;
38
39     public static ReferenceQueue JavaDoc getReferenceQueue() {
40         if (queue != null)
41             return queue;
42         
43         queue = new ReferenceQueue JavaDoc();
44         thread = new CleanerThread();
45         return queue;
46     }
47
48     /**
49      * If objects registered with the reference queue associated with
50      * this class implement this interface then the 'cleared' method
51      * will be called when the reference is queued.
52      */

53     public static interface ReferenceCleared {
54         /* Called when the reference is cleared */
55         public void cleared();
56     }
57
58     /**
59      * A SoftReference subclass that automatically registers with
60      * the cleaner ReferenceQueue.
61      */

62     public static abstract class SoftReferenceCleared extends SoftReference JavaDoc
63       implements ReferenceCleared {
64         public SoftReferenceCleared(Object JavaDoc o) {
65             super (o, CleanerThread.getReferenceQueue());
66         }
67     }
68
69     /**
70      * A WeakReference subclass that automatically registers with
71      * the cleaner ReferenceQueue.
72      */

73     public static abstract class WeakReferenceCleared extends WeakReference JavaDoc
74       implements ReferenceCleared {
75         public WeakReferenceCleared(Object JavaDoc o) {
76             super (o, CleanerThread.getReferenceQueue());
77         }
78     }
79
80     /**
81      * A PhantomReference subclass that automatically registers with
82      * the cleaner ReferenceQueue.
83      */

84     public static abstract class PhantomReferenceCleared
85         extends PhantomReference JavaDoc
86         implements ReferenceCleared {
87         public PhantomReferenceCleared(Object JavaDoc o) {
88             super (o, CleanerThread.getReferenceQueue());
89         }
90     }
91             
92     protected CleanerThread() {
93         setDaemon(true);
94         start();
95     }
96
97     public void run() {
98         while(true) {
99             try {
100                 Reference JavaDoc ref;
101                 try {
102                     ref = queue.remove();
103                     // System.err.println("Cleaned: " + ref);
104
} catch (InterruptedException JavaDoc ie) {
105                     continue;
106                 }
107
108                 if (ref instanceof ReferenceCleared) {
109                     ReferenceCleared rc = (ReferenceCleared)ref;
110                     rc.cleared();
111                 }
112             } catch (ThreadDeath JavaDoc td) {
113                 throw td;
114             } catch (Throwable JavaDoc t) {
115                 t.printStackTrace();
116             }
117         }
118     }
119 }
120
Popular Tags