Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/org/adblockplus/libadblockplus/Disposer.java

Issue 5698450620416000: Replaced the Disposer (Closed)
Patch Set: Created April 30, 2014, 11:17 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.libadblockplus;
19
20 import java.lang.ref.ReferenceQueue;
21 import java.lang.ref.WeakReference;
22 import java.util.HashSet;
23
24 public final class Disposer extends WeakReference<Disposable>
25 {
26 final static ReferenceQueue<Disposable> referenceQueue = new ReferenceQueue<Di sposable>();
27 final static HashSet<Disposer> disposerSet = new HashSet<Disposer>();
Felix Dahlke 2014/04/30 15:34:08 Should be "static final".
René Jeschke 2014/04/30 15:43:16 Done.
28 private final Disposable disposable;
29 private volatile boolean isDisposed = false;
Felix Dahlke 2014/04/30 15:34:08 Should be "disposed" according to the bean convent
René Jeschke 2014/04/30 15:43:16 Done.
30
31 static
32 {
33 final Thread thread = new Thread(new Cleaner());
34 thread.setName(Cleaner.class.getCanonicalName());
35 thread.setDaemon(true);
36 thread.start();
37 }
38
39 public Disposer(final Disposable referent, final Disposable disposable)
Felix Dahlke 2014/04/30 15:34:08 How about renaming "disposable" to "reference"? Th
René Jeschke 2014/04/30 15:43:16 'referent' is the reference that get stored in the
Felix Dahlke 2014/04/30 15:50:43 Isn't disposable the object that _gets_ disposed o
40 {
41 super(referent, referenceQueue);
Felix Dahlke 2014/04/30 15:34:08 Shouldn't it be the referentQueue if it stores the
René Jeschke 2014/04/30 15:43:16 It stores the value from a local variable called '
42 this.disposable = disposable;
43
44 synchronized (disposerSet)
45 {
46 disposerSet.add(this);
47 }
48 }
49
50 public synchronized void dispose()
51 {
52 if (!this.isDisposed)
53 {
54 try
55 {
56 this.disposable.dispose();
57 }
58 catch (final Throwable t)
59 {
60 // catch to set state to 'disposed' on all circumstances
61 }
62
63 this.isDisposed = true;
64 synchronized (disposerSet)
65 {
66 disposerSet.remove(this);
67 }
68 }
69 }
70
71 private static final class Cleaner implements Runnable
72 {
73 public Cleaner()
74 {
75 //
76 }
77
78 @Override
79 public void run()
80 {
81 for (;;)
82 {
83 try
84 {
85 ((Disposer) Disposer.referenceQueue.remove()).dispose();
86 }
87 catch (final Throwable t)
88 {
89 // ignored
90 }
91 }
92 }
93 }
94 }
OLDNEW

Powered by Google App Engine
This is Rietveld