Skip to content

Commit 69c0316

Browse files
klueverGoogle Java Core Libraries
authored andcommitted
Add Java 21 advice to each method of ThreadFactoryBuilder.
RELNOTES=n/a PiperOrigin-RevId: 792835335
1 parent ab2f614 commit 69c0316

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

android/guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,25 @@ public final class ThreadFactoryBuilder {
5959
private @Nullable UncaughtExceptionHandler uncaughtExceptionHandler = null;
6060
private @Nullable ThreadFactory backingThreadFactory = null;
6161

62-
/** Creates a new {@link ThreadFactory} builder. */
62+
/**
63+
* Creates a new {@link ThreadFactory} builder.
64+
*
65+
* <p><b>Java 21+ users:</b> use {@link Thread#ofPlatform()} instead, translating other calls on
66+
* the builder as documented on each method (except for the rarely used {@link #setThreadFactory},
67+
* which does not have an equivalent).
68+
*/
6369
public ThreadFactoryBuilder() {}
6470

6571
/**
6672
* Sets the naming format to use when naming threads ({@link Thread#setName}) which are created
6773
* with this ThreadFactory.
6874
*
75+
* <p><b>Java 21+ users:</b> use {@link Thread.Builder#name(String, long)} instead. Note that
76+
* {@link #setNameFormat} accepts a thread name <i>format string</i> (e.g., {@code
77+
* threadFactoryBuilder.setNameFormat("rpc-pool-%d")}), while {@code threadBuilder.name()} accepts
78+
* a thread name <i>prefix</i> and initial counter value (e.g., {@code
79+
* threadBuilder.name("rpc-pool-", 0)}.
80+
*
6981
* @param nameFormat a {@link String#format(String, Object...)}-compatible format String, to which
7082
* a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will
7183
* be unique to the built instance of the ThreadFactory and will be assigned sequentially. For
@@ -83,6 +95,8 @@ public ThreadFactoryBuilder setNameFormat(String nameFormat) {
8395
/**
8496
* Sets daemon or not for new threads created with this ThreadFactory.
8597
*
98+
* <p><b>Java 21+ users:</b> use {@link Thread.Builder.OfPlatform#daemon(boolean)} instead.
99+
*
86100
* @param daemon whether or not new Threads created with this ThreadFactory will be daemon threads
87101
* @return this for the builder pattern
88102
*/
@@ -98,6 +112,8 @@ public ThreadFactoryBuilder setDaemon(boolean daemon) {
98112
* <p><b>Warning:</b> relying on the thread scheduler is <a
99113
* href="http://errorprone.info/bugpattern/ThreadPriorityCheck">discouraged</a>.
100114
*
115+
* <p><b>Java 21+ users:</b> use {@link Thread.Builder.OfPlatform#priority(int)} instead.
116+
*
101117
* @param priority the priority for new Threads created with this ThreadFactory
102118
* @return this for the builder pattern
103119
*/
@@ -122,6 +138,9 @@ public ThreadFactoryBuilder setPriority(int priority) {
122138
/**
123139
* Sets the {@link UncaughtExceptionHandler} for new threads created with this ThreadFactory.
124140
*
141+
* <p><b>Java 21+ users:</b> use {@link
142+
* Thread.Builder#uncaughtExceptionHandler(Thread.UncaughtExceptionHandler)} instead.
143+
*
125144
* @param uncaughtExceptionHandler the uncaught exception handler for new Threads created with
126145
* this ThreadFactory
127146
* @return this for the builder pattern
@@ -153,6 +172,8 @@ public ThreadFactoryBuilder setThreadFactory(ThreadFactory backingThreadFactory)
153172
* building, it is still possible to change the options used to build the ThreadFactory and/or
154173
* build again. State is not shared amongst built instances.
155174
*
175+
* <p><b>Java 21+ users:</b> use {@link Thread.Builder#factory()} instead.
176+
*
156177
* @return the fully constructed {@link ThreadFactory}
157178
*/
158179
public ThreadFactory build() {
@@ -161,6 +182,7 @@ public ThreadFactory build() {
161182

162183
// Split out so that the anonymous ThreadFactory can't contain a reference back to the builder.
163184
// At least, I assume that's why. TODO(cpovirk): Check, and maybe add a test for this.
185+
@SuppressWarnings("ThreadPriorityCheck") // We only propagate user requests (which we discourage).
164186
private static ThreadFactory doBuild(ThreadFactoryBuilder builder) {
165187
String nameFormat = builder.nameFormat;
166188
Boolean daemon = builder.daemon;

guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,25 @@ public final class ThreadFactoryBuilder {
5959
private @Nullable UncaughtExceptionHandler uncaughtExceptionHandler = null;
6060
private @Nullable ThreadFactory backingThreadFactory = null;
6161

62-
/** Creates a new {@link ThreadFactory} builder. */
62+
/**
63+
* Creates a new {@link ThreadFactory} builder.
64+
*
65+
* <p><b>Java 21+ users:</b> use {@link Thread#ofPlatform()} instead, translating other calls on
66+
* the builder as documented on each method (except for the rarely used {@link #setThreadFactory},
67+
* which does not have an equivalent).
68+
*/
6369
public ThreadFactoryBuilder() {}
6470

6571
/**
6672
* Sets the naming format to use when naming threads ({@link Thread#setName}) which are created
6773
* with this ThreadFactory.
6874
*
75+
* <p><b>Java 21+ users:</b> use {@link Thread.Builder#name(String, long)} instead. Note that
76+
* {@link #setNameFormat} accepts a thread name <i>format string</i> (e.g., {@code
77+
* threadFactoryBuilder.setNameFormat("rpc-pool-%d")}), while {@code threadBuilder.name()} accepts
78+
* a thread name <i>prefix</i> and initial counter value (e.g., {@code
79+
* threadBuilder.name("rpc-pool-", 0)}.
80+
*
6981
* @param nameFormat a {@link String#format(String, Object...)}-compatible format String, to which
7082
* a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will
7183
* be unique to the built instance of the ThreadFactory and will be assigned sequentially. For
@@ -83,6 +95,8 @@ public ThreadFactoryBuilder setNameFormat(String nameFormat) {
8395
/**
8496
* Sets daemon or not for new threads created with this ThreadFactory.
8597
*
98+
* <p><b>Java 21+ users:</b> use {@link Thread.Builder.OfPlatform#daemon(boolean)} instead.
99+
*
86100
* @param daemon whether or not new Threads created with this ThreadFactory will be daemon threads
87101
* @return this for the builder pattern
88102
*/
@@ -98,6 +112,8 @@ public ThreadFactoryBuilder setDaemon(boolean daemon) {
98112
* <p><b>Warning:</b> relying on the thread scheduler is <a
99113
* href="http://errorprone.info/bugpattern/ThreadPriorityCheck">discouraged</a>.
100114
*
115+
* <p><b>Java 21+ users:</b> use {@link Thread.Builder.OfPlatform#priority(int)} instead.
116+
*
101117
* @param priority the priority for new Threads created with this ThreadFactory
102118
* @return this for the builder pattern
103119
*/
@@ -122,6 +138,9 @@ public ThreadFactoryBuilder setPriority(int priority) {
122138
/**
123139
* Sets the {@link UncaughtExceptionHandler} for new threads created with this ThreadFactory.
124140
*
141+
* <p><b>Java 21+ users:</b> use {@link
142+
* Thread.Builder#uncaughtExceptionHandler(Thread.UncaughtExceptionHandler)} instead.
143+
*
125144
* @param uncaughtExceptionHandler the uncaught exception handler for new Threads created with
126145
* this ThreadFactory
127146
* @return this for the builder pattern
@@ -153,6 +172,8 @@ public ThreadFactoryBuilder setThreadFactory(ThreadFactory backingThreadFactory)
153172
* building, it is still possible to change the options used to build the ThreadFactory and/or
154173
* build again. State is not shared amongst built instances.
155174
*
175+
* <p><b>Java 21+ users:</b> use {@link Thread.Builder#factory()} instead.
176+
*
156177
* @return the fully constructed {@link ThreadFactory}
157178
*/
158179
public ThreadFactory build() {
@@ -161,6 +182,7 @@ public ThreadFactory build() {
161182

162183
// Split out so that the anonymous ThreadFactory can't contain a reference back to the builder.
163184
// At least, I assume that's why. TODO(cpovirk): Check, and maybe add a test for this.
185+
@SuppressWarnings("ThreadPriorityCheck") // We only propagate user requests (which we discourage).
164186
private static ThreadFactory doBuild(ThreadFactoryBuilder builder) {
165187
String nameFormat = builder.nameFormat;
166188
Boolean daemon = builder.daemon;

0 commit comments

Comments
 (0)