Unverified 提交 affb7326 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1140 from katzyn/closer

Fix strange random failure in TestClearReferences
...@@ -16,15 +16,18 @@ class OnExitDatabaseCloser extends Thread { ...@@ -16,15 +16,18 @@ class OnExitDatabaseCloser extends Thread {
private static final WeakHashMap<Database, Void> DATABASES = new WeakHashMap<>(); private static final WeakHashMap<Database, Void> DATABASES = new WeakHashMap<>();
private static OnExitDatabaseCloser INSTANCE; private static final Thread INSTANCE = new OnExitDatabaseCloser();
private static boolean registered;
static synchronized void register(Database db) { static synchronized void register(Database db) {
DATABASES.put(db, null); DATABASES.put(db, null);
if (INSTANCE == null) { if (!registered) {
// Mark as registered unconditionally to avoid further attempts to register a
// shutdown hook in case of exception.
registered = true;
try { try {
// Assign INSTANCE unconditionally to avoid further attempts to register a Runtime.getRuntime().addShutdownHook(INSTANCE);
// shutdown hook in case of exception.
Runtime.getRuntime().addShutdownHook(INSTANCE = new OnExitDatabaseCloser());
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// shutdown in progress - just don't register the handler // shutdown in progress - just don't register the handler
// (maybe an application wants to write something into a // (maybe an application wants to write something into a
...@@ -39,7 +42,7 @@ class OnExitDatabaseCloser extends Thread { ...@@ -39,7 +42,7 @@ class OnExitDatabaseCloser extends Thread {
static synchronized void unregister(Database db) { static synchronized void unregister(Database db) {
DATABASES.remove(db); DATABASES.remove(db);
if (DATABASES.isEmpty() && INSTANCE != null) { if (DATABASES.isEmpty() && registered) {
try { try {
Runtime.getRuntime().removeShutdownHook(INSTANCE); Runtime.getRuntime().removeShutdownHook(INSTANCE);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
...@@ -47,7 +50,7 @@ class OnExitDatabaseCloser extends Thread { ...@@ -47,7 +50,7 @@ class OnExitDatabaseCloser extends Thread {
} catch (SecurityException e) { } catch (SecurityException e) {
// applets may not do that - ignore // applets may not do that - ignore
} }
INSTANCE = null; registered = false;
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论