Thread.Builder.OfPlatform
, Thread.Builder.OfVirtual
Thread
public static sealed interface Thread.Builder permits Thread.Builder.OfPlatform, Thread.Builder.OfVirtual
Thread
and ThreadFactory
objects. Builder
defines methods to set Thread
properties such as the thread name
. This includes properties that would otherwise be inherited. Once set, a Thread
or ThreadFactory
is created with the following methods:
Thread
to run a task. The Thread
's start
method must be invoked to schedule the thread to execute.
Thread
to run a task and schedules the thread to execute. ThreadFactory
. A Thread.Builder
is not thread safe. The ThreadFactory
returned by the builder's factory()
method is thread safe.
Unless otherwise specified, passing a null argument to a method in this interface causes a NullPointerException
to be thrown.
Modifier and Type | Interface | Description |
---|---|---|
static interface |
Thread.Builder.OfPlatform |
A builder for creating a platform Thread or ThreadFactory that creates platform threads. |
static interface |
Thread.Builder.OfVirtual |
A builder for creating a virtual Thread or ThreadFactory that creates virtual threads. |
Modifier and Type | Method | Description |
---|---|---|
ThreadFactory |
factory() |
Returns a ThreadFactory to create threads from the current state of the builder. |
Thread.Builder |
inheritInheritableThreadLocals |
Sets whether the thread inherits the initial values of inheritable-thread-local variables from the constructing thread. |
Thread.Builder |
name |
Sets the thread name. |
Thread.Builder |
name |
Sets the thread name to be the concatenation of a string prefix and the string representation of a counter value. |
Thread |
start |
Creates a new Thread from the current state of the builder and schedules it to execute. |
Thread.Builder |
uncaughtExceptionHandler |
Sets the uncaught exception handler. |
Thread |
unstarted |
Creates a new Thread from the current state of the builder to run the given task. |
Thread.Builder name(String name)
name
- thread nameThread.Builder name(String prefix, long start)
start
. It is incremented after a Thread
is created with this builder so that the next thread is named with the new counter value. A ThreadFactory
created with this builder is seeded with the current value of the counter. The
ThreadFactory
increments its copy of the counter after newThread
is used to create a Thread
.worker-0
" and "worker-1
". Thread.Builder builder = Thread.ofPlatform().name("worker-", 0);
Thread t1 = builder.start(task1); // name "worker-0"
Thread t2 = builder.start(task2); // name "worker-1"
prefix
- thread name prefixstart
- the starting value of the counterIllegalArgumentException
- if start is negativeThread.Builder inheritInheritableThreadLocals(boolean inherit)
inherit
- true
to inherit, false
to not inheritThread.Builder uncaughtExceptionHandler(Thread.UncaughtExceptionHandler ueh)
ueh
- uncaught exception handlerThread unstarted(Runnable task)
Thread
from the current state of the builder to run the given task. The Thread
's start
method must be invoked to schedule the thread to execute.task
- the object to run when the thread executesSecurityException
- if denied by the security manager (See Interaction with security manager when creating platform threads)Thread start(Runnable task)
Thread
from the current state of the builder and schedules it to execute.task
- the object to run when the thread executesSecurityException
- if denied by the security manager (See Interaction with security manager when creating platform threads)ThreadFactory factory()
ThreadFactory
to create threads from the current state of the builder. The returned thread factory is safe for use by multiple concurrent threads.
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.Builder.html