Java实现终止线程池中正在运行的定时任务

代码星冰乐

专注成就未来

首页 归档 关于

Java实现终止线程池中正在运行的定时任务

Jan 17, 2018 | haifeiWu | Java | 阅读
文章目录
  1. 1. 线程池是什么
  2. 2. 线程池service的获取
  3. 3. 中断某一个正在运行的线程代码实现
  4. 4. 踩坑记录
  5. 5. 总结一下吧

作 者:haifeiWu
原文链接:https://www.hchstudio.cn/article/2018/cc2a/
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。


由于版权原因,请阅读原文 --> Java实现终止线程池中正在运行的定时任务

关注我们

作 者:haifeiWu
原文链接:https://www.hchstudio.cn/article/2018/cc2a/
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。

作 者:haifeiWu
原文链接:https://www.hchstudio.cn/article/2018/cc2a/
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。

最近项目中遇到了一个新的需求,就是实现一个可以动态添加定时任务的功能。说到这里,有人可能会说简单啊,使用quartz就好了,简单粗暴。然而quartz框架太重了,小项目根本不好操作啊。当然,也有人会说,jdk提供了timer的接口啊,完全够用啊。但是我们项目的需求完全是多线程的模型啊,而timer是单线程的,so,楼主最后还是选择了jdk的线程池。

线程池是什么

Java通过Executors提供四种线程池,分别为:
newCachedThreadPool :创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool : 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool : 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor : 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

楼主项目中用到的是newScheduledThreadPool, 就这些吧,再多的楼主就班门弄斧了,Google一下,一大堆。

线程池service的获取

楼主通过单例模式来获取线程池的service,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 线程池创建.
* @author wuhf
* @date 2018/01/16
*/
public class ThreadPoolUtils {

private static ScheduledExecutorService executorService;

private ThreadPoolUtils() {
//手动创建线程池.
executorService = new ScheduledThreadPoolExecutor(10,
new BasicThreadFactory.Builder().namingPattern("syncdata-schedule-pool-%d").daemon(true).build());
}

private static class PluginConfigHolder {
private final static ThreadPoolUtils INSTANCE = new ThreadPoolUtils();
}

public static ThreadPoolUtils getInstance() {
return PluginConfigHolder.INSTANCE;
}

public ScheduledExecutorService getThreadPool(){
return executorService;
}

}

中断某一个正在运行的线程代码实现

废话就不多说了,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 中断线程池的某个任务.
*/
public class InterruptThread implements Runnable {

private int num;

public InterruptThread (int num){
this.num = num;
}

public static void main(String[] args) throws InterruptedException {

Thread interruptThread = new Thread(new InterruptThread(1));
ScheduledFuture<?> t = ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate(interruptThread,0,2,
TimeUnit.SECONDS);

InterruptThread interruptThread1 = new InterruptThread(2);
ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate(interruptThread1,0,2,
TimeUnit.SECONDS);

InterruptThread interruptThread2 = new InterruptThread(3);
ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate(interruptThread2,0,2,
TimeUnit.SECONDS);
Thread.sleep(5000);

//终止正在运行的线程interruptThread
t.cancel(true);
while (true){

}
}

@Override
public void run() {
System.out.println("this is a thread" + num);
}
}

踩坑记录

楼主在使用如下代码时,突然想到当这个定时任务需要被停止时该如何停止线程运行

1
2
ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate(interruptThread,0,2,
TimeUnit.SECONDS);

既然我有这样的需求,那就Google一下吧,找了大半圈,愣是没找到相关资料,都是一些关于Java线程池的深入分析。或者是全局变量啥的,并没有找到令楼主满意的解决方案。

既然没有线程的那就扒一下scheduleAtFixedRate的底层源码看看是什么东西吧,果不其然我在源码中看到了scheduleAtFixedRate方法的具体实现,发现他的返回值是ScheduledFuture。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit) {
if (command == null || unit == null)
throw new NullPointerException();
if (period <= 0)
throw new IllegalArgumentException();
ScheduledFutureTask<Void> sft =
new ScheduledFutureTask<Void>(command,
null,
triggerTime(initialDelay, unit),
unit.toNanos(period));
RunnableScheduledFuture<Void> t = decorateTask(command, sft);
sft.outerTask = t;
delayedExecute(t);
return t;
}

接着往下我们再看看ScheduledFuture里面有什么东西吧,没有让楼主失望,看到了这个

1
2
3
4
5
6
7
8
9
10
11
12
13
public boolean cancel(boolean mayInterruptIfRunning) {
boolean cancelled = super.cancel(mayInterruptIfRunning);
if (cancelled && removeOnCancel && heapIndex >= 0)
remove(this);
return cancelled;
}

//从线程的运行队列中移除当前线程
public boolean remove(Runnable task) {
boolean removed = workQueue.remove(task);
tryTerminate(); // In case SHUTDOWN and now empty
return removed;
}

再往上查super.cancel(mayInterruptIfRunning)是什么东西,我们看到了这个,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//通过调用线程的interrupt方法终止线程运行
public boolean cancel(boolean mayInterruptIfRunning) {
if (!(state == NEW &&
UNSAFE.compareAndSwapInt(this, stateOffset, NEW,
mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
return false;
try { // in case call to interrupt throws exception
if (mayInterruptIfRunning) {
try {
Thread t = runner;
if (t != null)
t.interrupt();
} finally { // final state
UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
}
}
} finally {
finishCompletion();
}
return true;
}

到这里所有的问题都迎刃而解。

总结一下吧

项目中总是会遇到比较难搞的解决方案,当Google不太好找时,翻一下jdk的源码或许也是一个不错的方法。

关注我们

作 者:haifeiWu
原文链接:https://www.hchstudio.cn/article/2018/cc2a/
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。

分享
Java
个人博客折腾记2017年度总结
微信关注我们
分类
  • Android8
  • Go4
  • Java59
  • Kafka,Java1
  • Kotlin2
  • Linux1
  • MapReduce1
  • Python2
  • Raft1
  • Redis1
  • ThreadPoolExecutor1
  • go1
  • 工具1
  • 总结8
  • 旅游日记1
标签
Nginx ChanghuiN haifeiWu Android Java 设计模式 hexo Kotlin 算法 MySQL 源码解析 Python Redis golang web Kafka 配置中心 总结 性能优化 旅游日记 Shell Go 问题排查 译文 Docker Spring Boot 工具 学习笔记 WebFlux 性能测试 go 散列表 源码 netty Raft
最近文章
  • Kafka的日志复制机制
  • 从20到21
  • go 并发编程
  • 【译】了解Linux CPU负载-您何时应该担心?
  • Zookeeper 与分布式锁
  • 基于Redis的分布式锁到底安全吗?
  • 【译】Raft 学生指南
  • ThreadPoolExecutor 的简单梳理
  • MapReduce 的简单实现
  • 使用 Map 实现策略模式
福利专区
    免费SSL证书
      阿里云红包
        腾讯云专属福利
        Copyright © 2021 代码星冰乐. Powered by ChanghuiN. 版权所有 晋ICP备15001365号
        特别感谢: 云服务器服务商 、 CDN 服务商