Skip to content

Commit ab380b7

Browse files
committed
Use RequestProcessor instead of Thread to perform atomic computations.
1 parent 53de8ce commit ab380b7

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

visualvm/heapviewer/src/org/graalvm/visualvm/heapviewer/utils/HeapOperations.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.graalvm.visualvm.lib.jfluid.heap.JavaClass;
3636
import org.netbeans.api.progress.ProgressHandle;
3737
import org.openide.util.NbBundle;
38+
import org.openide.util.RequestProcessor;
3839

3940
/**
4041
*
@@ -82,10 +83,10 @@ public static void initializeRetainedSizes(Heap heap) throws InterruptedExceptio
8283
// --- References ----------------------------------------------------------
8384

8485
private static boolean referencesInitialized;
85-
private static volatile Thread referencesComputer;
86+
private static volatile RequestProcessor.Task referencesComputer;
8687

8788
private void initializeReferencesImpl(Heap heap) throws InterruptedException {
88-
Thread _referencesComputer;
89+
RequestProcessor.Task _referencesComputer;
8990

9091
synchronized (this) {
9192
if (referencesInitialized) return;
@@ -114,29 +115,28 @@ public void run() {
114115
}
115116
}
116117
};
117-
referencesComputer = new Thread(workerR, "References Computer"); // NO18N
118-
_referencesComputer = referencesComputer; // NOTE: must be assigned before starting the thread which eventually nulls the referencesComputer!
119-
referencesComputer.start();
118+
referencesComputer = new RequestProcessor("References Computer").post(workerR); // NO18N
119+
_referencesComputer = referencesComputer;
120120
} else {
121121
_referencesComputer = referencesComputer;
122122
}
123123
}
124124

125125
assert !SwingUtilities.isEventDispatchThread();
126126

127-
_referencesComputer.join();
127+
_referencesComputer.waitFinished(0);
128128
}
129129

130130

131131
// --- GC Roots ------------------------------------------------------------
132132

133133
private static boolean gcrootsInitialized;
134-
private static volatile Thread gcrootsComputer;
134+
private static volatile RequestProcessor.Task gcrootsComputer;
135135

136136
private void initializeGCRootsImpl(Heap heap) throws InterruptedException {
137137
initializeReferencesImpl(heap);
138138

139-
Thread _gcrootsComputer;
139+
RequestProcessor.Task _gcrootsComputer;
140140

141141
synchronized (this) {
142142
if (gcrootsInitialized) return;
@@ -165,29 +165,27 @@ public void run() {
165165
}
166166
}
167167
};
168-
gcrootsComputer = new Thread(workerR, "GC Roots Computer"); // NO18N
169-
_gcrootsComputer = gcrootsComputer; // NOTE: must be assigned before starting the thread which eventually nulls the gcrootsComputer!
170-
gcrootsComputer.start();
168+
gcrootsComputer = new RequestProcessor("GC Roots Computer").post(workerR); // NO18N
169+
_gcrootsComputer = gcrootsComputer;
171170
} else {
172171
_gcrootsComputer = gcrootsComputer;
173172
}
174173
}
175174

176175
assert !SwingUtilities.isEventDispatchThread();
177176

178-
_gcrootsComputer.join();
177+
_gcrootsComputer.waitFinished(0);
179178
}
180179

181-
182180
// --- GC Roots ------------------------------------------------------------
183181

184182
private static boolean retainedInitialized;
185-
private static volatile Thread retainedComputer;
183+
private static volatile RequestProcessor.Task retainedComputer;
186184

187185
private void initializeRetainedSizesImpl(Heap heap) throws InterruptedException {
188186
initializeGCRootsImpl(heap);
189187

190-
Thread _retainedComputer;
188+
RequestProcessor.Task _retainedComputer;
191189

192190
synchronized (this) {
193191
if (retainedInitialized) return;
@@ -216,17 +214,16 @@ public void run() {
216214
}
217215
}
218216
};
219-
retainedComputer = new Thread(workerR, "Retained Sizes Computer"); // NO18N
220-
_retainedComputer = retainedComputer; // NOTE: must be assigned before starting the thread which eventually nulls the retainedComputer!
221-
retainedComputer.start();
217+
retainedComputer = new RequestProcessor("Retained Sizes Computer").post(workerR); // NO18N
218+
_retainedComputer = retainedComputer;
222219
} else {
223220
_retainedComputer = retainedComputer;
224221
}
225222
}
226223

227224
assert !SwingUtilities.isEventDispatchThread();
228225

229-
_retainedComputer.join();
226+
_retainedComputer.waitFinished(0);
230227
}
231228

232229
}

0 commit comments

Comments
 (0)