From 15b8b8138bcb3148c5fd64823d2c2b5170a661cc Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Mon, 26 Oct 2015 11:02:53 -0400 Subject: [PATCH] Improving forceGC method on ActiveMQTestBase --- .../artemis/tests/util/ActiveMQTestBase.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java index 7e358a91ba..5556c4e510 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java @@ -525,14 +525,15 @@ public abstract class ActiveMQTestBase extends Assert { public static void forceGC() { log.info("#test forceGC"); - AtomicInteger finalized = new AtomicInteger(0); + CountDownLatch finalized = new CountDownLatch(1); WeakReference dumbReference = new WeakReference<>(new DumbReference(finalized)); + // 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.runFinalization(); try { - Thread.sleep(100); + finalized.await(100, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { } @@ -2540,15 +2541,14 @@ public abstract class ActiveMQTestBase extends Assert { protected static class DumbReference { - private AtomicInteger finalized; + private CountDownLatch finalized; - public DumbReference(AtomicInteger finalized) { + public DumbReference(CountDownLatch finalized) { this.finalized = finalized; } public void finalize() throws Throwable { - System.out.println("FINALIZE"); - finalized.incrementAndGet(); + finalized.countDown(); super.finalize(); } }