ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/WeakHashMap/GCDuringIteration.java
(Generate patch)

Comparing jsr166/src/test/jtreg/util/WeakHashMap/GCDuringIteration.java (file contents):
Revision 1.11 by jsr166, Mon Jan 8 03:12:03 2018 UTC vs.
Revision 1.12 by jsr166, Mon May 7 23:44:42 2018 UTC

# Line 34 | Line 34
34  
35   import jdk.test.lib.RandomFactory;
36  
37 + import java.lang.ref.ReferenceQueue;
38   import java.lang.ref.WeakReference;
39   import java.util.Arrays;
40   import java.util.Iterator;
# Line 44 | Line 45 | import java.util.WeakHashMap;
45   import java.util.concurrent.CountDownLatch;
46   import java.util.function.BooleanSupplier;
47  
48 < import static java.util.concurrent.TimeUnit.SECONDS;
48 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
49  
50   public class GCDuringIteration {
51  
52      /** No guarantees, but effective in practice. */
53      static void forceFullGc() {
54 +        long timeoutMillis = 1000L;
55          CountDownLatch finalizeDone = new CountDownLatch(1);
56 <        WeakReference<?> ref = new WeakReference<Object>(new Object() {
57 <            protected void finalize() { finalizeDone.countDown(); }});
56 >        ReferenceQueue<Object> queue = new ReferenceQueue<>();
57 >        WeakReference<Object> ref = new WeakReference<>(
58 >            new Object() {
59 >                protected void finalize() { finalizeDone.countDown(); }},
60 >            queue);
61          try {
62 <            for (int i = 0; i < 10; i++) {
62 >            for (int tries = 3; tries--> 0; ) {
63                  System.gc();
64 <                if (finalizeDone.await(1L, SECONDS) && ref.get() == null) {
64 >                if (finalizeDone.await(timeoutMillis, MILLISECONDS)
65 >                    && queue.remove(timeoutMillis) != null
66 >                    && ref.get() == null) {
67                      System.runFinalization(); // try to pick up stragglers
68                      return;
69                  }
70 +                timeoutMillis *= 4;
71              }
72          } catch (InterruptedException unexpected) {
73              throw new AssertionError("unexpected InterruptedException");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines