博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
测试同步、锁、原子引用的性能
阅读量:4038 次
发布时间:2019-05-24

本文共 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 AtomicReference
ar=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/

你可能感兴趣的文章
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python append 与浅拷贝
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
JVM最简生存指南
查看>>
Java的对象驻留
查看>>
logback高级特性使用(二) 自定义Pattern模板
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
可扩展、高可用服务网络设计方案
查看>>