Skip to content

Commit 8bfa79d

Browse files
committed
Tweak thread names.
Especially the hook virtual threads, but also the mongo change receiver thread and some test threads.
1 parent e5c81b9 commit 8bfa79d

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

bosk-core/src/main/java/works/bosk/Bosk.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,13 @@ public class Bosk<R extends StateTreeNode> implements BoskInfo<R> {
8787
private final ThreadLocal<R> rootSnapshot = new ThreadLocal<>();
8888
private final HookRegistrar hookRegistrar;
8989
private final Queue<HookRegistration<?>> hooks = new ConcurrentLinkedQueue<>();
90-
private final ExecutorService hookExecutor = Executors.newVirtualThreadPerTaskExecutor();
9190
private final PathCompiler pathCompiler;
9291

92+
private final Thread.Builder hookThreadBuilder = Thread
93+
.ofVirtual()
94+
.name("bosk-hook-", 1);
95+
private final ExecutorService hookExecutor = Executors.newThreadPerTaskExecutor(hookThreadBuilder::unstarted);
96+
9397
// Mutable state
9498
private volatile R currentRoot;
9599

bosk-core/src/test/java/works/bosk/HooksTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
import java.util.function.Consumer;
1111
import org.junit.jupiter.api.BeforeEach;
1212
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.TestInfo;
1314
import org.junit.jupiter.params.ParameterizedTest;
1415
import org.junit.jupiter.params.provider.EnumSource;
1516
import org.junit.jupiter.params.provider.ValueSource;
1617
import works.bosk.annotations.Hook;
1718
import works.bosk.annotations.ReferencePath;
1819
import works.bosk.exceptions.InvalidTypeException;
1920

21+
import static java.lang.Thread.currentThread;
2022
import static java.util.Arrays.asList;
2123
import static java.util.Collections.emptyList;
2224
import static java.util.Collections.singletonList;
@@ -64,6 +66,11 @@ void setupBosk() throws InvalidTypeException {
6466
recorder = new HookRecorder();
6567
}
6668

69+
@BeforeEach
70+
void setThreadName(TestInfo testInfo) {
71+
currentThread().setName("test: " + testInfo.getDisplayName());
72+
}
73+
6774
@ParameterizedTest
6875
@EnumSource(Variant.class)
6976
void beforeRegistration_noHooks(Variant variant) {

bosk-mongo/src/main/java/works/bosk/drivers/mongo/internal/ChangeReceiver.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class ChangeReceiver implements Closeable {
5555
private final ChangeListener listener;
5656
private final MongoDriverSettings settings;
5757
private final MongoCollection<BsonDocument> collection;
58-
private final ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);
58+
private final Thread.Builder threadBuilder = Thread.ofPlatform().name("bosk-mongo-change-receiver");
59+
private final ScheduledExecutorService ex = Executors.newScheduledThreadPool(1, threadBuilder::unstarted);
5960
private final Exception creationPoint;
6061
private volatile @Nullable Thread thread = null;
6162
private volatile boolean isClosed = false;
@@ -124,8 +125,6 @@ public void close() {
124125
* around the loop.
125126
*/
126127
private void connectionLoop() {
127-
String oldThreadName = currentThread().getName();
128-
currentThread().setName(getClass().getSimpleName() + " [" + boskName + "]");
129128
try (MDCScope _ = setupMDC(boskName, boskID)) {
130129
LOGGER.debug("Starting connectionLoop task");
131130
try {
@@ -217,7 +216,6 @@ private void connectionLoop() {
217216
}
218217
} finally {
219218
LOGGER.debug("Ending connectionLoop task; isClosed={}", isClosed);
220-
currentThread().setName(oldThreadName);
221219
thread = null;
222220
}
223221
} catch (RuntimeException e) {

bosk-testing/src/main/java/works/bosk/testing/drivers/AbstractDriverTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ public abstract class AbstractDriverTest {
3636

3737
@BeforeEach
3838
void logStart(TestInfo testInfo) {
39-
logTest("/=== Start", testInfo);
4039
oldThreadName = Thread.currentThread().getName();
41-
Thread.currentThread().setName(testInfo.getDisplayName());
40+
String newThreadName = "test: " + testInfo.getDisplayName();
41+
Thread.currentThread().setName(newThreadName);
42+
logTest("/=== Start", testInfo);
43+
LOGGER.debug("Old thread name was {}", oldThreadName);
4244
}
4345

4446
@AfterEach
4547
void logDone(TestInfo testInfo) {
46-
Thread.currentThread().setName(oldThreadName);
4748
logTest("\\=== Done", testInfo);
49+
Thread.currentThread().setName(oldThreadName);
4850
}
4951

5052
private static void logTest(String verb, TestInfo testInfo) {

0 commit comments

Comments
 (0)