本文使用:

  Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,

但不能在指定时间运行。一般用的较少。

类似于quartz任务调度:demo地址 lsr-core-base模块中

直接上代码:

线程基类:

package cn.lsr.core.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.sql.Timestamp;
import java.util.Timer;
import java.util.TimerTask; /**
* @Description: 轮询线程基类
* @Package: lsr-microservice
* @author: Hacker_lsr@126.com
**/
public abstract class AbstractPollThread {
private static final Logger log = LoggerFactory.getLogger(AbstractPollThread.class);
private long delay = 60; // 延期时间(第一次执行时间-调用时间)
private long timeInterval = 60; //时间间隔(s)
private Timer timer;
private Timestamp lastProcess = null; //上次执行的时间
private boolean isTimerStart = false; //定时器是否启动
private boolean isOnProcessing = false; //是否在执行处理
private final String threadName; //线程名称
private final String threadId; //线程标识
private final String longname; //中文名称
private long processCount = 0; //执行次数
private long processTime = 0; //已运行时间
private long errorProcessCount = 0; //执行失败的次数 public AbstractPollThread(String threadId, String threadName, String longname, Long delay, Long timeInterval) {
this.threadId = threadId;
this.threadName = threadName;
this.longname = longname;
if (timeInterval != null && timeInterval > 0)
this.timeInterval = timeInterval; if (delay != null)
this.delay = delay;
PollThreadManager.get().register(threadName, this);
}
/**
* 线程启动时回调方法。
*/
public void init() { } /**
* 轮询间隔回调方法。
*/
public abstract void process(); /**
* 启动方法
*/
public void startup() {
this.timer = new Timer( "lsr-timer-" + this.threadName, false);
try {
this.timer.schedule(new TimerTask() {
boolean initialized = false;
@Override
public void run() {
// 异步线程资源清理
//清理完上下文数据,在打log4j标记
log.info("--------------------轮训线程[" + AbstractPollThread.this.threadName + "]开始调度--------------------");
// 2015.4.20 每个线程开始受理请求时对数据库连接进行检查,若不可用则重连一次
long start = System.currentTimeMillis();
try {
AbstractPollThread.this.isOnProcessing = true;
AbstractPollThread.this.lastProcess = new Timestamp(System.currentTimeMillis());
AbstractPollThread.this.process();
AbstractPollThread.this.isOnProcessing = false;
}
catch (Throwable t) {
log.error("轮询线程出现异常", t);
AbstractPollThread.this.errorProcessCount++;
} finally {
AbstractPollThread.this.processCount++;
AbstractPollThread.this.processTime = AbstractPollThread.this.processTime + System.currentTimeMillis() - start;
log.info("--------------------轮训线程[" + AbstractPollThread.this.threadName + "]调度结束. [" + (System.currentTimeMillis() - start) + "]--------------------");
// 异步线程资源清理
}
}
}, this.delay * 1000, this.timeInterval * 1000);
this.isTimerStart = true;
} catch (Exception e) {
this.isTimerStart = false;
log.error("轮询线程设置定时器失败,", e);
throw new RuntimeException("轮询线程设置定时器失败", e);
}
} public void shutdown() {
try {
if (this.timer != null)
this.timer.cancel();
this.isTimerStart = false;
} catch (Exception e) {
this.isTimerStart = false;
log.error("关闭轮询线程中的定时器失败", e);
throw new RuntimeException("关闭轮询线程中的定时器失败", e);
}
} public String getThreadName() {
return this.threadName;
} public String getLongname() {
return this.longname;
} public long getProcessCount() {
return this.processCount;
} public long getProcessTime() {
return this.processTime;
} public long getErrorProcessCount() {
return this.errorProcessCount;
} public void setTimeInterval(long timeInterval) {
this.timeInterval = timeInterval;
} public long getTimeInterval() {
return this.timeInterval;
} public boolean isTimerStart() {
return this.isTimerStart;
} public boolean isOnProcessing() {
return this.isOnProcessing;
} public String getLastProcessTime() {
return this.lastProcess == null ? null : this.lastProcess.toString();
} public void resetCountInfo() {
this.processCount = 0;
this.processTime = 0;
this.errorProcessCount = 0;
this.lastProcess = null;
} }

轮询线程配置类:

package cn.lsr.core.thread;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; /**
* @Description: 轮询线程配置
* @Package: lsr-microservice
* @author: Hacker_lsr@126.com
**/
public class PollThreadConfig {
/**
* 轮询线程ID
*/
private String threadId; /**
* 轮询线程名称
*/
private String threadName; /**
* 间隔时间
*/
private Long timeInterval; /**
* 轮询线程首次延迟时间
*/
private Long delay; // get set 省略 }

线程管理器:

package cn.lsr.core.thread;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; /**
* @Description: 线程管理器
* @Package: lsr-microservice
* @author: Hacker_lsr@126.com
**/
public class PollThreadManager {
private final static Map<String, AbstractPollThread> pollThreadMap = new ConcurrentHashMap<String, AbstractPollThread>(); private final static PollThreadManager instance = new PollThreadManager(); public static PollThreadManager get() {
return instance;
} public void register(String threadName, AbstractPollThread pollThread) {
pollThreadMap.put(threadName, pollThread);
} public AbstractPollThread getPollThread(String threadName) {
return pollThreadMap.get(threadName);
} public Map<String, AbstractPollThread> getPollThreads() {
return pollThreadMap;
}
}

测试线程(可扩展):

package cn.lsr.core.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.sql.Timestamp; /**
* @Description: 测试轮询线程
* @Package: lsr-microservice
* @author: Hacker_lsr@126.com
**/
public class TestServerPollThread extends AbstractPollThread{
private static final Logger log = LoggerFactory.getLogger(TestServerPollThread.class);
/**
* 插件 - OnlineServerManagerPlugin -启动的时候初始化线程
* @param threadId 轮询线程ID
* @param threadName 轮询线程名称
* @param longname 中文名称
* @param delay 轮询线程首次延迟时间
* @param timeInterval 时间间隔
*/
public TestServerPollThread(String threadId, String threadName, String longname, Long delay, Long timeInterval) {
super(threadId, threadName, longname, delay, timeInterval);
} /**
* 轮询间隔回调方法
*/
@Override
public void process() {
log.info("刷新时间为:{}", new Timestamp(System.currentTimeMillis()));
//逻辑
}
}

使用:

//注入:
private TestServerPollThread testServerPollThread;
// 封装的工具类,获取的ioc的线程配置
PollThreadConfig pollThreadConfig = SpringUtil.getBean(PollThreadConfig.class);
testServerPollThread = new TestServerPollThread(pollThreadConfig.getThreadId(),pollThreadConfig.getThreadName(),pollThreadConfig.getThreadName(),pollThreadConfig.getDelay(),pollThreadConfig.getTimeInterval());
//调用方法
testServerPollThread.startup();

application.properties:

##################################### 轮询线程 #######################################
#开关
lsr.poll.thread.enabled=true
#线程id
lsr.poll.thread.threadId=LSRThread
#线程中文名
lsr.poll.thread.threadName=轮询线程
#间隔时间
lsr.poll.thread.timeInterval=60
#加载延迟时间
lsr.poll.thread.delay=60

最新文章

  1. 解读ASP.NET 5 &amp; MVC6系列(14):View Component
  2. cssReset - css初始化
  3. C#多线程网摘 2
  4. 让JAVA代码跑得更快
  5. C# 格式化字符串(转载)
  6. 【操作教程】利用YCSB测试巨杉数据库性能
  7. [USACO13NOV]没有找零No Change [TPLY]
  8. 超级有爱的五款APP共享 可以让你神操作
  9. 网页中嵌入google地图
  10. UOJ.35.[模板]后缀排序(后缀数组 倍增)
  11. Linux基本的命令使用2018-4-20 18:47:28
  12. 一、Ansible安装
  13. 1安装Linux
  14. Scoop Windows 的命令行安装程序管理工具
  15. &quot;Linux内核分析&quot;第六周实验报告
  16. java框架篇---hibernate之CRUD操作
  17. px em rem 区别
  18. latex安装及基本使用
  19. Mac下安装和卸载MySQL(含配置)
  20. SharePoint Excel Service-PowerShell

热门文章

  1. linux异常 - 网卡故障
  2. [AtCoder]Grand Contest 028
  3. 解决windows10 OBS Studioobsstudio显示器捕获黑屏
  4. 【牛客小白月赛21】NC201605 Bits
  5. 【算法学习记录-排序题】【PAT A1012】The Best Rank
  6. 图的最小生成树prim算法模板
  7. 转载:EQ
  8. 关于我 &amp; 关于这个博客
  9. 情人节用Python智能聊天机器人的实现|制作一个虚拟恋人
  10. 如何用navicat连接linux服务器上的mysql以及重启服务