1.简单使用
public static void main(String[] args) throws InterruptedException {
ScheduledExecutorService ext = Executors.newScheduledThreadPool(50);
ext.awaitTermination(10L, TimeUnit.SECONDS);
for (int i = 0; i <10 ; i++) {
System.out.println("i=="+ i +"---" + System.currentTimeMillis());
ext.execute(new Runnable() {
@Override
public void run() {
System.out.println(System.currentTimeMillis());
}
});
}
}
2.源码分析
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
1.上面的代码可以看出使用的是延时队列DelayedWorkQueue
2.最大核心数仍是Integer.MAX_VALUE如果使用的话不能直接使用要使用ScheduledThreadPoolExecutor自己创建,同是设置合适的最大核心数。
3.从下图执行方法来看核心还是这里的触发器triggerTime