|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
@@ -78,29 +78,30 @@ public static boolean waitFor(BooleanSupplier booleanSupplier, long timeout) {
|
78 | 78 | ReferenceQueue<Object> queue = new ReferenceQueue<>();
|
79 | 79 | Object obj = new Object();
|
80 | 80 | PhantomReference<Object> ref = new PhantomReference<>(obj, queue);
|
81 |
| - obj = null; |
82 |
| - Reference.reachabilityFence(obj); |
83 |
| - Reference.reachabilityFence(ref); |
| 81 | + try { |
| 82 | + obj = null; |
84 | 83 |
|
85 |
| - int retries = (int)(timeout / 200); |
86 |
| - for (; retries >= 0; retries--) { |
87 |
| - if (booleanSupplier.getAsBoolean()) { |
88 |
| - return true; |
89 |
| - } |
| 84 | + int retries = (int) (timeout / 200); |
| 85 | + for (; retries >= 0; retries--) { |
| 86 | + if (booleanSupplier.getAsBoolean()) { |
| 87 | + return true; |
| 88 | + } |
90 | 89 |
|
91 |
| - System.gc(); |
| 90 | + System.gc(); |
92 | 91 |
|
93 |
| - try { |
94 |
| - // The remove() will always block for the specified milliseconds |
95 |
| - // if the reference has already been removed from the queue. |
96 |
| - // But it is fine. For most cases, the 1st GC is sufficient |
97 |
| - // to trigger and complete the cleanup. |
98 |
| - queue.remove(200L); |
99 |
| - } catch (InterruptedException ie) { |
100 |
| - // ignore, the loop will try again |
| 92 | + try { |
| 93 | + // The remove() will always block for the specified milliseconds |
| 94 | + // if the reference has already been removed from the queue. |
| 95 | + // But it is fine. For most cases, the 1st GC is sufficient |
| 96 | + // to trigger and complete the cleanup. |
| 97 | + queue.remove(200L); |
| 98 | + } catch (InterruptedException ie) { |
| 99 | + // ignore, the loop will try again |
| 100 | + } |
101 | 101 | }
|
| 102 | + } finally { |
| 103 | + Reference.reachabilityFence(ref); |
102 | 104 | }
|
103 |
| - |
104 | 105 | return booleanSupplier.getAsBoolean();
|
105 | 106 | }
|
106 | 107 | }
|
|
0 commit comments