Skip to content

Commit 51b85a1

Browse files
author
Brent Christian
committedSep 11, 2024
8339687: Rearrange reachabilityFence()s in jdk.test.lib.util.ForceGC
Reviewed-by: dholmes, smarks, kbarrett
1 parent d9fdf69 commit 51b85a1

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed
 

‎test/lib/jdk/test/lib/util/ForceGC.java

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* 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) {
7878
ReferenceQueue<Object> queue = new ReferenceQueue<>();
7979
Object obj = new Object();
8080
PhantomReference<Object> ref = new PhantomReference<>(obj, queue);
81-
obj = null;
82-
Reference.reachabilityFence(obj);
83-
Reference.reachabilityFence(ref);
81+
try {
82+
obj = null;
8483

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+
}
9089

91-
System.gc();
90+
System.gc();
9291

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+
}
101101
}
102+
} finally {
103+
Reference.reachabilityFence(ref);
102104
}
103-
104105
return booleanSupplier.getAsBoolean();
105106
}
106107
}

0 commit comments

Comments
 (0)
Please sign in to comment.