From 0c0ded951b13df24cde8143aac9420a7a9714456 Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Sun, 19 Jan 2025 15:57:59 +0800 Subject: [PATCH] feat: JMHExample15 --- .../main/java/cn/bunny/jmh/JMHExample14.java | 1 + .../main/java/cn/bunny/jmh/JMHExample15.java | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 multithreading1/src/main/java/cn/bunny/jmh/JMHExample15.java diff --git a/multithreading1/src/main/java/cn/bunny/jmh/JMHExample14.java b/multithreading1/src/main/java/cn/bunny/jmh/JMHExample14.java index 9b95a2f..08d651d 100644 --- a/multithreading1/src/main/java/cn/bunny/jmh/JMHExample14.java +++ b/multithreading1/src/main/java/cn/bunny/jmh/JMHExample14.java @@ -32,6 +32,7 @@ public class JMHExample14 { this.queue = new LinkedBlockingDeque<>(10); } + // 会出现长时间的阻塞最长时间正常是10分钟,在下一章节会对这个进行优化 @GroupThreads(5) @Group("blockingQueue") @Benchmark diff --git a/multithreading1/src/main/java/cn/bunny/jmh/JMHExample15.java b/multithreading1/src/main/java/cn/bunny/jmh/JMHExample15.java new file mode 100644 index 0000000..a10e100 --- /dev/null +++ b/multithreading1/src/main/java/cn/bunny/jmh/JMHExample15.java @@ -0,0 +1,52 @@ +package cn.bunny.jmh; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; +import org.openjdk.jmh.runner.options.TimeValue; + +import java.util.concurrent.BlockingDeque; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@Fork(1) +@Warmup(iterations = 5) +@Measurement(iterations = 5) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Group) +public class JMHExample15 { + private final static int VALUE = Integer.MAX_VALUE; + private BlockingDeque queue; + + public static void main(String[] args) throws RunnerException { + final Options options = new OptionsBuilder() + .include(JMHExample15.class.getSimpleName()) + // 最长批次超时时间不能大于10秒 + .timeout(TimeValue.seconds(10)) + .build(); + new Runner(options).run(); + } + + @Setup + public void init() { + this.queue = new LinkedBlockingDeque<>(10); + } + + // 会出现长时间的阻塞最长时间正常是10分钟,在下一章节会对这个进行优化 + @GroupThreads(5) + @Group("blockingQueue") + @Benchmark + public void put() throws InterruptedException { + this.queue.put(VALUE); + } + + @GroupThreads(5) + @Group("blockingQueue") + @Benchmark + public int take() throws InterruptedException { + return this.queue.take(); + } +}