性能主题
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();
}
}
}
}
测试结果:
偏向锁BiasedLock并不是Intel芯片缺省实现,可以使用-XX:-UseBiasedLocking测试是否效果。
在Intel芯片上,推荐使用 ReentrantLock好于同步锁synchronized。