1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.util.internal;
17
18 import io.netty.util.concurrent.FastThreadLocalThread;
19
20 import java.lang.ref.ReferenceQueue;
21 import java.lang.ref.WeakReference;
22 import java.security.AccessController;
23 import java.security.PrivilegedAction;
24 import java.util.Set;
25 import java.util.concurrent.atomic.AtomicBoolean;
26
27 import static io.netty.util.internal.SystemPropertyUtil.getInt;
28 import static java.lang.Math.max;
29
30
31
32
33
34 public final class ObjectCleaner {
35 private static final int REFERENCE_QUEUE_POLL_TIMEOUT_MS =
36 max(500, getInt("io.netty.util.internal.ObjectCleaner.refQueuePollTimeout", 10000));
37
38
39 static final String CLEANER_THREAD_NAME = ObjectCleaner.class.getSimpleName() + "Thread";
40
41 private static final Set<AutomaticCleanerReference> LIVE_SET = new ConcurrentSet<AutomaticCleanerReference>();
42 private static final ReferenceQueue<Object> REFERENCE_QUEUE = new ReferenceQueue<Object>();
43 private static final AtomicBoolean CLEANER_RUNNING = new AtomicBoolean(false);
44 private static final Runnable CLEANER_TASK = new Runnable() {
45 @Override
46 public void run() {
47 boolean interrupted = false;
48 for (;;) {
49
50
51 while (!LIVE_SET.isEmpty()) {
52 final AutomaticCleanerReference reference;
53 try {
54 reference = (AutomaticCleanerReference) REFERENCE_QUEUE.remove(REFERENCE_QUEUE_POLL_TIMEOUT_MS);
55 } catch (InterruptedException ex) {
56
57 interrupted = true;
58 continue;
59 }
60 if (reference != null) {
61 try {
62 reference.cleanup();
63 } catch (Throwable ignored) {
64
65
66 }
67 LIVE_SET.remove(reference);
68 }
69 }
70 CLEANER_RUNNING.set(false);
71
72
73
74 if (LIVE_SET.isEmpty() || !CLEANER_RUNNING.compareAndSet(false, true)) {
75
76
77 break;
78 }
79 }
80 if (interrupted) {
81
82 Thread.currentThread().interrupt();
83 }
84 }
85 };
86
87
88
89
90
91
92
93
94 public static void register(Object object, Runnable cleanupTask) {
95 AutomaticCleanerReference reference = new AutomaticCleanerReference(object,
96 ObjectUtil.checkNotNull(cleanupTask, "cleanupTask"));
97
98
99 LIVE_SET.add(reference);
100
101
102 if (CLEANER_RUNNING.compareAndSet(false, true)) {
103 final Thread cleanupThread = new FastThreadLocalThread(CLEANER_TASK);
104 cleanupThread.setPriority(Thread.MIN_PRIORITY);
105
106
107
108
109
110 AccessController.doPrivileged(new PrivilegedAction<Void>() {
111 @Override
112 public Void run() {
113 cleanupThread.setContextClassLoader(null);
114 return null;
115 }
116 });
117 cleanupThread.setName(CLEANER_THREAD_NAME);
118
119
120
121 cleanupThread.setDaemon(true);
122 cleanupThread.start();
123 }
124 }
125
126 public static int getLiveSetCount() {
127 return LIVE_SET.size();
128 }
129
130 private ObjectCleaner() {
131
132 }
133
134 private static final class AutomaticCleanerReference extends WeakReference<Object> {
135 private final Runnable cleanupTask;
136
137 AutomaticCleanerReference(Object referent, Runnable cleanupTask) {
138 super(referent, REFERENCE_QUEUE);
139 this.cleanupTask = cleanupTask;
140 }
141
142 void cleanup() {
143 cleanupTask.run();
144 }
145
146 @Override
147 public Thread get() {
148 return null;
149 }
150
151 @Override
152 public void clear() {
153 LIVE_SET.remove(this);
154 super.clear();
155 }
156 }
157 }