Commit 8d3d3c20 authored by yanzg's avatar yanzg

升级新版本

parent b7799941
......@@ -4,8 +4,10 @@ import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.DateHelper;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
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;
/**
......@@ -21,10 +23,12 @@ public class ThreadHelper {
private static RunPlan interval;
private static final long SECOND_UNIT = 1000;
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,10 +102,7 @@ public class ThreadHelper {
* 监控线程的方法,防止线程执行死机
*/
private static void startMonitor() {
runThread(new Runnable() {
@Override
public void run() {
runThread(() -> {
do {
if (threadIsRun && ((System.currentTimeMillis() - threadDate.getTime()) / SECOND_UNIT) > NEXT_SECOND) {
try {
......@@ -116,7 +117,6 @@ public class ThreadHelper {
}
sleep(1000 * 60);
} while (true);
}
});
}
......@@ -210,12 +210,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,9 +219,7 @@ public class ThreadHelper {
* @param run
*/
public static void runThread(final RunInterval run) {
runThread(new Runnable() {
@Override
public void run() {
runThread(() -> {
while (!run.isBreakFlag()) {
try {
run.getCode().run();
......@@ -235,7 +228,6 @@ public class ThreadHelper {
}
sleep(run.getTime());
}
}
});
}
......
......@@ -19,4 +19,11 @@ public class TestTimeout {
// 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