Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuangcp committed May 15, 2024
1 parent 42f704d commit 7f88ade
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
14 changes: 7 additions & 7 deletions class/src/main/java/jvm/gc/MxBeanCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<GarbageCollectorMXBean> gcbeans = java.lang.management.ManagementFactory.getGarbageCollectorMXBeans();
Expand Down Expand Up @@ -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());
Expand All @@ -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) + "%");
}
}
};
Expand Down
19 changes: 6 additions & 13 deletions concurrency/src/main/java/thread/pool/RecommendUsePool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<>());
}


}
22 changes: 9 additions & 13 deletions concurrency/src/test/java/thread/pool/RecommendUsePoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testTaskPool() throws Exception {
/**
* 测试消费批量任务
* 任务特点:高内存高CPU占用,特殊时段批量创建其他时间较空闲
* 目标:固定并发数情况下平缓消费
* 目标:固定并发数的前提下平缓消费,空闲时释放所有线程
* <p>
* 限制内存 -Xmx500m
*/
Expand All @@ -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;
Expand All @@ -116,6 +111,7 @@ public void testBatchTaskPool() throws Exception {
} catch (Exception e) {
log.error("", e);
}
// 实际可能是1min 或 30s 取任务执行耗时的中位数
}, 3, 3, TimeUnit.SECONDS);

Blade.create()
Expand Down

0 comments on commit 7f88ade

Please sign in to comment.