Improving forceGC method on ActiveMQTestBase

This commit is contained in:
Clebert Suconic 2015-10-26 11:02:53 -04:00
parent 8c6ba443a3
commit 15b8b8138b
1 changed files with 7 additions and 7 deletions

View File

@ -525,14 +525,15 @@ public abstract class ActiveMQTestBase extends Assert {
public static void forceGC() { public static void forceGC() {
log.info("#test forceGC"); log.info("#test forceGC");
AtomicInteger finalized = new AtomicInteger(0); CountDownLatch finalized = new CountDownLatch(1);
WeakReference<DumbReference> dumbReference = new WeakReference<>(new DumbReference(finalized)); WeakReference<DumbReference> dumbReference = new WeakReference<>(new DumbReference(finalized));
// A loop that will wait GC, using the minimal time as possible // A loop that will wait GC, using the minimal time as possible
while (!(dumbReference.get() == null && finalized.get() == 1)) { while (!(dumbReference.get() == null && finalized.getCount() == 0)) {
System.gc(); System.gc();
System.runFinalization(); System.runFinalization();
try { try {
Thread.sleep(100); finalized.await(100, TimeUnit.MILLISECONDS);
} }
catch (InterruptedException e) { catch (InterruptedException e) {
} }
@ -2540,15 +2541,14 @@ public abstract class ActiveMQTestBase extends Assert {
protected static class DumbReference { protected static class DumbReference {
private AtomicInteger finalized; private CountDownLatch finalized;
public DumbReference(AtomicInteger finalized) { public DumbReference(CountDownLatch finalized) {
this.finalized = finalized; this.finalized = finalized;
} }
public void finalize() throws Throwable { public void finalize() throws Throwable {
System.out.println("FINALIZE"); finalized.countDown();
finalized.incrementAndGet();
super.finalize(); super.finalize();
} }
} }