8287384: Speed up jdk.test.lib.util.ForceGC

Reviewed-by: rriggs, bchristi, dfuchs, mchung
This commit is contained in:
Xue-Lei Andrew Fan 2022-05-31 17:44:54 +00:00
parent 6f6486e977
commit d5b6c7bde1

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -32,21 +32,23 @@ import java.util.function.BooleanSupplier;
* Utility class to invoke System.gc()
*/
public class ForceGC {
private final CountDownLatch cleanerInvoked = new CountDownLatch(1);
private final Cleaner cleaner = Cleaner.create();
private final static Cleaner cleaner = Cleaner.create();
private final CountDownLatch cleanerInvoked;
private Object o;
public ForceGC() {
this.o = new Object();
cleaner.register(o, () -> cleanerInvoked.countDown());
this.cleanerInvoked = new CountDownLatch(1);
cleaner.register(o, cleanerInvoked::countDown);
}
private void doit(int iter) {
private void doIt(int iter) {
try {
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 100; i++) {
System.gc();
System.out.println("doit() iter: " + iter + ", gc " + i);
if (cleanerInvoked.await(1L, TimeUnit.SECONDS)) {
System.out.println("doIt() iter: " + iter + ", gc " + i);
if (cleanerInvoked.await(100L, TimeUnit.MILLISECONDS)) {
return;
}
}
@ -62,18 +64,23 @@ public class ForceGC {
* @param s boolean supplier
* @return true if the {@code BooleanSupplier} returns true and false if
* the predefined waiting time elapsed before the count reaches zero.
* @throws InterruptedException if the current thread is interrupted while waiting
*/
public boolean await(BooleanSupplier s) {
o = null; // Keep reference to Object until now, to ensure the Cleaner
// doesn't count down the latch before await() is called.
for (int i = 0; i < 10; i++) {
if (s.getAsBoolean()) return true;
doit(i);
try { Thread.sleep(1000); } catch (InterruptedException e) {
for (int i = 0; i < 1000; i++) {
if (s.getAsBoolean()) {
return true;
}
doIt(i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new AssertionError("unexpected interrupted sleep", e);
}
}
return false;
}
}