diff --git a/multithreading1/src/main/java/cn/bunny/JMHExample04.java b/multithreading1/src/main/java/cn/bunny/JMHExample04.java new file mode 100644 index 0000000..7206211 --- /dev/null +++ b/multithreading1/src/main/java/cn/bunny/JMHExample04.java @@ -0,0 +1,40 @@ +package cn.bunny; + +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 java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@Fork(1) +@Warmup(iterations = 10) +@Measurement(iterations = 10) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Threads(5) +public class JMHExample04 { + public static void main(String[] args) throws RunnerException { + Options options = new OptionsBuilder() + .include(JMHExample04.class.getSimpleName()) + .build(); + new Runner(options).run(); + } + + @Benchmark + public void test(Test test) { + test.method(); + } + + // Test实例会被多个线程共享,只有一份Test + @State(Scope.Benchmark) + public static class Test { + public Test() { + System.out.println("create instance"); + } + + public void method() { + } + } +}