From 0af3ba4f602438aab082efe67951335aae6b43c9 Mon Sep 17 00:00:00 2001 From: Ashley <73482956+ascopes@users.noreply.github.com> Date: Fri, 28 Oct 2022 08:36:35 +0100 Subject: [PATCH 1/2] Run GC hooks asynchronously --- .../ascopes/jct/utils/GarbageDisposal.java | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/GarbageDisposal.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/GarbageDisposal.java index c747744db..ad09de7ac 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/GarbageDisposal.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/GarbageDisposal.java @@ -62,10 +62,10 @@ public static void onPhantom(Object ref, String name, AutoCloseable hook) { } private static Cleaner newCleaner() { + // This thread factory has exactly 1 thread created from it. return Cleaner.create(runnable -> { var thread = new Thread(runnable); - thread.setDaemon(false); - thread.setName(runnable.toString()); + thread.setName("JCT GC hook caller"); thread.setPriority(Thread.MIN_PRIORITY); return thread; }); @@ -87,25 +87,35 @@ private CloseableDelegate(String name, AutoCloseable closeable) { @Override public String toString() { - return "JCT GC hook for " + name + " (" + closeable + ")"; + return "JCT GC hook for " + + name + + " at " + + Integer.toHexString(System.identityHashCode(closeable)) + + " (delegate at " + + Integer.toHexString(System.identityHashCode(this)) + + ")"; } @Override public void run() { - try { - LOGGER.trace("Closing {} ({})", name, closeable); - // TODO: should I delegate this to a separate thread since we use a custom thread - // factory now? - closeable.close(); - } catch (Exception ex) { - var thread = Thread.currentThread(); - LOGGER.error( - "Failed to close resource on thread {} [{}]", - thread.getId(), - thread.getName(), - ex - ); - } + var thread = new Thread(() -> { + try { + LOGGER.trace("Closing {} ({})", name, closeable); + closeable.close(); + } catch (Exception ex) { + var thread = Thread.currentThread(); + LOGGER.error( + "Failed to close resource on thread {} [{}]", + thread.getId(), + thread.getName(), + ex + ); + } + }); + + thread.setName(toString()); + thread.setPriority(Thread.MAX_PRIORITY); + thread.start(); } } } From 7696329292aa7cd3fd8d1186daa0809e34e00bfe Mon Sep 17 00:00:00 2001 From: Ashley <73482956+ascopes@users.noreply.github.com> Date: Fri, 28 Oct 2022 08:40:18 +0100 Subject: [PATCH 2/2] Update GarbageDisposal.java --- .../java/io/github/ascopes/jct/utils/GarbageDisposal.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/GarbageDisposal.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/GarbageDisposal.java index ad09de7ac..66e099e34 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/GarbageDisposal.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/GarbageDisposal.java @@ -103,11 +103,11 @@ public void run() { LOGGER.trace("Closing {} ({})", name, closeable); closeable.close(); } catch (Exception ex) { - var thread = Thread.currentThread(); - LOGGER.error( + var thisThread = Thread.currentThread(); + LOGGER.error( "Failed to close resource on thread {} [{}]", - thread.getId(), - thread.getName(), + thisThread.getId(), + thisThread.getName(), ex ); }