Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import java.util.regex.Pattern;

/** This {@link ExecutorService} wrapper renames threads for the duration of task execution. */
final class RenamingExecutorService extends AbstractExecutorService {
Expand Down Expand Up @@ -76,6 +77,8 @@ public String toString() {
}

final class RenamingRunnable implements Runnable {
private static final String ORIGINAL_NAME_DELIMITER = "-was-";
private static final Pattern ORIGINAL_NAME_PATTERN = Pattern.compile(ORIGINAL_NAME_DELIMITER);

private final Runnable command;

Expand All @@ -87,13 +90,16 @@ final class RenamingRunnable implements Runnable {
public void run() {
final Thread currentThread = Thread.currentThread();
final String originalName = currentThread.getName();
ThreadNames.setThreadName(currentThread, nameSupplier.get());
final String newName = nameSupplier.get();
ThreadNames.setThreadName(currentThread, newName);
try {
command.run();
} catch (Throwable t) {
handler.uncaughtException(currentThread, t);
} finally {
ThreadNames.setThreadName(currentThread, originalName);
ThreadNames.setThreadName(
currentThread,
ORIGINAL_NAME_PATTERN.split(originalName, 2)[0] + ORIGINAL_NAME_DELIMITER + newName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void testThreadName() {

assertThat(Thread.currentThread().getName())
.as("Thread names should not be tainted")
.isEqualTo(originalThreadName);
.isEqualTo(originalThreadName + "-was-foo-0");
}

@Test
Expand Down