java三种_Java三种常见锁性能对比

Java三种常见锁性能对比

该实验测试三种锁synchronized  CyclicBarrier 和可重入锁ReentrantLock,以计数器为例:

import java.util.concurrent.BrokenBarrierException;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

import java.util.concurrent.CyclicBarrier;

import static java.lang.System.out;

public final class TestLocks implements Runnable

{

public enum LockType { JVM, JUC }

public static LockType lockType;

public static final long ITERATIONS = 500L * 1000L *1000L;

public static long counter = 0L;

public static final Object jvmLock = new Object();

public static final Lock jucLock = new ReentrantLock();

private static int numThreads;

private static CyclicBarrier barrier;

public static void main(final String[] args) throws Exception

{

lockType = LockType.valueOf(args[0]);

numThreads = Integer.parseInt(args[1]);

runTest(numThreads); // warm up

counter = 0L;

final long start = System.nanoTime();

runTest(numThreads);

final long duration = System.nanoTime() - start;

out.printf("%d threads, duration %,d (ns)\n", numThreads, duration);

out.printf("%,d ns/op\n", duration / ITERATIONS);

out.printf("%,d ops/s\n", (ITERATIONS * 1000000000L) / duration);

out.println("counter = " + counter);

}

private static void runTest(final int numThreads) throws Exception

{

barrier = new CyclicBarrier(numThreads);

Thread[] threads = new Thread[numThreads];

for (int i = 0; i < threads.length; i++)

{

threads[i] = new Thread(new TestLocks());

}

for (Thread t : threads)

{

t.start();

}

for (Thread t : threads)

{

t.join();

}

}

public void run()

{

try

{

barrier.await();

}

catch (Exception e)

{

// don't care

}

switch (lockType)

{

case JVM: jvmLockInc(); break;

case JUC: jucLockInc(); break;

}

}

private void jvmLockInc()

{

long count = ITERATIONS / numThreads;

while (0 != count--)

{

synchronized (jvmLock)

{

++counter;

}

}

}

private void jucLockInc()

{

long count = ITERATIONS / numThreads;

while (0 != count--)

{

jucLock.lock();

try

{

++counter;

}

finally

{

jucLock.unlock();

}

}

}

}

测试结果:

7a556b5b5d4bd23a5dd777de93e7b315.png

683e3d725398d636bbce8e0eed22ff19.png

28423916944451f22e4c7b1c287ca965.png

偏向锁BiasedLock并不是Intel芯片缺省实现,可以使用-XX:-UseBiasedLocking测试是否效果。

在Intel芯片上,推荐使用 ReentrantLock好于同步锁synchronized。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值