From 7f88ade07e411a725153035eb94536a94ac43956 Mon Sep 17 00:00:00 2001 From: kuangcp Date: Wed, 15 May 2024 19:47:57 +0800 Subject: [PATCH] clean --- .../src/main/java/jvm/gc/MxBeanCallback.java | 14 ++++++------ .../java/thread/pool/RecommendUsePool.java | 19 +++++----------- .../thread/pool/RecommendUsePoolTest.java | 22 ++++++++----------- 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/class/src/main/java/jvm/gc/MxBeanCallback.java b/class/src/main/java/jvm/gc/MxBeanCallback.java index 5902fdb0..9a1e2862 100644 --- a/class/src/main/java/jvm/gc/MxBeanCallback.java +++ b/class/src/main/java/jvm/gc/MxBeanCallback.java @@ -21,7 +21,7 @@ public class MxBeanCallback { /** * http://www.fasterj.com/articles/gcnotifs.shtml */ - public static void installGCMonitoring(){ + public static void installGCMonitoring() { //get all the GarbageCollectorMXBeans - there's one for each heap generation //so probably two - the old generation and young generation List gcbeans = java.lang.management.ManagementFactory.getGarbageCollectorMXBeans(); @@ -50,7 +50,7 @@ public void handleNotification(Notification notification, Object handback) { } else if ("end of major GC".equals(gctype)) { gctype = "Old Gen GC"; } - log.info(gctype + ": - " + info.getGcInfo().getId()+ " " + info.getGcName() + " (from " + info.getGcCause()+") "+duration + " milliseconds; start-end times " + info.getGcInfo().getStartTime()+ "-" + info.getGcInfo().getEndTime()); + log.info(gctype + ": - " + info.getGcInfo().getId() + " " + info.getGcName() + " (from " + info.getGcCause() + ") " + duration + " milliseconds; start-end times " + info.getGcInfo().getStartTime() + "-" + info.getGcInfo().getEndTime()); //log.info("GcInfo CompositeType: " + info.getGcInfo().getCompositeType()); //log.info("GcInfo MemoryUsageAfterGc: " + info.getGcInfo().getMemoryUsageAfterGc()); //log.info("GcInfo MemoryUsageBeforeGc: " + info.getGcInfo().getMemoryUsageBeforeGc()); @@ -66,15 +66,15 @@ public void handleNotification(Notification notification, Object handback) { long memMax = memdetail.getMax(); long memUsed = memdetail.getUsed(); MemoryUsage before = membefore.get(name); - long beforepercent = memCommitted==0?0:((before.getUsed()*1000L)/memCommitted); - long percent = memCommitted==0?0:((memUsed*1000L)/memCommitted); //>100% when it gets expanded + long beforepercent = memCommitted == 0 ? 0 : ((before.getUsed() * 1000L) / memCommitted); + long percent = memCommitted == 0 ? 0 : ((memUsed * 1000L) / memCommitted); //>100% when it gets expanded final String memType = memCommitted == memMax ? "(fully expanded)" : "(still expandable)"; - log.info(" "+name + memType +"used: "+(beforepercent/10)+"."+(beforepercent%10)+"%->"+(percent/10)+"."+(percent%10)+"%("+((memUsed/1048576)+1)+"MB) / "); + log.info(" " + name + memType + "used: " + (beforepercent / 10) + "." + (beforepercent % 10) + "%->" + (percent / 10) + "." + (percent % 10) + "%(" + ((memUsed / 1048576) + 1) + "MB) / "); } totalGcDuration += info.getGcInfo().getDuration(); - long percent = totalGcDuration*1000L/info.getGcInfo().getEndTime(); - log.info("GC cumulated overhead "+(percent/10)+"."+(percent%10)+"%"); + long percent = totalGcDuration * 1000L / info.getGcInfo().getEndTime(); + log.info("GC cumulated overhead " + (percent / 10) + "." + (percent % 10) + "%"); } } }; diff --git a/concurrency/src/main/java/thread/pool/RecommendUsePool.java b/concurrency/src/main/java/thread/pool/RecommendUsePool.java index c3f8dc13..37b55de0 100644 --- a/concurrency/src/main/java/thread/pool/RecommendUsePool.java +++ b/concurrency/src/main/java/thread/pool/RecommendUsePool.java @@ -18,15 +18,18 @@ public class RecommendUsePool { /** * 测试拒绝策略 */ - public static ThreadPoolExecutor discardPool; + public static ThreadPoolExecutor discardPool = new ThreadPoolExecutor(2, 5, 1, TimeUnit.MINUTES, + new LinkedBlockingQueue<>(5), new TrackDiscardPolicy()); /** * 测试自定义runnable */ - public static ThreadPoolExecutor taskPool; + public static ThreadPoolExecutor taskPool = new ThreadPoolExecutor(2, 5, 1, TimeUnit.MINUTES, + new LinkedBlockingQueue<>(5), new TrackDiscardPolicy()); /** * 限制最高并发 批量处理任务 */ - public static ThreadPoolExecutor limitPool; + public static ThreadPoolExecutor limitPool = new ThreadPoolExecutor(0, 20, + 60L, TimeUnit.SECONDS, new SynchronousQueue<>()); public static class TrackDiscardPolicy extends ThreadPoolExecutor.DiscardPolicy { private final AtomicInteger counter = new AtomicInteger(); @@ -60,15 +63,5 @@ public void run() { } } - static { - discardPool = new ThreadPoolExecutor(2, 5, 1, TimeUnit.MINUTES, - new LinkedBlockingQueue<>(5), new TrackDiscardPolicy()); - taskPool = new ThreadPoolExecutor(2, 5, 1, TimeUnit.MINUTES, - new LinkedBlockingQueue<>(5), new TrackDiscardPolicy()); - - limitPool = new ThreadPoolExecutor(0, 20, - 60L, TimeUnit.SECONDS, new SynchronousQueue<>()); - } - } diff --git a/concurrency/src/test/java/thread/pool/RecommendUsePoolTest.java b/concurrency/src/test/java/thread/pool/RecommendUsePoolTest.java index c10ef291..825bbbeb 100644 --- a/concurrency/src/test/java/thread/pool/RecommendUsePoolTest.java +++ b/concurrency/src/test/java/thread/pool/RecommendUsePoolTest.java @@ -64,7 +64,7 @@ public void testTaskPool() throws Exception { /** * 测试消费批量任务 * 任务特点:高内存高CPU占用,特殊时段批量创建其他时间较空闲 - * 目标:固定并发数情况下平缓消费 + * 目标:固定并发数的前提下平缓消费,空闲时释放所有线程 *

* 限制内存 -Xmx500m */ @@ -82,24 +82,19 @@ public void testBatchTaskPool() throws Exception { try { log.info("check"); int batch = consumerCnt.incrementAndGet(); - for (int i = 0; i < semaphore.get().availablePermits(); i++) { + + // 如果小任务居多 大任务穿插出现,可以将expect适当调大 提高整体执行效率 + int expect = semaphore.get().availablePermits(); + + for (int i = 0; i < expect; i++) { String task = shardQueue.poll(100, TimeUnit.MILLISECONDS); if (Objects.nonNull(task)) { - // 并发不安全,也就是判断时条件满足,提交任务时条件不满足,会出现任务被丢弃的情况 -// int activeCount = RecommendUsePool.limitPool.getActiveCount(); -// int max = RecommendUsePool.limitPool.getMaximumPoolSize(); -// if (activeCount == max) { -// log.warn("state {} {}", activeCount, max); -// break; -// } -// log.info("state {} {}", activeCount, max); - semaphore.get().acquire(); - // TODO 此处换成MySQL或Redis 实现集群资源跑任务 - + // TODO 此处换成MySQL或Redis 实现集群方式跑任务 RecommendUsePool.limitPool.execute(() -> { try { + // mock byte[] cache = new byte[100 * 1024 * 1024]; TimeUnit.SECONDS.sleep(2 + ThreadLocalRandom.current().nextInt(7)); cache[0] = 2; @@ -116,6 +111,7 @@ public void testBatchTaskPool() throws Exception { } catch (Exception e) { log.error("", e); } + // 实际可能是1min 或 30s 取任务执行耗时的中位数 }, 3, 3, TimeUnit.SECONDS); Blade.create()