Thread OfPlatform Builder Naming

Create platform threads through Thread.ofPlatform with explicit names and an uncaught handler instead of leaving unnamed Thread-N workers behind.

JavaResilience

Code

snippet
public class WorkerFleet {

    public static void main(String[] args) throws InterruptedException {
        // Thread.ofPlatform() 返回 builder:命名、守护属性、异常处理一次配齐
        Thread first = Thread.ofPlatform()
                .name("inventory-sync")
                .daemon(false)
                .uncaughtExceptionHandler((t, e) ->
                        System.out.println(t.getName() + " failed: " + e.getMessage()))
                .start(() -> {
                    for (int i = 0; i < 3; i++) {
                        System.out.println(Thread.currentThread().getName() + " tick " + i);
                    }
                });

        // 未命名线程只会拿到 Thread-N 这样的自动编号
        Thread anonymous = Thread.ofPlatform().start(
                () -> System.out.println("called: " + Thread.currentThread().getName()));

        first.join();
        anonymous.join();
        System.out.println("first named: " + first.getName());
    }
}

Inside the snippet

Thread OfPlatform Builder Naming comes down to the class WorkerFleet.

Repetition is confined to for (int i = 0; i < 3; i++) — the rest of the file is straight-line.

Putting it to use

The demo output is labeled (called:, first named:), so every printed line maps back to its originating operation.

Core lines

Thread anonymous = Thread.ofPlatform().start(
        () -> System.out.println("called: " + Thread.currentThread().getName()));

Interactive Tools for This Topic

FAQ

What does Thread OfPlatform Builder Naming print when run?
Running the file directly is enough — expect labeled output lines such as `called:`, `first named:`.