Commit 8d3d3c20 authored by yanzg's avatar yanzg

升级新版本

parent b7799941
...@@ -4,8 +4,10 @@ import com.yanzuoguang.util.exception.ExceptionHelper; ...@@ -4,8 +4,10 @@ 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.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
...@@ -21,10 +23,12 @@ public class ThreadHelper { ...@@ -21,10 +23,12 @@ public class ThreadHelper {
private static RunPlan interval; private static RunPlan interval;
private static final long SECOND_UNIT = 1000; private static final long SECOND_UNIT = 1000;
private static final long NEXT_SECOND = 5; private static final long NEXT_SECOND = 5;
private static final int THREAD_SIZE = 1000;
/** /**
* 线程对象 * 线程对象
*/ */
private static final ExecutorService executeService = Executors.newCachedThreadPool(); private static final ExecutorService executeService = new ThreadPoolExecutor(1, THREAD_SIZE, 1000,
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(THREAD_SIZE));
/** /**
* 初始化线程对象,需要调用该函数之后才能使用 * 初始化线程对象,需要调用该函数之后才能使用
...@@ -98,25 +102,21 @@ public class ThreadHelper { ...@@ -98,25 +102,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 +210,7 @@ public class ThreadHelper { ...@@ -210,12 +210,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 +219,14 @@ public class ThreadHelper { ...@@ -224,17 +219,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());
} }
}); });
} }
......
...@@ -19,4 +19,11 @@ public class TestTimeout { ...@@ -19,4 +19,11 @@ public class TestTimeout {
// System.err.println("已经等待" + time); // System.err.println("已经等待" + time);
}); });
} }
@Test
public void testQueue() {
for (int i = 0; i < 50000; i++) {
ThreadHelper.runThread(() -> test());
}
}
} }
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