Commit 6eca5cec authored by yanzg's avatar yanzg

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

 Conflicts:
	yzg-util-base/src/main/java/com/yanzuoguang/util/helper/YzgTimeout.java
parents bdf06be4 72a368a2
...@@ -4,7 +4,7 @@ import com.yanzuoguang.util.log.Log; ...@@ -4,7 +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.*; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
...@@ -16,8 +16,7 @@ public class YzgTimeout { ...@@ -16,8 +16,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 Queue<TimeInfo> queueInfos = null;
private static final Queue<TimeInfo> queueInfos = new ConcurrentLinkedQueue<>();
/** /**
...@@ -85,18 +84,16 @@ public class YzgTimeout { ...@@ -85,18 +84,16 @@ public class YzgTimeout {
} }
private static void init() { private static void init() {
if (executorService != null) { if (queueInfos != null) {
return; return;
} }
synchronized (YzgTimeout.class) { synchronized (YzgTimeout.class) {
if (executorService != null) { if (queueInfos != null) {
return; return;
} }
executorService = new ThreadPoolExecutor(1, 10, 1000, queueInfos = new ConcurrentLinkedQueue<>();
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10));
ThreadHelper.runThread(() -> { ThreadHelper.runThread(() -> {
while (true) { while (true) {
try { try {
...@@ -128,7 +125,7 @@ public class YzgTimeout { ...@@ -128,7 +125,7 @@ public class YzgTimeout {
if (time > poll.getTimeMax()) { if (time > poll.getTimeMax()) {
try { try {
poll.setTimeMax(poll.getTimeMax() + poll.getTimeOutTip()); poll.setTimeMax(poll.getTimeMax() + poll.getTimeOutTip());
executorService.execute(() -> poll.getHeart().heart(time)); ThreadHelper.runThread(() -> poll.getHeart().heart(time));
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
......
...@@ -4,8 +4,7 @@ import com.yanzuoguang.util.exception.ExceptionHelper; ...@@ -4,8 +4,7 @@ import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.DateHelper; import com.yanzuoguang.util.helper.DateHelper;
import java.util.Date; import java.util.Date;
import java.util.concurrent.ExecutorService; import java.util.concurrent.*;
import java.util.concurrent.Executors;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
...@@ -98,25 +97,21 @@ public class ThreadHelper { ...@@ -98,25 +97,21 @@ public class ThreadHelper {
* 监控线程的方法,防止线程执行死机 * 监控线程的方法,防止线程执行死机
*/ */
private static void startMonitor() { private static void startMonitor() {
runThread(new Runnable() { runThread(() -> {
do {
@Override if (threadIsRun && ((System.currentTimeMillis() - threadDate.getTime()) / SECOND_UNIT) > NEXT_SECOND) {
public void run() { try {
do { if (threadIsRun) {
if (threadIsRun && ((System.currentTimeMillis() - threadDate.getTime()) / SECOND_UNIT) > NEXT_SECOND) { threadIsRun = false;
try {
if (threadIsRun) {
threadIsRun = false;
}
} catch (Exception ex) {
ExceptionHelper.handleException(ThreadHelper.class, ex);
} }
threadIsRun = false; } catch (Exception ex) {
startThread(); ExceptionHelper.handleException(ThreadHelper.class, ex);
} }
sleep(1000 * 60); threadIsRun = false;
} while (true); startThread();
} }
sleep(1000 * 60);
} while (true);
}); });
} }
...@@ -210,12 +205,7 @@ public class ThreadHelper { ...@@ -210,12 +205,7 @@ public class ThreadHelper {
* @param run * @param run
*/ */
public static void runThread(final Runnable run) { public static void runThread(final Runnable run) {
executeService.execute(new Runnable() { executeService.execute(() -> executeCatch(run));
@Override
public void run() {
executeCatch(run);
}
});
} }
/** /**
...@@ -224,17 +214,14 @@ public class ThreadHelper { ...@@ -224,17 +214,14 @@ public class ThreadHelper {
* @param run * @param run
*/ */
public static void runThread(final RunInterval run) { public static void runThread(final RunInterval run) {
runThread(new Runnable() { runThread(() -> {
@Override while (!run.isBreakFlag()) {
public void run() { try {
while (!run.isBreakFlag()) { run.getCode().run();
try { } catch (Exception ex) {
run.getCode().run(); ExceptionHelper.handleException(ThreadHelper.class, ex);
} catch (Exception ex) {
ExceptionHelper.handleException(ThreadHelper.class, ex);
}
sleep(run.getTime());
} }
sleep(run.getTime());
} }
}); });
} }
......
...@@ -6,17 +6,24 @@ import org.junit.Test; ...@@ -6,17 +6,24 @@ import org.junit.Test;
public class TestTimeout { public class TestTimeout {
public static final int WAIT_TIME = 1 * 60 * 1000;
@Test @Test
public void test() { public void test() {
YzgTimeout.timeOut(TestTimeout.class, "消息", new Runnable() { YzgTimeout.timeOut(TestTimeout.class, "消息", () -> {
@Override System.out.println("开始运行");
public void run() { ThreadHelper.sleep(WAIT_TIME);
System.out.println("开始运行"); System.out.println("结束运行");
ThreadHelper.sleep(1 * 60 * 1000);
System.out.println("结束运行");
}
}, (time) -> { }, (time) -> {
// System.err.println("已经等待" + time); // System.err.println("已经等待" + time);
}); });
} }
@Test
public void testQueue() {
for (int i = 0; i < 5000; i++) {
ThreadHelper.runThread(() -> test());
}
ThreadHelper.sleep(WAIT_TIME * 2);
}
} }
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