Commit 552c2c68 authored by yanzg's avatar yanzg

修复实体关系

parent 6b71ce70
...@@ -95,22 +95,31 @@ public class YzgTimeout { ...@@ -95,22 +95,31 @@ public class YzgTimeout {
} }
} }
ThreadHelper.runThread(() -> { ThreadHelper.runThread(() -> {
try { while (true) {
long timeMax = tipOutDefault; runItem();
long start = System.currentTimeMillis(); ThreadHelper.sleep(1000);
do {
long end = System.currentTimeMillis();
long time = end - start;
if (time > timeMax) {
timeMax += timeOutTip;
heart.heart(time);
} }
ThreadHelper.sleep(tipUnit); });
} while (!isRun.value); }
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) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
}); }
if (!poll.isRun()) {
queueInfos.add(poll);
}
}
} }
} }
...@@ -119,12 +128,16 @@ class TimeInfo { ...@@ -119,12 +128,16 @@ class TimeInfo {
private int timeOutDefault; private int timeOutDefault;
private int timeOutTip; private int timeOutTip;
private YzgTimeoutHeart heart; private YzgTimeoutHeart heart;
private boolean isRun; private boolean run;
private long start;
private int timeMax;
public TimeInfo(int timeOutDefault, int timeOutTip, YzgTimeoutHeart heart) { public TimeInfo(int timeOutDefault, int timeOutTip, YzgTimeoutHeart heart) {
this.timeOutDefault = timeOutDefault; this.timeOutDefault = timeOutDefault;
this.timeOutTip = timeOutTip; this.timeOutTip = timeOutTip;
this.heart = heart; this.heart = heart;
this.timeMax = timeOutDefault;
this.start = System.currentTimeMillis();
} }
public int getTimeOutDefault() { public int getTimeOutDefault() {
...@@ -152,10 +165,26 @@ class TimeInfo { ...@@ -152,10 +165,26 @@ class TimeInfo {
} }
public boolean isRun() { public boolean isRun() {
return isRun; return run;
} }
public void setRun(boolean 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