@@ -108,6 +108,13 @@ protected void resetChildren() {
108
108
}
109
109
110
110
public void forgetChildren (NodesCache cache ) {
111
+ synchronized (this ) {
112
+ if (currentWorker != null ) {
113
+ currentWorker .interrupt ();
114
+ currentWorker = null ;
115
+ }
116
+ }
117
+
111
118
if (children != null && children .length > 0 ) {
112
119
for (HeapViewerNode node : children ) {
113
120
node .forgetChildren (cache );
@@ -142,6 +149,8 @@ private HeapViewerNode[] resolveChildren() {
142
149
}
143
150
144
151
152
+ private Thread currentWorker ;
153
+
145
154
protected HeapViewerNode [] computeChildren (final RootNode root ) {
146
155
// if (this == root) {
147
156
// System.err.println(">>> COMPUTING CHILDREN OF ROOT in " + Thread.currentThread());
@@ -151,11 +160,32 @@ protected HeapViewerNode[] computeChildren(final RootNode root) {
151
160
152
161
SwingWorker <HeapViewerNode [], HeapViewerNode []> worker = new SwingWorker <HeapViewerNode [], HeapViewerNode []>() {
153
162
protected HeapViewerNode [] doInBackground () throws Exception {
154
- return lazilyComputeChildren (root .getContext ().getFragment ().getHeap (), root .getViewID (), root .getViewFilter (), root .getDataTypes (), root .getSortOrders (), progress );
163
+ synchronized (HeapViewerNode .this ) {
164
+ if (currentWorker != null ) {
165
+ currentWorker .interrupt ();
166
+ // System.err.println(">>> Cancelling children of " + currentWorker);
167
+ }
168
+ currentWorker = Thread .currentThread ();
169
+ // System.err.println(">>> Computing children in " + Thread.currentThread() + "...");
170
+ }
171
+
172
+ HeapViewerNode [] ret = lazilyComputeChildren (root .getContext ().getFragment ().getHeap (), root .getViewID (), root .getViewFilter (), root .getDataTypes (), root .getSortOrders (), progress );
173
+
174
+ synchronized (HeapViewerNode .this ) {
175
+ if (currentWorker == Thread .currentThread ()) {
176
+ currentWorker = null ;
177
+ // System.err.println(">>> Computed children in " + Thread.currentThread());
178
+ }
179
+ if (Thread .interrupted () || currentWorker != null ) {
180
+ ret = null ;
181
+ // System.err.println(">>> Cancelled children in " + Thread.currentThread());
182
+ }
183
+ }
184
+
185
+ return ret ;
155
186
}
156
187
protected void done () {
157
188
if (children != null ) try {
158
- // TODO: children not valid in case the sorting changed during computation!
159
189
HeapViewerNode [] newChildren = get ();
160
190
// newChildren may be null, for example if the worker thread has been interrupted
161
191
if (newChildren != null ) {
0 commit comments