Commit b3a81e83 authored by yanzg's avatar yanzg

Merge branch 'ver1.1' of http://192.168.0.204/yzg/yzg-util

parents 9b7f1ed0 95d52d47
...@@ -4,10 +4,7 @@ import com.yanzuoguang.util.log.Log; ...@@ -4,10 +4,7 @@ import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.thread.ThreadHelper; import com.yanzuoguang.util.thread.ThreadHelper;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
...@@ -20,7 +17,7 @@ public class YzgTimeout { ...@@ -20,7 +17,7 @@ public class YzgTimeout {
public static final int TIME_OUT_DEFAULT = 15 * 1000; public static final int TIME_OUT_DEFAULT = 15 * 1000;
public static final int TIME_OUT_TIP = 10 * 1000; public static final int TIME_OUT_TIP = 10 * 1000;
private static ExecutorService executorService; private static ExecutorService executorService;
private static final Queue<TimeInfo> queueInfos = new ArrayBlockingQueue<>(Integer.MAX_VALUE); private static final Queue<TimeInfo> queueInfos = new ConcurrentLinkedQueue<>();
/** /**
...@@ -74,16 +71,16 @@ public class YzgTimeout { ...@@ -74,16 +71,16 @@ public class YzgTimeout {
try { try {
runnable.run(); runnable.run();
} finally { } finally {
synchronized (timeInfo) { timeInfo.setRun(true);
timeInfo.setRun(true);
}
} }
} }
private static TimeInfo getTimeInfo(int tipOutDefault, int timeOutTip, YzgTimeoutHeart heart) { private static TimeInfo getTimeInfo(int tipOutDefault, int timeOutTip, YzgTimeoutHeart heart) {
init(); init();
TimeInfo timeInfo = new TimeInfo(tipOutDefault, timeOutTip, heart); TimeInfo timeInfo = new TimeInfo(tipOutDefault, timeOutTip, heart);
queueInfos.add(timeInfo); synchronized (queueInfos) {
queueInfos.add(timeInfo);
}
return timeInfo; return timeInfo;
} }
...@@ -106,21 +103,29 @@ public class YzgTimeout { ...@@ -106,21 +103,29 @@ public class YzgTimeout {
} }
private static void runItem() { private static void runItem() {
int size = queueInfos.size(); int size;
synchronized (queueInfos) {
size = queueInfos.size();
}
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
TimeInfo poll = queueInfos.poll(); TimeInfo poll;
synchronized (queueInfos) {
poll = queueInfos.poll();
}
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
long time = end - poll.getStart(); long time = end - poll.getStart();
if (time > poll.getTimeMax()) { if (time > poll.getTimeMax()) {
try { try {
poll.setTimeMax(poll.getTimeMax() + poll.getTimeOutTip()); poll.setTimeMax(poll.getTimeMax() + poll.getTimeOutTip());
executorService.submit(() -> poll.getHeart().heart(time)); executorService.execute(() -> poll.getHeart().heart(time));
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
if (!poll.isRun()) { if (!poll.isRun()) {
queueInfos.add(poll); synchronized (queueInfos) {
queueInfos.add(poll);
}
} }
} }
} }
......
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