From 3150e9c58108953c4d47e49205726d261c9833f1 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Thu, 18 Nov 2021 22:42:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8A=A8=E6=80=81=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=B1=A0=E6=B5=8B=E8=AF=95=E8=BF=90=E8=A1=8C=E6=8A=A5?= =?UTF-8?q?=E8=AD=A6=E6=B5=8B=E8=AF=95=E7=A8=8B=E5=BA=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/inittest/RunStateHandlerTest.java | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/hippo4j-example/src/main/java/cn/hippo4j/example/inittest/RunStateHandlerTest.java b/hippo4j-example/src/main/java/cn/hippo4j/example/inittest/RunStateHandlerTest.java index 20f14377..dec9d03f 100644 --- a/hippo4j-example/src/main/java/cn/hippo4j/example/inittest/RunStateHandlerTest.java +++ b/hippo4j-example/src/main/java/cn/hippo4j/example/inittest/RunStateHandlerTest.java @@ -3,14 +3,12 @@ package cn.hippo4j.example.inittest; import cn.hippo4j.example.constant.GlobalTestConstant; import cn.hippo4j.starter.core.GlobalThreadPoolManage; import cn.hippo4j.starter.wrapper.DynamicThreadPoolWrapper; +import cn.hutool.core.thread.ThreadUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.Random; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; /** * Test run time metrics. @@ -27,32 +25,37 @@ public class RunStateHandlerTest { public void runStateHandlerTest() { log.info("Test thread pool runtime state interface, The rejection policy will be triggered after 30s..."); - ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); - scheduledThreadPool.scheduleAtFixedRate(() -> { - DynamicThreadPoolWrapper poolWrapper = GlobalThreadPoolManage.getExecutorService(GlobalTestConstant.MESSAGE_PRODUCE); - ThreadPoolExecutor pool = poolWrapper.getPool(); - try { - pool.execute(() -> { - log.info("Thread pool name :: {}, Executing incoming blocking...", Thread.currentThread().getName()); - try { - int maxRandom = 10; - int temp = 2; - Random random = new Random(); - // Assignment thread pool completedTaskCount - if (random.nextInt(maxRandom) % temp == 0) { - Thread.sleep(10241024); - } else { - Thread.sleep(3000); + new Thread(() -> { + ThreadUtil.sleep(5000); + for (int i = 0; i < Integer.MAX_VALUE; i++) { + DynamicThreadPoolWrapper poolWrapper = GlobalThreadPoolManage.getExecutorService(GlobalTestConstant.MESSAGE_PRODUCE); + ThreadPoolExecutor pool = poolWrapper.getPool(); + try { + pool.execute(() -> { + log.info("Thread pool name :: {}, Executing incoming blocking...", Thread.currentThread().getName()); + try { + int maxRandom = 10; + int temp = 2; + Random random = new Random(); + // Assignment thread pool completedTaskCount + if (random.nextInt(maxRandom) % temp == 0) { + Thread.sleep(10241024); + } else { + Thread.sleep(3000); + } + } catch (InterruptedException e) { + // ignore } - } catch (InterruptedException e) { - // ignore - } - }); - } catch (Exception ex) { - // ignore + }); + } catch (Exception ex) { + // ignore + } + + log.info(" >>> Number of dynamic thread pool tasks executed :: {}", i); + ThreadUtil.sleep(500); } - }, 5, 2, TimeUnit.SECONDS); + }).start(); } }