阅读背景:

java 原子+1 性能对比 performance

来源:互联网 
package thread; import java.lang.reflect.Field; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.LongAdder; import sun.misc.Unsafe; @SuppressWarnings("restriction") public class Sy2 { // 测试规模,调用一次getAndIncreaseX视作提供一次业务服务,记录提供TEST_SIZE次服务的耗时 private static final int TEST_SIZE = 100000000; // 客户线程数 private static final int THREAD_COUNT = 10; // 使用CountDownLatch让各线程同时开始 private CountDownLatch cdl = new CountDownLatch(THREAD_COUNT + 1); private int n = 0; private AtomicInteger ai = new AtomicInteger(0); private static LongAdder longAdder = new LongAdder(); private long startTime; private static volatile int value = 0; private static long casOffset; public static Unsafe unsafe; static { try { Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafe.setAccessible(true); unsafe = (Unsafe) theUnsafe.get(null); // initial unsafe // get field offset by unsafe casOffset = unsafe.staticFieldOffset(Sy2.class.getDeclaredField("value")); } catch (Exception e) { e.printStackTrace(); } } public void init() { startTime = System.nanoTime(); } public int cas() { while (true) { if (unsafe.compareAndSwapInt(Sy2.class, casOffset, value, value + 1)) return value; } } /** * 使用AtomicInteger.getAndIncrement,测试结果为1.8比1.7有明显性能提升 * * @return */ private final int getAndIncreaseA() { int result = ai.getAndIncrement(); if (result == TEST_SIZE) { System.out.println(System.nanoTime() - startTime); System.exit(0); } return result; } /** * 使用synchronized来完成同步,测试结果为1.7和1.8几乎无性能差别 * * @return */ private final int getAndIncreaseB() { int result; synchronized (this) { result = n++; } if (result == TEST_SIZE) { System.out.println(System.nanoTime() - startTime); System.exit(0); } return result; } /** * 使用AtomicInteger.compareAndSet在java代码层面做失败重试(与1.7的AtomicInteger.getAndIncrement的实现类似), * 测试结果为1.7和1.8几乎无性能差别 * * @return */ private final int getAndIncreaseC() { int result; do { result = ai.get(); } while (!ai.compareAndSet(result, result + 1)); if (result == TEST_SIZE) { System.out.println(System.nanoTime() - startTime); System.exit(0); } return value; } /** * 自己调用 unsafe.compareAndSwapInt */ private void getAndIncreaseD() { int result = cas(); if (result == TEST_SIZE) { System.out.println(System.nanoTime() - startTime); System.exit(0); } } /** * long adder */ private void getAndIncreaseE() { longAdder.increment(); if(longAdder.sum() == TEST_SIZE) { System.out.println(System.nanoTime() - startTime); System.exit(0); } } public class MyTask implements Runnable { @Override public void run() { cdl.countDown(); try { cdl.await(); } catch (InterruptedException e) { e.printStackTrace(); } while (true) getAndIncreaseE();// getAndIncreaseB(); } } public static void main(String[] args) throws InterruptedException { Sy2 at = new Sy2(); for (int n = 0; n < THREAD_COUNT; n++) new Thread(at.new MyTask()).start(); System.out.println("start"); at.init(); at.cdl.countDown(); } } package thread; import java.lang.reflect.Field; i



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: