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