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