1
- // Copyright (c) 2018, 2021 , Oracle and/or its affiliates.
1
+ // Copyright (c) 2018, 2023 , Oracle and/or its affiliates.
2
2
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3
3
4
4
package oracle .kubernetes .operator .work ;
5
5
6
+ import java .util .Collection ;
7
+ import java .util .List ;
8
+ import java .util .concurrent .Callable ;
9
+ import java .util .concurrent .ExecutionException ;
6
10
import java .util .concurrent .Executor ;
11
+ import java .util .concurrent .ExecutorService ;
12
+ import java .util .concurrent .Executors ;
13
+ import java .util .concurrent .Future ;
7
14
import java .util .concurrent .ScheduledExecutorService ;
15
+ import java .util .concurrent .ScheduledFuture ;
8
16
import java .util .concurrent .ScheduledThreadPoolExecutor ;
9
- import java .util .concurrent .ThreadFactory ;
10
- import java .util .concurrent .atomic . AtomicInteger ;
17
+ import java .util .concurrent .TimeUnit ;
18
+ import java .util .concurrent .TimeoutException ;
11
19
import java .util .concurrent .atomic .AtomicReference ;
12
20
import javax .annotation .Nonnull ;
13
21
14
22
/**
15
23
* Collection of {@link Fiber}s. Owns an {@link Executor} to run them.
16
24
*/
17
25
public class Engine {
18
- private static final int DEFAULT_THREAD_COUNT = 10 ;
26
+ private static final int DEFAULT_THREAD_COUNT = 2 ;
19
27
private final AtomicReference <ScheduledExecutorService > threadPool = new AtomicReference <>();
20
28
21
29
/**
@@ -29,30 +37,146 @@ public Engine(ScheduledExecutorService threadPool) {
29
37
30
38
/**
31
39
* Creates engine with the specified id and default container and executor.
32
- *
33
- * @param id Engine id
34
40
*/
35
- public Engine (String id ) {
36
- this (wrappedExecutorService (id , ContainerResolver .getDefault ().getContainer ()));
41
+ public Engine () {
42
+ this (wrappedExecutorService (ContainerResolver .getDefault ().getContainer ()));
37
43
}
38
44
39
45
/**
40
46
* wrapped executor service.
41
- * @param id id
42
47
* @param container container
43
48
* @return executor service
44
49
*/
45
- public static ScheduledExecutorService wrappedExecutorService (String id , Container container ) {
46
- ScheduledThreadPoolExecutor threadPool =
47
- new ScheduledThreadPoolExecutor (DEFAULT_THREAD_COUNT , new DaemonThreadFactory (id ));
50
+ public static ScheduledExecutorService wrappedExecutorService (Container container ) {
51
+ ScheduledThreadPoolExecutor threadPool = new ScheduledThreadPoolExecutor (DEFAULT_THREAD_COUNT );
48
52
threadPool .setRemoveOnCancelPolicy (true );
49
- return wrap (container , threadPool );
53
+ return wrap (container , new VirtualScheduledExectuorService ( threadPool ) );
50
54
}
51
55
52
56
private static ScheduledExecutorService wrap (Container container , ScheduledExecutorService ex ) {
53
57
return container != null ? ContainerResolver .getDefault ().wrapExecutor (container , ex ) : ex ;
54
58
}
55
59
60
+ private static class VirtualScheduledExectuorService implements ScheduledExecutorService {
61
+ private final ScheduledExecutorService service ;
62
+ private final ExecutorService virtualService = Executors .newVirtualThreadPerTaskExecutor ();
63
+
64
+ public VirtualScheduledExectuorService (ScheduledExecutorService service ) {
65
+ this .service = service ;
66
+ }
67
+
68
+ private Runnable wrap (Runnable command ) {
69
+ return () -> virtualService .execute (command );
70
+ }
71
+
72
+ @ Nonnull
73
+ @ Override
74
+ public ScheduledFuture <?> schedule (@ Nonnull Runnable command , long delay , @ Nonnull TimeUnit unit ) {
75
+ return service .schedule (wrap (command ), delay , unit );
76
+ }
77
+
78
+ @ Nonnull
79
+ @ Override
80
+ public <V > ScheduledFuture <V > schedule (@ Nonnull Callable <V > callable , long delay , @ Nonnull TimeUnit unit ) {
81
+ throw new UnsupportedOperationException ();
82
+ }
83
+
84
+ @ Nonnull
85
+ @ Override
86
+ public ScheduledFuture <?> scheduleAtFixedRate (
87
+ @ Nonnull Runnable command , long initialDelay , long period , @ Nonnull TimeUnit unit ) {
88
+ return service .scheduleAtFixedRate (wrap (command ), initialDelay , period , unit );
89
+ }
90
+
91
+ @ Nonnull
92
+ @ Override
93
+ public ScheduledFuture <?> scheduleWithFixedDelay (
94
+ @ Nonnull Runnable command , long initialDelay , long delay , @ Nonnull TimeUnit unit ) {
95
+ return service .scheduleWithFixedDelay (wrap (command ), initialDelay , delay , unit );
96
+ }
97
+
98
+ @ Override
99
+ public void shutdown () {
100
+ service .shutdown ();
101
+ }
102
+
103
+ @ Nonnull
104
+ @ Override
105
+ public List <Runnable > shutdownNow () {
106
+ return service .shutdownNow ();
107
+ }
108
+
109
+ @ Override
110
+ public boolean isShutdown () {
111
+ return service .isShutdown ();
112
+ }
113
+
114
+ @ Override
115
+ public boolean isTerminated () {
116
+ return service .isTerminated ();
117
+ }
118
+
119
+ @ Override
120
+ public boolean awaitTermination (long timeout , @ Nonnull TimeUnit unit ) throws InterruptedException {
121
+ return service .awaitTermination (timeout , unit );
122
+ }
123
+
124
+ @ Nonnull
125
+ @ Override
126
+ public <T > Future <T > submit (@ Nonnull Callable <T > task ) {
127
+ throw new UnsupportedOperationException ();
128
+ }
129
+
130
+ @ Nonnull
131
+ @ Override
132
+ public <T > Future <T > submit (@ Nonnull Runnable task , T result ) {
133
+ return service .submit (wrap (task ), result );
134
+ }
135
+
136
+ @ Nonnull
137
+ @ Override
138
+ public Future <?> submit (@ Nonnull Runnable task ) {
139
+ return service .submit (wrap (task ));
140
+ }
141
+
142
+ @ Nonnull
143
+ @ Override
144
+ public <T > List <Future <T >> invokeAll (@ Nonnull Collection <? extends Callable <T >> tasks ) throws InterruptedException {
145
+ throw new UnsupportedOperationException ();
146
+ }
147
+
148
+ @ Nonnull
149
+ @ Override
150
+ public <T > List <Future <T >> invokeAll (
151
+ @ Nonnull Collection <? extends Callable <T >> tasks , long timeout , @ Nonnull TimeUnit unit )
152
+ throws InterruptedException {
153
+ throw new UnsupportedOperationException ();
154
+ }
155
+
156
+ @ Nonnull
157
+ @ Override
158
+ public <T > T invokeAny (@ Nonnull Collection <? extends Callable <T >> tasks )
159
+ throws InterruptedException , ExecutionException {
160
+ throw new UnsupportedOperationException ();
161
+ }
162
+
163
+ @ Override
164
+ public <T > T invokeAny (@ Nonnull Collection <? extends Callable <T >> tasks , long timeout , @ Nonnull TimeUnit unit )
165
+ throws InterruptedException , ExecutionException , TimeoutException {
166
+ throw new UnsupportedOperationException ();
167
+ }
168
+
169
+ @ Override
170
+ public void close () {
171
+ service .close ();
172
+ }
173
+
174
+ @ Override
175
+ public void execute (@ Nonnull Runnable command ) {
176
+ virtualService .execute (command );
177
+ }
178
+ }
179
+
56
180
/**
57
181
* Returns the executor.
58
182
*
@@ -81,25 +205,4 @@ public Fiber createFiber() {
81
205
Fiber createChildFiber (Fiber parent ) {
82
206
return new Fiber (this , parent );
83
207
}
84
-
85
- private static class DaemonThreadFactory implements ThreadFactory {
86
- final AtomicInteger threadNumber = new AtomicInteger (1 );
87
- final String namePrefix ;
88
-
89
- DaemonThreadFactory (String id ) {
90
- namePrefix = "engine-" + id + "-thread-" ;
91
- }
92
-
93
- public Thread newThread (@ Nonnull Runnable r ) {
94
- Thread t = new Thread (r );
95
- t .setName (namePrefix + threadNumber .getAndIncrement ());
96
- if (!t .isDaemon ()) {
97
- t .setDaemon (true );
98
- }
99
- if (t .getPriority () != Thread .NORM_PRIORITY ) {
100
- t .setPriority (Thread .NORM_PRIORITY );
101
- }
102
- return t ;
103
- }
104
- }
105
208
}
0 commit comments