MultiThread/multithreading_init/src/main/java/jmh/JMHExample05.java

57 lines
1.4 KiB
Java
Raw Normal View History

2025-01-21 21:39:23 +08:00
package jmh;
2025-01-19 12:43:59 +08:00
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 = 5)
@Measurement(iterations = 10)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class JMHExample05 {
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(JMHExample05.class.getSimpleName())
.build();
new Runner(options).run();
}
// 在线程组test在线称中有三个线程不断对Test实例write方法进行调用
@GroupThreads(3)
@Group("test")
@Benchmark
public void testWrite(MyState state) {
state.write();
}
// 在线程组test在线程中有三个线程对testRead方法进行调用
@GroupThreads(3)
@Group("test")
@Benchmark
public void testRead(MyState state) {
state.read();
}
@State(Scope.Benchmark)
public static class MyState {
public MyState() {
System.out.println("create instance");
}
public void write() {
System.out.println("write");
}
public void read() {
System.out.println("read");
}
}
}