修改测试用例显示创建线程, 改为使用线程池来创建 (#194)

pull/200/head
zhz 2 years ago
parent 246ba7f5b8
commit ae59658699

@ -9,7 +9,9 @@ import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static cn.hippo4j.common.constant.Constants.EXECUTE_TIMEOUT_TRACE;
@ -29,6 +31,20 @@ public class RunStateHandlerTest {
@Resource
private ThreadPoolExecutor messageProduceDynamicThreadPool;
private final ThreadPoolExecutor runStateHandlerTestExecutor = new ThreadPoolExecutor(
2,
2,
0L,
TimeUnit.MILLISECONDS,
new SynchronousQueue<>(),
r -> {
Thread t = new Thread(r);
t.setName("client.example.runStateHandler.test");
t.setDaemon(true);
return t;
},
new ThreadPoolExecutor.AbortPolicy());
@PostConstruct
@SuppressWarnings("all")
public void runStateHandlerTest() {
@ -43,7 +59,7 @@ public class RunStateHandlerTest {
private void runTask(ExecutorService executorService) {
// 模拟任务运行
new Thread(() -> {
runStateHandlerTestExecutor.execute(() -> {
/**
* 线, MDC Trace , .
*/
@ -73,7 +89,7 @@ public class RunStateHandlerTest {
ThreadUtil.sleep(500);
}
}).start();
});
}
}

@ -9,7 +9,9 @@ import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;
import org.springframework.stereotype.Component;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* TaskDecorator test.
@ -23,12 +25,26 @@ public class TaskDecoratorTest {
public static final String PLACEHOLDER = "site";
private final ThreadPoolExecutor taskDecoratorTestExecutor = new ThreadPoolExecutor(
1,
1,
0L,
TimeUnit.MILLISECONDS,
new SynchronousQueue<>(),
r -> {
Thread t = new Thread(r);
t.setName("client.example.taskDecorator.test");
t.setDaemon(true);
return t;
},
new ThreadPoolExecutor.AbortPolicy());
/**
* 线 {@link TaskDecorator}
* , @PostConstruct
*/
public void taskDecoratorTest() {
new Thread(() -> {
taskDecoratorTestExecutor.execute(() -> {
MDC.put(PLACEHOLDER, "查看官网: https://www.hippox.cn");
ThreadUtil.sleep(5000);
DynamicThreadPoolWrapper poolWrapper = GlobalThreadPoolManage.getExecutorService(GlobalTestConstant.MESSAGE_PRODUCE);
@ -40,7 +56,7 @@ public class TaskDecoratorTest {
*/
log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER));
});
}).start();
});
}

Loading…
Cancel
Save