Interface Thread.Builder
- All Known Subinterfaces:
Thread.Builder.OfPlatform,Thread.Builder.OfVirtual
- Enclosing class:
Thread
public static sealed interfaceThread.BuilderpermitsThread.Builder.OfPlatform,Thread.Builder.OfVirtual
A builder for
Thread andThreadFactory objects.Builder defines methods to setThread properties such as the threadname. This includes properties that would otherwise beinherited. Once set, aThread orThreadFactory is created with the following methods:
- Theunstarted method creates a newunstarted
Threadto run a task. TheThread'sstartmethod must be invoked to schedule the thread to execute. - Thestart method creates a new
Threadto run a task and schedules the thread to execute. - Thefactory method creates a
ThreadFactory.
AThread.Builder is not thread safe. TheThreadFactory returned by the builder'sfactory() method is thread safe.
Unless otherwise specified, passing a null argument to a method in this interface causes aNullPointerException to be thrown.
- Since:
- 21
- See Also:
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA builder for creating a platformThreadorThreadFactorythat creates platform threads.static interfaceA builder for creating a virtualThreadorThreadFactorythat creates virtual threads.Method Summary
Modifier and TypeMethodDescriptionfactory()Returns aThreadFactoryto create threads from the current state of the builder.inheritInheritableThreadLocals(boolean inherit) Sets whether the thread inherits the initial values ofinheritable-thread-local variables from the constructing thread.Sets the thread name.Sets the thread name to be the concatenation of a string prefix and the string representation of a counter value.Creates a newThreadfrom the current state of the builder and schedules it to execute.Sets the uncaught exception handler.Creates a newThreadfrom the current state of the builder to run the given task.
Method Details
name
Sets the thread name.- Parameters:
name- thread name- Returns:
- this builder
name
Sets the thread name to be the concatenation of a string prefix and the string representation of a counter value. The counter's initial value isstart. It is incremented after aThreadis created with this builder so that the next thread is named with the new counter value. AThreadFactorycreated with this builder is seeded with the current value of the counter. TheThreadFactoryincrements its copy of the counter afternewThreadis used to create aThread.- API Note:
- The following example creates a builder that is invoked twice to start two threads named "
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" - Parameters:
prefix- thread name prefixstart- the starting value of the counter- Returns:
- this builder
- Throws:
IllegalArgumentException- if start is negative
inheritInheritableThreadLocals
Sets whether the thread inherits the initial values ofinheritable-thread-local variables from the constructing thread. The default is to inherit.- Parameters:
inherit-trueto inherit,falseto not inherit- Returns:
- this builder
uncaughtExceptionHandler
Sets the uncaught exception handler.- Parameters:
ueh- uncaught exception handler- Returns:
- this builder
unstarted
Creates a newThreadfrom the current state of the builder to run the given task. TheThread'sstartmethod must be invoked to schedule the thread to execute.- Parameters:
task- the object to run when the thread executes- Returns:
- a new unstarted Thread
- See Also:
start
Creates a newThreadfrom the current state of the builder and schedules it to execute.- Parameters:
task- the object to run when the thread executes- Returns:
- a new started Thread
- See Also:
factory
ThreadFactory factory()Returns aThreadFactoryto create threads from the current state of the builder. The returned thread factory is safe for use by multiple concurrent threads.- Returns:
- a thread factory to create threads