LongAdder与AtomicInteger并发性能的简单比较测试
来源:互联网 发布:php usleep 性能 编辑:程序博客网 时间:2024/06/13 01:38
测试代码如下:
package lhever.JVM;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.atomic.AtomicLong;import java.util.concurrent.atomic.LongAdder;public class AtomicTest{ public static final int MAX_THREAD_COUNT = 10; private static final int TASK_COUNT = 10; private static final int TARGET_COUNT = 100000000; private AtomicLong atomicLongVal = new AtomicLong(0); private LongAdder longAdderVal = new LongAdder(); private static CountDownLatch latchForAtomicLong = new CountDownLatch(TASK_COUNT); private static CountDownLatch latchForLongAdder = new CountDownLatch(TASK_COUNT); public class AtomicLongThread implements Runnable { protected String name; protected long startTime; public AtomicLongThread(long startTime) { this.startTime = startTime; } @Override public void run() { long v = atomicLongVal.get(); while(v < TARGET_COUNT) { v = atomicLongVal.incrementAndGet(); } long endTime = System.currentTimeMillis(); System.out.println("AtomicLongThread Spend:" + (endTime - startTime) + "ms, v = " + v); latchForAtomicLong.countDown(); } } public class LongAddderThread implements Runnable { protected String name; protected long startTime; public LongAddderThread(long startTime) { this.startTime = startTime; } @Override public void run() { long v = longAdderVal.sum(); while(v < TARGET_COUNT) { longAdderVal.increment(); v = longAdderVal.sum(); } long endTime = System.currentTimeMillis(); System.out.println("LongAddderThread Spend:" + (endTime - startTime) + "ms, v = " + v); latchForLongAdder.countDown(); } } public void testAtomicLongThread() throws InterruptedException { ExecutorService exe = Executors.newFixedThreadPool(MAX_THREAD_COUNT); long startTime = System.currentTimeMillis(); AtomicLongThread atomicIntegerThread = new AtomicLongThread(startTime); for(int i = 0; i < TASK_COUNT; i++) { exe.submit(atomicIntegerThread); } latchForAtomicLong.await(); exe.shutdown(); } public void testLongAdderThread() throws InterruptedException { ExecutorService exe = Executors.newFixedThreadPool(MAX_THREAD_COUNT); long startTime = System.currentTimeMillis(); LongAddderThread longAddderThread = new LongAddderThread(startTime); for(int i = 0; i < TASK_COUNT; i++) { exe.submit(longAddderThread); } latchForLongAdder.await(); exe.shutdown(); } /** * 下面的测试表明,AtomicLong的并发性能比LongAdder要差,在线程竞争越剧烈的场合,其表现出来的性能会更加优越 * @param args * @throws InterruptedException * @author lhever 2017年4月4日 下午11:10:06 * @since v1.0 */ public static void main(String[] args) throws InterruptedException { /** * MAX_THREAD_COUNT、TASK_COUNT的值都为3时候测试结果如下: */ AtomicTest test = new AtomicTest(); test.testAtomicLongThread(); test.testLongAdderThread(); /* 在本人的win7 64位系统 运行结果如下 AtomicLongThread Spend:2258ms, v = 100000001 AtomicLongThread Spend:2258ms, v = 100000002 AtomicLongThread Spend:2258ms, v = 100000000 LongAddderThread Spend:2155ms, v = 100000000 LongAddderThread Spend:2155ms, v = 100000000 LongAddderThread Spend:2155ms, v = 100000000 */ /** * MAX_THREAD_COUNT、TASK_COUNT的值都为10时候测试结果如下: */ AtomicTest test1 = new AtomicTest(); test1.testAtomicLongThread(); test1.testLongAdderThread(); /* 在本人的win7 64位系统 运行结果如下 AtomicLongThread Spend:2889ms, v = 100000005 AtomicLongThread Spend:2890ms, v = 100000002 AtomicLongThread Spend:2890ms, v = 100000006 AtomicLongThread Spend:2889ms, v = 100000003 AtomicLongThread Spend:2889ms, v = 100000004 AtomicLongThread Spend:2890ms, v = 100000007 AtomicLongThread Spend:2889ms, v = 100000000 AtomicLongThread Spend:2889ms, v = 100000001 AtomicLongThread Spend:2891ms, v = 100000008 AtomicLongThread Spend:2892ms, v = 100000009 LongAddderThread Spend:2054ms, v = 100000005 LongAddderThread Spend:2054ms, v = 100000003 LongAddderThread Spend:2054ms, v = 100000003 LongAddderThread Spend:2054ms, v = 100000007 LongAddderThread Spend:2054ms, v = 100000002 LongAddderThread Spend:2054ms, v = 100000006 LongAddderThread Spend:2054ms, v = 100000007 LongAddderThread Spend:2054ms, v = 100000004 LongAddderThread Spend:2054ms, v = 100000003 LongAddderThread Spend:2057ms, v = 100000008 */ }}
0 0
- LongAdder与AtomicInteger并发性能的简单比较测试
- synchronized和AtomicInteger解决并发问题的性能比较
- AtomicInteger的并发处理
- AtomicInteger的并发处理
- AtomicInteger的并发处理
- AtomicInteger的并发处理
- JDK 8新特性LongAdder和AtomicLong的性能测试对比
- Zend_Db 与 Adodb 的性能测试比较
- java并发包-AtomicInteger的并发处理
- Java并发控制synchronized与AtomicInteger类
- 关于C#中for与foreach的性能比较测试
- java中Json工具的用法比较与性能测试
- Java并发包-浅析LongAdder
- 对于Synchronized、ReentrantLock、Atomic、CAS在并发下面的性能比较测试
- 性能测试与并发用户访问时间
- MixPHP 与 Phalcon 数据库并发性能测试
- LongAdder的实现以及与AtomicLong的对比
- 内存数据库单表并发更新性能测试比较
- uva10487/lower_bound使用
- JAVA求学之路第十一天(继承&抽象&final)
- 【书山有路】SQL必知必会 第12课
- HDOJ 2846 Repository
- poj2084Game of Connections 卡特兰数
- LongAdder与AtomicInteger并发性能的简单比较测试
- php基础知识
- mysql sql语句调优及,索引总结
- Two Sum
- ORACLE 在线移动redo log路径、删除无效的redo log
- 理解数据结构
- leetcode 506. Relative Ranks
- 初级数据结构简总结
- JAVA求学之路第十二天(接口)