View | Details | Raw Unified | Return to bug 48771
Collapse All | Expand All

(-)sources/org/apache/batik/util/CleanerThread.java (-15 / +30 lines)
Lines 34-50 Link Here
34
 */
34
 */
35
public class CleanerThread extends Thread {
35
public class CleanerThread extends Thread {
36
36
37
    static volatile ReferenceQueue queue = null;
37
    volatile ReferenceQueue<Object> queue;
38
    static CleanerThread  thread = null;
38
    public static final CleanerThread  THREAD = new CleanerThread();
39
40
    public static ReferenceQueue getReferenceQueue() {
41
39
42
        if ( queue == null ) {
40
    public synchronized ReferenceQueue<Object> getReferenceQueue() {
43
            synchronized (CleanerThread.class) {
44
                queue = new ReferenceQueue();
45
                thread = new CleanerThread();
46
            }
47
        }
48
        return queue;
41
        return queue;
49
    }
42
    }
50
43
Lines 65-71 Link Here
65
    public abstract static class SoftReferenceCleared extends SoftReference
58
    public abstract static class SoftReferenceCleared extends SoftReference
66
      implements ReferenceCleared {
59
      implements ReferenceCleared {
67
        public SoftReferenceCleared(Object o) {
60
        public SoftReferenceCleared(Object o) {
68
            super (o, CleanerThread.getReferenceQueue());
61
            super (o, THREAD.getReferenceQueue());
69
        }
62
        }
70
    }
63
    }
71
64
Lines 76-82 Link Here
76
    public abstract static class WeakReferenceCleared extends WeakReference
69
    public abstract static class WeakReferenceCleared extends WeakReference
77
      implements ReferenceCleared {
70
      implements ReferenceCleared {
78
        public WeakReferenceCleared(Object o) {
71
        public WeakReferenceCleared(Object o) {
79
            super (o, CleanerThread.getReferenceQueue());
72
            super (o, THREAD.getReferenceQueue());
80
        }
73
        }
81
    }
74
    }
82
75
Lines 88-109 Link Here
88
        extends PhantomReference
81
        extends PhantomReference
89
        implements ReferenceCleared {
82
        implements ReferenceCleared {
90
        public PhantomReferenceCleared(Object o) {
83
        public PhantomReferenceCleared(Object o) {
91
            super (o, CleanerThread.getReferenceQueue());
84
            super (o, THREAD.getReferenceQueue());
92
        }
85
        }
93
    }
86
    }
94
87
95
    protected CleanerThread() {
88
    protected CleanerThread() {
96
        super("Batik CleanerThread");
89
        super("Batik CleanerThread");
90
        queue = new ReferenceQueue<Object>();
97
        setDaemon(true);
91
        setDaemon(true);
98
        start();
92
        start();
99
    }
93
    }
100
94
101
    public void run() {
95
    public void run() {
102
        while(true) {
96
    	ReferenceQueue<Object> rq;
97
        while ((rq = getReferenceQueue ()) != null) {
103
            try {
98
            try {
104
                Reference ref;
99
                Reference ref;
105
                try {
100
                try {
106
                    ref = queue.remove();
101
                    ref = rq.remove();
107
                    // System.err.println("Cleaned: " + ref);
102
                    // System.err.println("Cleaned: " + ref);
108
                } catch (InterruptedException ie) {
103
                } catch (InterruptedException ie) {
109
                    continue;
104
                    continue;
Lines 120-123 Link Here
120
            }
115
            }
121
        }
116
        }
122
    }
117
    }
118
    
119
    /**
120
     * Stops the cleaner thread. Calling this method is recommended in all long running applications
121
     * with custom class loaders (e.g., web applications).
122
     */
123
    public void exit() {
124
        // try to stop it gracefully
125
        synchronized (this) {
126
            queue = null;
127
        }
128
        this.interrupt();
129
        try {
130
            this.join(500);
131
        } catch (InterruptedException e) {
132
133
        }
134
        // last resort tentative to kill the cleaner thread
135
        if (this.isAlive())
136
            this.stop();
137
    }
123
}
138
}

Return to bug 48771