GeezerVison is showing github.com — Make Context.doDispose() less deadlock-prone · scijava/scijava-common@5dba100
File tree
Expand file treeCollapse file tree
Open diff view settings
src/main/java/org/scijava
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/main/java/org/scijava/Context.java +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -113,7 +113,7 @@ public class Context implements Disposable, AutoCloseable { |
113 | 113 | * False if the context is currently active; true if the context |
114 | 114 | * has already been disposed, or is in the process of being disposed. |
115 | 115 | */ |
116 | | - private boolean disposed; |
| 116 | + private volatile boolean disposed; |
117 | 117 |
|
118 | 118 | /** |
119 | 119 | * Creates a new SciJava application context with all available services. |
@@ -456,7 +456,7 @@ public boolean isInjectable(final Class<?> type) { |
456 | 456 |
|
457 | 457 | @Override |
458 | 458 | public void dispose() { |
459 | | - doDispose(true); |
| 459 | + if (!disposed) doDispose(true); |
460 | 460 | } |
461 | 461 |
|
462 | 462 | // -- AutoCloseable methods -- |
@@ -609,9 +609,11 @@ private String createMissingServiceMessage( |
609 | 609 | return msg.toString(); |
610 | 610 | } |
611 | 611 |
|
612 | | - private synchronized void doDispose(final boolean announce) { |
613 | | - if (disposed) return; |
614 | | - disposed = true; |
| 612 | + private void doDispose(final boolean announce) { |
| 613 | + synchronized (this) { |
| 614 | + if (disposed) return; |
| 615 | + disposed = true; |
| 616 | + } |
615 | 617 | CONTEXTS.remove(this); |
616 | 618 | if (announce) { |
617 | 619 | final EventService eventService = getService(EventService.class); |
|