提交 f98f5db6 authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 411: CloseWatcher registration was not concurency-safe.

上级 a747d787
...@@ -13,7 +13,9 @@ import java.io.PrintWriter; ...@@ -13,7 +13,9 @@ import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.lang.ref.PhantomReference; import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue; import java.lang.ref.ReferenceQueue;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set;
/** /**
* A phantom reference to watch for unclosed objects. * A phantom reference to watch for unclosed objects.
...@@ -23,13 +25,13 @@ public class CloseWatcher extends PhantomReference<Object> { ...@@ -23,13 +25,13 @@ public class CloseWatcher extends PhantomReference<Object> {
/** /**
* The queue (might be set to null at any time). * The queue (might be set to null at any time).
*/ */
public static ReferenceQueue<Object> queue = new ReferenceQueue<Object>(); private static ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
/** /**
* The reference set. Must keep it, otherwise the references are garbage * The reference set. Must keep it, otherwise the references are garbage
* collected first and thus never enqueued. * collected first and thus never enqueued.
*/ */
public static HashSet<CloseWatcher> refs = New.hashSet(); private static Set<CloseWatcher> refs = createSet();
/** /**
* The stack trace of when the object was created. It is converted to a * The stack trace of when the object was created. It is converted to a
...@@ -48,6 +50,10 @@ public class CloseWatcher extends PhantomReference<Object> { ...@@ -48,6 +50,10 @@ public class CloseWatcher extends PhantomReference<Object> {
this.closeable = closeable; this.closeable = closeable;
} }
private static Set<CloseWatcher> createSet() {
return Collections.synchronizedSet(new HashSet<CloseWatcher>());
}
/** /**
* Check for an collected object. * Check for an collected object.
* *
...@@ -96,7 +102,7 @@ public class CloseWatcher extends PhantomReference<Object> { ...@@ -96,7 +102,7 @@ public class CloseWatcher extends PhantomReference<Object> {
cw.openStackTrace = s.toString(); cw.openStackTrace = s.toString();
} }
if (refs == null) { if (refs == null) {
refs = New.hashSet(); refs = createSet();
} }
refs.add(cw); refs.add(cw);
return cw; return cw;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论