26
26
*/
27
27
public final class ThreadPoolUtils {
28
28
29
- public static final int FixedThread = 0 ;
30
- public static final int CachedThread = 1 ;
31
- public static final int SingleThread = 2 ;
29
+ public static final int ScheduledThread = 0 ;
30
+ public static final int FixedThread = 1 ;
31
+ public static final int CachedThread = 2 ;
32
+ public static final int SingleThread = 3 ;
32
33
33
- @ IntDef ({FixedThread , CachedThread , SingleThread })
34
+ @ IntDef ({ScheduledThread , FixedThread , CachedThread , SingleThread })
34
35
@ Retention (RetentionPolicy .SOURCE )
35
36
public @interface Type {
36
37
}
37
38
38
- private ExecutorService exec ;
39
- private ScheduledExecutorService scheduleExec ;
39
+ private ExecutorService exec ;
40
40
41
41
private ThreadPoolUtils () {
42
42
throw new UnsupportedOperationException ("u can't instantiate me..." );
@@ -49,16 +49,19 @@ private ThreadPoolUtils() {
49
49
* @param corePoolSize 只对Fixed和Scheduled线程池起效
50
50
*/
51
51
public ThreadPoolUtils (@ Type final int type , final int corePoolSize ) {
52
- // 构造有定时功能的线程池
52
+
53
+ switch (type ) {
54
+ case ScheduledThread :
55
+ // 构造有定时功能的线程池
53
56
// new ThreadPoolExecutor(
54
57
// corePoolSize,
55
58
// Integer.MAX_VALUE,
56
59
// 10L,
57
60
// TimeUnit.MILLISECONDS,
58
61
// new DelayedWorkQueue()
59
62
// );
60
- scheduleExec = Executors .newScheduledThreadPool (corePoolSize );
61
- switch ( type ) {
63
+ exec = Executors .newScheduledThreadPool (corePoolSize );
64
+ break ;
62
65
case FixedThread :
63
66
// 构造一个固定线程数目的线程池
64
67
// new ThreadPoolExecutor(
@@ -295,7 +298,10 @@ public <T> T invokeAny(final Collection<? extends Callable<T>> tasks,
295
298
public ScheduledFuture <?> schedule (final Runnable command ,
296
299
final long delay ,
297
300
final TimeUnit unit ) {
298
- return scheduleExec .schedule (command , delay , unit );
301
+ if (!(exec instanceof ScheduledExecutorService )) {
302
+ throw new ClassCastException ("Exec can't cast to ScheduledExecutorService." );
303
+ }
304
+ return ((ScheduledExecutorService ) exec ).schedule (command , delay , unit );
299
305
}
300
306
301
307
/**
@@ -310,7 +316,10 @@ public ScheduledFuture<?> schedule(final Runnable command,
310
316
public <T > ScheduledFuture <T > schedule (final Callable <T > callable ,
311
317
final long delay ,
312
318
final TimeUnit unit ) {
313
- return scheduleExec .schedule (callable , delay , unit );
319
+ if (!(exec instanceof ScheduledExecutorService )) {
320
+ throw new ClassCastException ("Exec can't cast to ScheduledExecutorService." );
321
+ }
322
+ return ((ScheduledExecutorService ) exec ).schedule (callable , delay , unit );
314
323
}
315
324
316
325
/**
@@ -326,7 +335,11 @@ public ScheduledFuture<?> scheduleWithFixedRate(final Runnable command,
326
335
final long initialDelay ,
327
336
final long period ,
328
337
final TimeUnit unit ) {
329
- return scheduleExec .scheduleAtFixedRate (command , initialDelay , period , unit );
338
+ if (!(exec instanceof ScheduledExecutorService )) {
339
+ throw new ClassCastException ("Exec can't cast to ScheduledExecutorService." );
340
+ }
341
+ return ((ScheduledExecutorService ) exec )
342
+ .scheduleAtFixedRate (command , initialDelay , period , unit );
330
343
}
331
344
332
345
/**
@@ -342,6 +355,10 @@ public ScheduledFuture<?> scheduleWithFixedDelay(final Runnable command,
342
355
final long initialDelay ,
343
356
final long delay ,
344
357
final TimeUnit unit ) {
345
- return scheduleExec .scheduleWithFixedDelay (command , initialDelay , delay , unit );
358
+ if (!(exec instanceof ScheduledExecutorService )) {
359
+ throw new ClassCastException ("Exec can't cast to ScheduledExecutorService." );
360
+ }
361
+ return ((ScheduledExecutorService ) exec )
362
+ .scheduleWithFixedDelay (command , initialDelay , delay , unit );
346
363
}
347
364
}
0 commit comments