本文共 4598 字,大约阅读时间需要 15 分钟。
代码:
import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicReference;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;import org.openjdk.jmh.annotations.Benchmark;import org.openjdk.jmh.annotations.BenchmarkMode;import org.openjdk.jmh.annotations.Group;import org.openjdk.jmh.annotations.GroupThreads;import org.openjdk.jmh.annotations.Measurement;import org.openjdk.jmh.annotations.Mode;import org.openjdk.jmh.annotations.OutputTimeUnit;import org.openjdk.jmh.annotations.Scope;import org.openjdk.jmh.annotations.State;import org.openjdk.jmh.annotations.Warmup;import org.openjdk.jmh.profile.StackProfiler;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;@Measurement(iterations=5)@Warmup(iterations=5)@BenchmarkMode(Mode.AverageTime)@OutputTimeUnit(TimeUnit.MICROSECONDS)@State(Scope.Group)public class AtomicRefTest { private volatile AtomicReferencear=new AtomicReference<>(new CustomerInfo("FangZhang",0)); private volatile CustomerInfo ci=new CustomerInfo("ZhiZhang",0); private volatile CustomerInfo lci=new CustomerInfo("FanJian",0); private final Lock lock=new ReentrantLock(); public static class CustomerInfo{ private final String name; private int moralIntegrityValue=0; public CustomerInfo(String name,int moralIntegrityValue) { this.name=name; this.moralIntegrityValue=moralIntegrityValue; } public String getName() { return name; } public int getMoralIntegrityValue() { return moralIntegrityValue; } public void setMoralIntegrityValue(int moralIntegrityValue) { this.moralIntegrityValue = moralIntegrityValue; } } @GroupThreads(10) @Group("sync") @Benchmark public void syncCI() { synchronized(this) { ci.setMoralIntegrityValue(ci.getMoralIntegrityValue()+1); } } @GroupThreads(10) @Group("lock") @Benchmark public void lockCI() { lock.lock(); try { lci.setMoralIntegrityValue(lci.getMoralIntegrityValue()+1); }finally { lock.unlock(); } } @GroupThreads(10) @Group("atom") @Benchmark public void atomCI() { final CustomerInfo c=ar.get(); CustomerInfo newCI=new CustomerInfo(c.getName(),c.getMoralIntegrityValue()+1); ar.compareAndSet(c, newCI); } public static void main(String[] args) { Options opt=new OptionsBuilder() .include(AtomicRefTest.class.getSimpleName()) .forks(1) .timeout(TimeValue.seconds(10)) .addProfiler(StackProfiler.class) .build(); try { new Runner(opt).run(); } catch (RunnerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
测试结果:
Result "jmhtest.AtomicRefTest.atom": 0.234 ±(99.9%) 0.021 us/op [Average] (min, avg, max) = (0.226, 0.234, 0.241), stdev = 0.006 CI (99.9%): [0.212, 0.255] (assumes normal distribution)Secondary result "jmhtest.AtomicRefTest.atom:·stack":Stack profiler:....[Thread state distributions].................................................................... 90.0% RUNNABLE 9.1% TIMED_WAITING 0.9% WAITINGResult "jmhtest.AtomicRefTest.lock": 0.203 ±(99.9%) 0.009 us/op [Average] (min, avg, max) = (0.201, 0.203, 0.207), stdev = 0.002 CI (99.9%): [0.195, 0.212] (assumes normal distribution)Secondary result "jmhtest.AtomicRefTest.lock:·stack":Stack profiler:....[Thread state distributions].................................................................... 78.9% WAITING 12.0% RUNNABLE 9.1% TIMED_WAITINGResult "jmhtest.AtomicRefTest.sync": 0.282 ±(99.9%) 0.066 us/op [Average] (min, avg, max) = (0.253, 0.282, 0.295), stdev = 0.017 CI (99.9%): [0.216, 0.347] (assumes normal distribution)Secondary result "jmhtest.AtomicRefTest.sync:·stack":Stack profiler:....[Thread state distributions].................................................................... 69.8% BLOCKED 21.0% RUNNABLE 9.1% TIMED_WAITINGBenchmark Mode Cnt Score Error UnitsAtomicRefTest.atom avgt 5 0.234 ± 0.021 us/opAtomicRefTest.atom:·stack avgt NaN ---AtomicRefTest.lock avgt 5 0.203 ± 0.009 us/opAtomicRefTest.lock:·stack avgt NaN ---AtomicRefTest.sync avgt 5 0.282 ± 0.066 us/opAtomicRefTest.sync:·stack avgt NaN ---
运行结果分析:
1、单纯从同步速度来看,lock方式的同步效率最高,其次是原子引用的方式,最后是synchronized的方式。
2、原子引用的方式虽然是非阻塞的方式,但是同步时的操作步骤明显要比其余两种方式要多,也正是因为步骤多,拖累了同步的运行效率。
3、多次运行后,统计结果仍然符合这种运行规律。
转载地址:http://bhpdi.baihongyu.com/