Commit 552c2c68 authored by yanzg's avatar yanzg

修复实体关系

parent 6b71ce70
......@@ -95,23 +95,32 @@ public class YzgTimeout {
}
}
ThreadHelper.runThread(() -> {
try {
long timeMax = tipOutDefault;
long start = System.currentTimeMillis();
do {
long end = System.currentTimeMillis();
long time = end - start;
if (time > timeMax) {
timeMax += timeOutTip;
heart.heart(time);
}
ThreadHelper.sleep(tipUnit);
} while (!isRun.value);
} catch (Exception ex) {
ex.printStackTrace();
while (true) {
runItem();
ThreadHelper.sleep(1000);
}
});
}
private static void runItem() {
int size = queueInfos.size();
for (int i = 0; i < size; i++) {
TimeInfo poll = queueInfos.poll();
long end = System.currentTimeMillis();
long time = end - poll.getStart();
if (time > poll.getTimeMax()) {
try {
poll.setTimeMax(poll.getTimeMax() + poll.getTimeOutTip());
executorService.submit(() -> poll.getHeart().heart(time));
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (!poll.isRun()) {
queueInfos.add(poll);
}
}
}
}
class TimeInfo {
......@@ -119,12 +128,16 @@ class TimeInfo {
private int timeOutDefault;
private int timeOutTip;
private YzgTimeoutHeart heart;
private boolean isRun;
private boolean run;
private long start;
private int timeMax;
public TimeInfo(int timeOutDefault, int timeOutTip, YzgTimeoutHeart heart) {
this.timeOutDefault = timeOutDefault;
this.timeOutTip = timeOutTip;
this.heart = heart;
this.timeMax = timeOutDefault;
this.start = System.currentTimeMillis();
}
public int getTimeOutDefault() {
......@@ -152,10 +165,26 @@ class TimeInfo {
}
public boolean isRun() {
return isRun;
return run;
}
public void setRun(boolean run) {
isRun = run;
this.run = run;
}
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
public int getTimeMax() {
return timeMax;
}
public void setTimeMax(int timeMax) {
this.timeMax = timeMax;
}
}
package helper;
import com.yanzuoguang.util.helper.YzgTimeout;
import com.yanzuoguang.util.thread.ThreadHelper;
import org.junit.Test;
public class TestTimeout {
@Test
public void test() {
YzgTimeout.timeOut(TestTimeout.class, "消息", new Runnable() {
@Override
public void run() {
System.out.println("开始运行");
ThreadHelper.sleep(1 * 60 * 1000);
System.out.println("结束运行");
}
}, (time) -> {
// System.err.println("已经等待" + time);
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment