关于ScheduleThreadPool的小问题

luqing414 2016-12-07 11:56:27
通过Executors.newScheduledThreadPool()创建,核心数量固定且非核心线程数量无限制,当非核心线程闲置时立即被回收。
适于执行定时任务和固定周期的重复任务。

这是网上查到的定义,完后还有一个小例子

Runnable runnable2 = new Runnable() {
@Override
public void run() {
Log.d(TAG,"runnable2 run in thread "+Thread.currentThread().getId());

try {
//睡眠1min 为了便于观察结果
TimeUnit.SECONDS.sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
Log.d(TAG,"runnable2 is interrupted");
}
Log.d(TAG,"任务完成!");

}
};

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
scheduledExecutorService.schedule(runnable2,1,TimeUnit.SECONDS);
scheduledExecutorService.schedule(runnable2,1,TimeUnit.SECONDS);
//创建的时候核心线程数为2 因此任务数目大于2时会等待前面的任务执行完才会开始执行新任务
scheduledExecutorService.schedule(runnable2,1,TimeUnit.SECONDS);

scheduledExecutorService.shutdown();

这个执行结果是,第三个任务等待签名任务执行完了,才开始执行,也就是说,第三个任务也是用核心线程执行的,那我想问,非核心线程是什么时候才会启用呢,为什么第三个任务没有用非核心线程执行呢
...全文
296 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
萧乡月夜 2016-12-08
  • 打赏
  • 举报
回复
在我看来2楼是对核心线程生命周期的解释,5楼是对ScheduledThreadPoolExecutor类的解释,没有冲突啊 核心线程在生命周期角度来看是常驻,在优先级角度来看跟非核心线程是同一级别上的 你的confuse在哪里?
逗泥丸的平方 2016-12-08
  • 打赏
  • 举报
回复
引用 5 楼 aqzwss 的回复:
不要太死板嘛。。corePoolSize翻译过来是啥?核心池容量吧 1.当线程池小于corePoolSize时,新提交任务将创建一个新线程执行任务,即使此时线程池中存在空闲线程。 2.当线程池达到corePoolSize时,新提交任务将被放入workQueue中,等待线程池中任务调度执行 3.当workQueue已满,且maximumPoolSize>corePoolSize时,新提交任务会创建新线程执行任务 4.当提交任务数超过maximumPoolSize时,新提交任务由RejectedExecutionHandler处理 5.当线程池中超过corePoolSize线程,空闲时间达到keepAliveTime时,关闭空闲线程 6.当设置allowCoreThreadTimeOut(true)时,线程池中corePoolSize线程空闲时间达到keepAliveTime也将关闭
引用 2 楼 aqzwss 的回复:
核心线程就是即使空闲了也不会被回收的线程,即永久存在的线程,非核心线程是运行结束后空闲了就会被销毁的线程。
但是这两个说法有什么关系吗.... 这里看起来是一直在用线程池里的线程,并没有感觉到你说的非核心线程呀 ? 根据你在2L的解释,我理解的"核心线程"指的是常驻的线程, 但是现在看起来,这些线程是在同一级别上的,只是任务时间不同而已,
soton_dolphin 2016-12-08
  • 打赏
  • 举报
回复
引用 7 楼 aqzwss 的回复:
在我看来2楼是对核心线程生命周期的解释,5楼是对ScheduledThreadPoolExecutor类的解释,没有冲突啊 核心线程在生命周期角度来看是常驻,在优先级角度来看跟非核心线程是同一级别上的 你的confuse在哪里?
在线程和同步的世界里,没有核心线程这个概念啊。倒是听过“守护线程””
soton_dolphin 2016-12-07
  • 打赏
  • 举报
回复
什么是核心线程?什么是非核心线程?
萧乡月夜 2016-12-07
  • 打赏
  • 举报
回复
不要太死板嘛。。corePoolSize翻译过来是啥?核心池容量吧 1.当线程池小于corePoolSize时,新提交任务将创建一个新线程执行任务,即使此时线程池中存在空闲线程。 2.当线程池达到corePoolSize时,新提交任务将被放入workQueue中,等待线程池中任务调度执行 3.当workQueue已满,且maximumPoolSize>corePoolSize时,新提交任务会创建新线程执行任务 4.当提交任务数超过maximumPoolSize时,新提交任务由RejectedExecutionHandler处理 5.当线程池中超过corePoolSize线程,空闲时间达到keepAliveTime时,关闭空闲线程 6.当设置allowCoreThreadTimeOut(true)时,线程池中corePoolSize线程空闲时间达到keepAliveTime也将关闭
soton_dolphin 2016-12-07
  • 打赏
  • 举报
回复
引用 2 楼 aqzwss 的回复:

    /**
     * Creates a new {@code ScheduledThreadPoolExecutor} with the
     * given core pool size.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
     */
    public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue());
    }
核心线程就是即使空闲了也不会被回收的线程,即永久存在的线程,非核心线程是运行结束后空闲了就会被销毁的线程。
这里根本就没有核心线程和非核心线程的概念啊,这只是说在这个线程池里生成线程的数量
逗泥丸的平方 2016-12-07
  • 打赏
  • 举报
回复
不是太懂 这个和核心有什么关系.... 这只是受限于你的线程池吧~
萧乡月夜 2016-12-07
  • 打赏
  • 举报
回复

    /**
     * Creates a new {@code ScheduledThreadPoolExecutor} with the
     * given core pool size.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
     */
    public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue());
    }
核心线程就是即使空闲了也不会被回收的线程,即永久存在的线程,非核心线程是运行结束后空闲了就会被销毁的线程。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧