7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.lang;
26
27 import java.lang.Thread.Builder;
28 import java.lang.Thread.Builder.OfPlatform;
29 import java.lang.Thread.Builder.OfVirtual;
30 import java.lang.Thread.UncaughtExceptionHandler;
31 import java.lang.invoke.MethodHandles;
32 import java.lang.invoke.VarHandle;
33 import java.util.Locale;
34 import java.util.Objects;
35 import java.util.concurrent.Executor;
36 import java.util.concurrent.ThreadFactory;
37 import jdk.internal.misc.Unsafe;
38 import jdk.internal.vm.ContinuationSupport;
39
40 /**
41 * Defines static methods to create platform and virtual thread builders.
42 */
43 class ThreadBuilders {
44
45 /**
46 * Base implementation of ThreadBuilder.
47 */
48 static abstract non-sealed
49 class BaseThreadBuilder<T extends Builder> implements Builder {
50 private String name;
51 private long counter;
52 private int characteristics;
53 private UncaughtExceptionHandler uhe;
54
55 String name() {
56 return name;
57 }
58
59 long counter() {
60 return counter;
61 }
62
63 int characteristics() {
64 return characteristics;
65 }
66
67 UncaughtExceptionHandler uncaughtExceptionHandler() {
68 return uhe;
69 }
70
71 String nextThreadName() {
72 if (name != null && counter >= 0) {
73 return name + (counter++);
74 } else {
75 return name;
76 }
77 }
78
79 @Override
80 @SuppressWarnings("unchecked")
81 public T name(String name) {
82 this.name = Objects.requireNonNull(name);
83 this.counter = -1;
84 return (T) this;
85 }
86
87 @Override
88 @SuppressWarnings("unchecked")
89 public T name(String prefix, long start) {
90 Objects.requireNonNull(prefix);
91 if (start < 0)
92 throw new IllegalArgumentException("'start' is negative");
93 this.name = prefix;
94 this.counter = start;
95 return (T) this;
96 }
97
98 @Override
99 @SuppressWarnings("unchecked")
100 public T allowSetThreadLocals(boolean allow) {
101 if (allow) {
102 characteristics &= ~Thread.NO_THREAD_LOCALS;
103 } else {
104 characteristics |= Thread.NO_THREAD_LOCALS;
105 }
106 return (T) this;
107 }
108
109 @Override
110 @SuppressWarnings("unchecked")
111 public T inheritInheritableThreadLocals(boolean inherit) {
112 if (inherit) {
113 characteristics &= ~Thread.NO_INHERIT_THREAD_LOCALS;
114 } else {
115 characteristics |= Thread.NO_INHERIT_THREAD_LOCALS;
116 }
117 return (T) this;
118 }
119
120 @Override
121 @SuppressWarnings("unchecked")
122 public T uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
123 this.uhe = Objects.requireNonNull(ueh);
124 return (T) this;
125 }
126 }
127
128 /**
129 * ThreadBuilder.OfPlatform implementation.
130 */
131 static final class PlatformThreadBuilder
132 extends BaseThreadBuilder<OfPlatform> implements OfPlatform {
133 private ThreadGroup group;
134 private boolean daemon;
135 private boolean daemonChanged;
136 private int priority;
137 private long stackSize;
138
139 PlatformThreadBuilder() {
140 }
141
142 @Override
143 String nextThreadName() {
144 String name = super.nextThreadName();
145 return (name != null) ? name : Thread.genThreadName();
146 }
147
148 @Override
149 public OfPlatform group(ThreadGroup group) {
150 this.group = Objects.requireNonNull(group);
151 return this;
152 }
153
154 @Override
155 public OfPlatform daemon(boolean on) {
156 daemon = on;
157 daemonChanged = true;
158 return this;
159 }
160
161 @Override
162 public OfPlatform priority(int priority) {
163 if (priority < Thread.MIN_PRIORITY || priority > Thread.MAX_PRIORITY)
164 throw new IllegalArgumentException();
165 this.priority = priority;
166 return this;
167 }
191
192 @Override
193 public Thread start(Runnable task) {
194 Thread thread = unstarted(task);
195 thread.start();
196 return thread;
197 }
198
199 @Override
200 public ThreadFactory factory() {
201 return new PlatformThreadFactory(group, name(), counter(), characteristics(),
202 daemonChanged, daemon, priority, stackSize, uncaughtExceptionHandler());
203 }
204
205 }
206
207 /**
208 * ThreadBuilder.OfVirtual implementation.
209 */
210 static final class VirtualThreadBuilder
211 extends BaseThreadBuilder<OfVirtual> implements OfVirtual {
212 private Executor scheduler;
213
214 VirtualThreadBuilder() {
215 }
216
217 // invoked by tests
218 VirtualThreadBuilder(Executor scheduler) {
219 if (!ContinuationSupport.isSupported())
220 throw new UnsupportedOperationException();
221 this.scheduler = Objects.requireNonNull(scheduler);
222 }
223
224 @Override
225 public Thread unstarted(Runnable task) {
226 Objects.requireNonNull(task);
227 var thread = newVirtualThread(scheduler, nextThreadName(), characteristics(), task);
228 UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
229 if (uhe != null)
230 thread.uncaughtExceptionHandler(uhe);
231 return thread;
232 }
233
234 @Override
235 public Thread start(Runnable task) {
236 Thread thread = unstarted(task);
237 thread.start();
238 return thread;
239 }
240
241 @Override
242 public ThreadFactory factory() {
243 return new VirtualThreadFactory(scheduler, name(), counter(), characteristics(),
|
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.lang;
26
27 import java.lang.Thread.Builder.OfPlatform;
28 import java.lang.Thread.Builder.OfVirtual;
29 import java.lang.Thread.UncaughtExceptionHandler;
30 import java.lang.invoke.MethodHandles;
31 import java.lang.invoke.VarHandle;
32 import java.util.Locale;
33 import java.util.Objects;
34 import java.util.concurrent.Executor;
35 import java.util.concurrent.ThreadFactory;
36 import jdk.internal.misc.Unsafe;
37 import jdk.internal.vm.ContinuationSupport;
38
39 /**
40 * Defines static methods to create platform and virtual thread builders.
41 */
42 class ThreadBuilders {
43 private ThreadBuilders() { }
44
45 /**
46 * Base class for Thread.Builder implementations.
47 */
48 private static class BaseThreadBuilder {
49 private String name;
50 private long counter;
51 private int characteristics;
52 private UncaughtExceptionHandler uhe;
53
54 String name() {
55 return name;
56 }
57
58 long counter() {
59 return counter;
60 }
61
62 int characteristics() {
63 return characteristics;
64 }
65
66 UncaughtExceptionHandler uncaughtExceptionHandler() {
67 return uhe;
68 }
69
70 String nextThreadName() {
71 if (name != null && counter >= 0) {
72 return name + (counter++);
73 } else {
74 return name;
75 }
76 }
77
78 void setName(String name) {
79 this.name = Objects.requireNonNull(name);
80 this.counter = -1;
81 }
82
83 void setName(String prefix, long start) {
84 Objects.requireNonNull(prefix);
85 if (start < 0)
86 throw new IllegalArgumentException("'start' is negative");
87 this.name = prefix;
88 this.counter = start;
89 }
90
91 void setInheritInheritableThreadLocals(boolean inherit) {
92 if (inherit) {
93 characteristics &= ~Thread.NO_INHERIT_THREAD_LOCALS;
94 } else {
95 characteristics |= Thread.NO_INHERIT_THREAD_LOCALS;
96 }
97 }
98
99 void setUncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
100 this.uhe = Objects.requireNonNull(ueh);
101 }
102 }
103
104 /**
105 * ThreadBuilder.OfPlatform implementation.
106 */
107 static final class PlatformThreadBuilder
108 extends BaseThreadBuilder implements OfPlatform {
109 private ThreadGroup group;
110 private boolean daemon;
111 private boolean daemonChanged;
112 private int priority;
113 private long stackSize;
114
115 PlatformThreadBuilder() {
116 }
117
118 @Override
119 String nextThreadName() {
120 String name = super.nextThreadName();
121 return (name != null) ? name : Thread.genThreadName();
122 }
123
124 @Override
125 public OfPlatform name(String name) {
126 setName(name);
127 return this;
128 }
129
130 @Override
131 public OfPlatform name(String prefix, long start) {
132 setName(prefix, start);
133 return this;
134 }
135
136 @Override
137 public OfPlatform inheritInheritableThreadLocals(boolean inherit) {
138 setInheritInheritableThreadLocals(inherit);
139 return this;
140 }
141
142 @Override
143 public OfPlatform uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
144 setUncaughtExceptionHandler(ueh);
145 return this;
146 }
147
148 @Override
149 public OfPlatform group(ThreadGroup group) {
150 this.group = Objects.requireNonNull(group);
151 return this;
152 }
153
154 @Override
155 public OfPlatform daemon(boolean on) {
156 daemon = on;
157 daemonChanged = true;
158 return this;
159 }
160
161 @Override
162 public OfPlatform priority(int priority) {
163 if (priority < Thread.MIN_PRIORITY || priority > Thread.MAX_PRIORITY)
164 throw new IllegalArgumentException();
165 this.priority = priority;
166 return this;
167 }
191
192 @Override
193 public Thread start(Runnable task) {
194 Thread thread = unstarted(task);
195 thread.start();
196 return thread;
197 }
198
199 @Override
200 public ThreadFactory factory() {
201 return new PlatformThreadFactory(group, name(), counter(), characteristics(),
202 daemonChanged, daemon, priority, stackSize, uncaughtExceptionHandler());
203 }
204
205 }
206
207 /**
208 * ThreadBuilder.OfVirtual implementation.
209 */
210 static final class VirtualThreadBuilder
211 extends BaseThreadBuilder implements OfVirtual {
212 private Executor scheduler;
213
214 VirtualThreadBuilder() {
215 }
216
217 // invoked by tests
218 VirtualThreadBuilder(Executor scheduler) {
219 if (!ContinuationSupport.isSupported())
220 throw new UnsupportedOperationException();
221 this.scheduler = Objects.requireNonNull(scheduler);
222 }
223
224 @Override
225 public OfVirtual name(String name) {
226 setName(name);
227 return this;
228 }
229
230 @Override
231 public OfVirtual name(String prefix, long start) {
232 setName(prefix, start);
233 return this;
234 }
235
236 @Override
237 public OfVirtual inheritInheritableThreadLocals(boolean inherit) {
238 setInheritInheritableThreadLocals(inherit);
239 return this;
240 }
241
242 @Override
243 public OfVirtual uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
244 setUncaughtExceptionHandler(ueh);
245 return this;
246 }
247
248 @Override
249 public Thread unstarted(Runnable task) {
250 Objects.requireNonNull(task);
251 var thread = newVirtualThread(scheduler, nextThreadName(), characteristics(), task);
252 UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
253 if (uhe != null)
254 thread.uncaughtExceptionHandler(uhe);
255 return thread;
256 }
257
258 @Override
259 public Thread start(Runnable task) {
260 Thread thread = unstarted(task);
261 thread.start();
262 return thread;
263 }
264
265 @Override
266 public ThreadFactory factory() {
267 return new VirtualThreadFactory(scheduler, name(), counter(), characteristics(),
|