scheduledthreadpool 开启了周期任务,怎么停止但不关闭线程池??

scheduledthreadpool 开启了周期任务,怎么停止但不关闭线程池??

问题描述:

scheduledthreadpool 开启了周期任务,怎么停止但不关闭线程池??

在执行任务时持有ScheduledFuture就行,调用cancel

如:

ScheduledFuture taskFuture = schedule.scheduleWithFixedDelay(() -> {
                           // 你的逻辑
                                                        }, 5000, 5000, TimeUnit.MILLISECONDS);

                                                        //如果想要取消
                                                        taskFuture.cancel(true);

希望对你有帮助...