1 18 package org.apache.batik.util; 19 20 import java.lang.ref.Reference ; 21 import java.lang.ref.ReferenceQueue ; 22 import java.lang.ref.SoftReference ; 23 import java.lang.ref.WeakReference ; 24 import java.lang.ref.PhantomReference ; 25 26 34 public class CleanerThread extends Thread { 35 36 static ReferenceQueue queue = null; 37 static CleanerThread thread = null; 38 39 public static ReferenceQueue getReferenceQueue() { 40 if (queue != null) 41 return queue; 42 43 queue = new ReferenceQueue (); 44 thread = new CleanerThread(); 45 return queue; 46 } 47 48 53 public static interface ReferenceCleared { 54 55 public void cleared(); 56 } 57 58 62 public static abstract class SoftReferenceCleared extends SoftReference 63 implements ReferenceCleared { 64 public SoftReferenceCleared(Object o) { 65 super (o, CleanerThread.getReferenceQueue()); 66 } 67 } 68 69 73 public static abstract class WeakReferenceCleared extends WeakReference 74 implements ReferenceCleared { 75 public WeakReferenceCleared(Object o) { 76 super (o, CleanerThread.getReferenceQueue()); 77 } 78 } 79 80 84 public static abstract class PhantomReferenceCleared 85 extends PhantomReference 86 implements ReferenceCleared { 87 public PhantomReferenceCleared(Object 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 ref; 101 try { 102 ref = queue.remove(); 103 } catch (InterruptedException ie) { 105 continue; 106 } 107 108 if (ref instanceof ReferenceCleared) { 109 ReferenceCleared rc = (ReferenceCleared)ref; 110 rc.cleared(); 111 } 112 } catch (ThreadDeath td) { 113 throw td; 114 } catch (Throwable t) { 115 t.printStackTrace(); 116 } 117 } 118 } 119 } 120 | Popular Tags |