专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

StopWatch任务执行时间监视器


//循环次数 private static int LOOP_COUNT = 10000000; //线程数量 //元素数量 private static int ITEM_COUNT1 = 10; private static Map<String, Long> normaluse() throws InterruptedException { ConcurrentHashMap<String, Long> freqs = new ConcurrentHashMap<>(ITEM_COUNT1); ForkJoinPool forkJoinPool = new ForkJoinPool(THREAD_COUNT); forkJoinPool.execute(() -> IntStream.rangeClosed(1, LOOP_COUNT).parallel().forEach(i -> { //获得一个随机的Key String key = "item" + ThreadLocalRandom.current().nextInt(ITEM_COUNT1); synchronized (freqs) { if (freqs.containsKey(key)) { //Key存在则+1 freqs.put(key, freqs.get(key) + 1); } else { //Key不存在则初始化为1 freqs.put(key, 1L); } } } )); forkJoinPool.shutdown(); forkJoinPool.awaitTermination(1, TimeUnit.HOURS); return freqs; } private static Map<String, Long> gooduse() throws InterruptedException { ConcurrentHashMap<String, LongAdder> freqs = new ConcurrentHashMap<>(ITEM_COUNT1); ForkJoinPool forkJoinPool = new ForkJoinPool(THREAD_COUNT); forkJoinPool.execute(() -> IntStream.rangeClosed(1, LOOP_COUNT).parallel().forEach(i -> { String key = "item" + ThreadLocalRandom.current().nextInt(ITEM_COUNT1); //利用computeIfAbsent()方法来实例化LongAdder,然后利用LongAdder来进行线程安全计数 freqs.computeIfAbsent(key, k -> new LongAdder()).increment(); } )); forkJoinPool.shutdown(); forkJoinPool.awaitTermination(1, TimeUnit.HOURS); //因为我们的Value是LongAdder而不是Long,所以需要做一次转换才能返回 return freqs.entrySet().stream() .collect(Collectors.toMap( e -> e.getKey(), e -> e.getValue().longValue()) ); } public static String good() throws InterruptedException { StopWatch stopWatch = new StopWatch(); stopWatch.start("normaluse"); Map<String, Long> normaluse = normaluse(); stopWatch.stop(); //校验元素数量 Assert.isTrue(normaluse.size() == ITEM_COUNT1, "normaluse size error"); //校验累计总数 Assert.isTrue(normaluse.entrySet().stream() .mapToLong(item -> item.getValue()).reduce(0, Long::sum) == LOOP_COUNT , "normaluse count error"); stopWatch.start("gooduse"); Map<String, Long> gooduse = gooduse(); stopWatch.stop(); Assert.isTrue(gooduse.size() == ITEM_COUNT1, "gooduse size error"); Assert.isTrue(gooduse.entrySet().stream() .mapToLong(item -> item.getValue()) .reduce(0, Long::sum) == LOOP_COUNT , "gooduse count error"); log.info(stopWatch.prettyPrint()); return "OK"; }

执行结果:

-—————————————-
ms % Task name
-—————————————-
01308 082% normaluse
00286 018% gooduse

文章永久链接:https://tech.souyunku.com/19754

未经允许不得转载:搜云库技术团队 » StopWatch任务执行时间监视器

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们