forked from thunkware/virtual-threads-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutorTool.java
More file actions
152 lines (138 loc) · 6.12 KB
/
ExecutorTool.java
File metadata and controls
152 lines (138 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package io.github.thunkware.vt.bridge;
import java.time.Duration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import static io.github.thunkware.vt.bridge.ThreadProviderFactory.getThreadProvider;
/**
* Utility for working with Executors API from Java21 in Java8+ VM. Convenience class for {@link ThreadProvider}
*/
public class ExecutorTool {
/**
* On Java 8+, returns false
* On Java 21+, returns true
*
* @return true if the JVM supports virtual threads
*/
public static final boolean hasVirtualThreads() {
return getThreadProvider().hasVirtualThreads();
}
/**
* Creates an Executor that starts a new Thread for each task.
* The number of threads created by the Executor is unbounded.
*
* <p> Invoking {@link Future#cancel(boolean) cancel(true)} on a {@link
* Future Future} representing the pending result of a task submitted to
* the Executor will {@link Thread#interrupt() interrupt} the thread
* executing the task.
*
* @param threadFactory the factory to use when creating new threads
* @return a new executor that creates a new Thread for each task
* @throws NullPointerException if threadFactory is null
*/
public static ExecutorService newThreadPerTaskExecutor(ThreadFactory threadFactory) {
return getThreadProvider().newThreadPerTaskExecutor(threadFactory);
}
/**
* Creates an Executor that starts a new virtual Thread on Java 21 + (or new platform thread on Java 8+) for each task.
* The number of threads created by the Executor is unbounded.
*
* <p> This method is equivalent to invoking
* {@link #newThreadPerTaskExecutor(ThreadFactory)} with a thread factory
* that creates virtual threads.
*
* @return a new executor that creates a new virtual Thread for each task
*/
public static ExecutorService newVirtualThreadPerTaskExecutor() {
return getThreadProvider().newVirtualThreadPerTaskExecutor();
}
/**
* Creates an Executor that starts a new virtual Thread on Java 21 + (or new platform thread on Java 8+) for each task.
* The number of threads created by the Executor is unbounded.
*
* <p> This method is equivalent to invoking
* {@link #newThreadPerTaskExecutor(ThreadFactory)} with a thread factory
* that creates virtual threads.
*
* @param threadCustomizer ThreadCustomizer to customize new unstarted threads
* @return a new executor that creates a new virtual Thread for each task
*/
public static ExecutorService newVirtualThreadPerTaskExecutor(ThreadCustomizer threadCustomizer) {
return getThreadProvider().newVirtualThreadPerTaskExecutor(threadCustomizer);
}
/**
* Creates an Executor that starts a new virtual Thread and limits concurrency to the number of sempahore permits
*
* @param permits number of sempahore permits
* @return a new executor with limited concurrency
* @deprecated Because of typo in method name
*/
@Deprecated
public static ExecutorService newSempahoreVirtualExecutor(int permits) {
return newSemaphoreVirtualExecutor(permits);
}
/**
* Creates an Executor that starts a new virtual Thread and limits concurrency to the number of semaphore permits
*
* @param permits number of semaphore permits
* @return a new executor with limited concurrency
*/
public static ExecutorService newSemaphoreVirtualExecutor(int permits) {
ExecutorService executor = getThreadProvider().newVirtualThreadPerTaskExecutor();
return new SemaphoreExecutor(executor, permits);
}
/**
* Creates an Executor that starts a new virtual Thread and limits concurrency
* to the number of semaphore permits.
*
* <p>
* When executing a task with the created ExecutorService if one permit is
* available, the task is executed. If no permit is available, then the executor
* thread becomes disabled for thread scheduling purposes and lies dormant until
* one of three things happens:
* <ul>
* <li>a permit becomes available</li>
* <li>some other thread {@linkplain Thread#interrupt interrupts} the current
* thread; or</li>
* <li>the specified waiting time elapses, in which case a Timeout exception is thrown</li>
* </ul>
*
* @param permits number of sempahore permits
* @param acquireTimeout time to wait for acquire a new task when no permit is
* available
* @return a new executor with limited concurrency
* @deprecated Because of typo in method name
*/
@Deprecated
public static ExecutorService newSempahoreVirtualExecutor(int permits, Duration acquireTimeout) {
return newSemaphoreVirtualExecutor(permits, acquireTimeout);
}
/**
* Creates an Executor that starts a new virtual Thread and limits concurrency
* to the number of semaphore permits.
*
* <p>
* When executing a task with the created ExecutorService if one permit is
* available, the task is executed. If no permit is available, then the executor
* thread becomes disabled for thread scheduling purposes and lies dormant until
* one of three things happens:
* <ul>
* <li>a permit becomes available</li>
* <li>some other thread {@linkplain Thread#interrupt interrupts} the current
* thread; or</li>
* <li>the specified waiting time elapses, in which case a Timeout exception is thrown</li>
* </ul>
*
* @param permits number of semaphore permits
* @param acquireTimeout time to wait for acquire a new task when no permit is
* available
* @return a new executor with limited concurrency
*/
public static ExecutorService newSemaphoreVirtualExecutor(int permits, Duration acquireTimeout) {
ExecutorService executor = getThreadProvider().newVirtualThreadPerTaskExecutor();
return new SemaphoreExecutor(executor, permits, acquireTimeout);
}
private ExecutorTool() {
throw new AssertionError();
}
}