27 |
|
* @run main IteratorMicroBenchmark iterations=1 size=8 warmup=0 |
28 |
|
*/ |
29 |
|
|
30 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
31 |
|
import static java.util.stream.Collectors.summingInt; |
32 |
|
import static java.util.stream.Collectors.toCollection; |
33 |
|
|
34 |
+ |
import java.lang.ref.ReferenceQueue; |
35 |
|
import java.lang.ref.WeakReference; |
36 |
|
import java.util.ArrayDeque; |
37 |
|
import java.util.ArrayList; |
56 |
|
import java.util.concurrent.PriorityBlockingQueue; |
57 |
|
import java.util.concurrent.CountDownLatch; |
58 |
|
import java.util.concurrent.ThreadLocalRandom; |
57 |
– |
import java.util.concurrent.TimeUnit; |
59 |
|
import java.util.concurrent.atomic.LongAdder; |
60 |
|
import java.util.function.UnaryOperator; |
61 |
|
import java.util.regex.Pattern; |
110 |
|
|
111 |
|
/** No guarantees, but effective in practice. */ |
112 |
|
static void forceFullGc() { |
113 |
< |
CountDownLatch finalizeDone = new CountDownLatch(1); |
114 |
< |
WeakReference<?> ref = new WeakReference<Object>(new Object() { |
115 |
< |
@SuppressWarnings("deprecation") |
116 |
< |
protected void finalize() { finalizeDone.countDown(); }}); |
113 |
> |
long timeoutMillis = 1000L; |
114 |
> |
CountDownLatch finalized = new CountDownLatch(1); |
115 |
> |
ReferenceQueue<Object> queue = new ReferenceQueue<>(); |
116 |
> |
WeakReference<Object> ref = new WeakReference<>( |
117 |
> |
new Object() { protected void finalize() { finalized.countDown(); }}, |
118 |
> |
queue); |
119 |
|
try { |
120 |
< |
for (int i = 0; i < 10; i++) { |
120 |
> |
for (int tries = 3; tries--> 0; ) { |
121 |
|
System.gc(); |
122 |
< |
if (finalizeDone.await(1L, TimeUnit.SECONDS) && ref.get() == null) { |
122 |
> |
if (finalized.await(timeoutMillis, MILLISECONDS) |
123 |
> |
&& queue.remove(timeoutMillis) != null |
124 |
> |
&& ref.get() == null) { |
125 |
|
System.runFinalization(); // try to pick up stragglers |
126 |
|
return; |
127 |
|
} |
128 |
+ |
timeoutMillis *= 4; |
129 |
|
} |
130 |
|
} catch (InterruptedException unexpected) { |
131 |
|
throw new AssertionError("unexpected InterruptedException"); |