Skip to content

Commit 897ba1b

Browse files
committed
see 03/28 log
1 parent f8fd701 commit 897ba1b

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

subutil/src/main/java/com/blankj/subutil/util/ThreadPoolUtils.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626
*/
2727
public final class ThreadPoolUtils {
2828

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;
3233

33-
@IntDef({FixedThread, CachedThread, SingleThread})
34+
@IntDef({ScheduledThread, FixedThread, CachedThread, SingleThread})
3435
@Retention(RetentionPolicy.SOURCE)
3536
public @interface Type {
3637
}
3738

38-
private ExecutorService exec;
39-
private ScheduledExecutorService scheduleExec;
39+
private ExecutorService exec;
4040

4141
private ThreadPoolUtils() {
4242
throw new UnsupportedOperationException("u can't instantiate me...");
@@ -49,16 +49,19 @@ private ThreadPoolUtils() {
4949
* @param corePoolSize 只对Fixed和Scheduled线程池起效
5050
*/
5151
public ThreadPoolUtils(@Type final int type, final int corePoolSize) {
52-
// 构造有定时功能的线程池
52+
53+
switch (type) {
54+
case ScheduledThread:
55+
// 构造有定时功能的线程池
5356
// new ThreadPoolExecutor(
5457
// corePoolSize,
5558
// Integer.MAX_VALUE,
5659
// 10L,
5760
// TimeUnit.MILLISECONDS,
5861
// new DelayedWorkQueue()
5962
// );
60-
scheduleExec = Executors.newScheduledThreadPool(corePoolSize);
61-
switch (type) {
63+
exec = Executors.newScheduledThreadPool(corePoolSize);
64+
break;
6265
case FixedThread:
6366
// 构造一个固定线程数目的线程池
6467
// new ThreadPoolExecutor(
@@ -295,7 +298,10 @@ public <T> T invokeAny(final Collection<? extends Callable<T>> tasks,
295298
public ScheduledFuture<?> schedule(final Runnable command,
296299
final long delay,
297300
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);
299305
}
300306

301307
/**
@@ -310,7 +316,10 @@ public ScheduledFuture<?> schedule(final Runnable command,
310316
public <T> ScheduledFuture<T> schedule(final Callable<T> callable,
311317
final long delay,
312318
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);
314323
}
315324

316325
/**
@@ -326,7 +335,11 @@ public ScheduledFuture<?> scheduleWithFixedRate(final Runnable command,
326335
final long initialDelay,
327336
final long period,
328337
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);
330343
}
331344

332345
/**
@@ -342,6 +355,10 @@ public ScheduledFuture<?> scheduleWithFixedDelay(final Runnable command,
342355
final long initialDelay,
343356
final long delay,
344357
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);
346363
}
347364
}

0 commit comments

Comments
 (0)