thunkware / virtual-threads-bridge

Use Java21's Virtual Threads API in Java 8+
MIT License
8 stars 3 forks source link

Added ThreadNameCallable and ThreadNameRunnable #29

Closed dhoard closed 2 months ago

dhoard commented 2 months ago

Summary

Added ThreadNameCallable and ThreadNameRunnable for scenarios when code needs to set the thread name during the execution of the Callable or Runnable, for correlation/logging purposes, but wants to revert the thread name to the original version after execution.

Example Scenario

Example Code

String threadName = Thread.currentThread().getName();
ExecutorService executorService = ExecutorTool.newVirtualThreadPerTaskExecutor();

executorService.submit(
    new NamedRunnable(
        threadName + "/customRunnable1", new CustomRunnable1());

executorService.submit(
    new NamedRunnable(
        threadName + "/customRunnable2", new CustomRunnable2());