Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions guava-tests/test/com/google/common/util/concurrent/TestThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,27 @@ public TestThread(L lockLikeObject, String threadName) {
*/
@Override
public void tearDown() throws Exception {
// First, try to interrupt the thread. This allows interruptible threads (like those in
// InterruptibleMonitorTest) to clean up gracefully. For uninterruptible threads, this
// will have no effect, but we try stop() next as a fallback for older JDKs.
interrupt();
try {
Thread.class.getMethod("stop").invoke(this);
join();
} catch (ReflectiveOperationException e) {
// stop() threw or did not exist. Don't join() the thread, which might hang forever.
// Give the thread a chance to respond to the interrupt before trying stop().
join(DUE_DILIGENCE_MILLIS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

if (isAlive()) {
// Thread didn't respond to interrupt. Try stop() for older JDKs (pre-Java 20).
// On Java 20+, stop() throws UnsupportedOperationException, so the thread will
// remain alive. This is unavoidable for uninterruptible threads.
try {
Thread.class.getMethod("stop").invoke(this);
join();
} catch (ReflectiveOperationException e) {
// stop() threw or did not exist. Don't join() the thread, which might hang forever.
}
}

if (uncaughtThrowable != null) {
Expand Down