package com.changhong.epc.parsing.service.loop.tools.thread; import com.changhong.epc.parsing.service.loop.tools.Perform; import lombok.extern.slf4j.Slf4j; /** * 增强 Thread 线程 * @author wzx * */ @Slf4j public class MyThread extends Thread{ private boolean isRun = true; private Perform perform; private String threadName; // 睡眠时间 private long sleepTime; // 线程创建的时间 private long startTime ; // 执行一次业务所用的时间 private long onceServiceRunTime; public MyThread(Perform perform, String threadName, long sleepTime) { this.perform = perform; this.threadName = threadName; this.sleepTime = sleepTime; super.setName(this.threadName); } @Override public void run() { startTime = System.currentTimeMillis(); while(isRun){ try { long st = System.currentTimeMillis(); perform.execute(); long et = System.currentTimeMillis(); onceServiceRunTime = et -st; Thread.sleep(sleepTime); } catch (Exception e) { log.error(e.getMessage(), e); try { Thread.sleep(sleepTime); } catch (InterruptedException e1) { log.error(e1.getMessage(), e1); Thread.currentThread().interrupt(); } } } } public boolean isRun() { return isRun; } public void setRun(boolean isRun) { this.isRun = isRun; } /** * 当前线程运行的时间 * @return 毫秒数 */ public long getCurrThreadRunTime() { return System.currentTimeMillis() - startTime; } /** * 获取执行间隔时间 毫秒数 * @return */ public long getSleepTime() { return sleepTime; } /** * 设置执行间隔时间 毫秒数 * @param sleepTime */ public void setSleepTime(long sleepTime) { this.sleepTime = sleepTime; } /** * 获得当前线程名称 * @return */ public String getThreadName() { return threadName; } /** * 获得当前线程启动的时间 * @return */ public long getStartTime() { return startTime; } public void setPerform(Perform perform) { this.perform = perform; } /** * 执行本次业务所用的时间 * @return */ public long getOnceServiceRunTime() { return onceServiceRunTime; } }