zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;
    }
    
    
 
    
    
}