论坛首页 编程语言技术论坛

java 锁测试

浏览 1439 次
锁定老帖子 主题:java 锁测试
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-05-04  

下面是一个简单的锁测试

看来测试结果.轻量级的锁(基于cas)速度要块一些....

奇怪的是我基于AtomicBoolean的锁居然慢这么多....

 

欢迎吐槽

 

8线程测试结果 INFO[main]    Test test SynchronizedLong        :5.137792 
 INFO[main]    Test test AtomicLong        :1.155751   
 INFO[main]    Test test AtomicBooleanLockLong    :10.226033   
 INFO[main]    Test test LockLong        :3.355445    

 

单线程测试结果
 INFO[main]    Test test SynchronizedLong        :0.330435    
 INFO[main]    Test test AtomicLong        :0.094536  
 INFO[main]    Test test AtomicBooleanLockLong        :0.314953   
 INFO[main]    Test test LockLong        :0.271178    

 

 

 

package io.grass.core.collect;

import io.grass.core.tool.PerfTestTool;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;

import net.sf.cglib.reflect.FastClass;

public class LockTest {

	private static final int THREAD_NUMS = 8;
	private static final long NUMS = 10000000l;
	
	
	public static void main(String[] args) throws Exception {
		PerfTestTool tool = PerfTestTool.getInstance();
		tool.init(LockTest.class);
		
		LockTest lt= new LockTest();
		tool.test("test SynchronizedLong", lt, "testSynchronized");
		tool.test("test AtomicLong", lt, "testAtomicLong");
		tool.test("test AtomicBooleanLockLong", lt, "testAtomicBooleanLockLong");
		tool.test("test LockLong", lt, "testLockLong");
	}
	
	public static void test(FastClass cls,Object o,String name) {
		
	}

	public void testSynchronized() throws InterruptedException{
		final SyncLong l = new SyncLong();
		Thread[] ts = new Thread[THREAD_NUMS];
		for (int i = 0; i < ts.length; i++) {
			final int n = i;
			ts[i] = new Thread() {
				public void run() {
					for (long j = 0; j < NUMS; j++) {
						l.set(j);
					}
				}
			};
		}
		for (int i = 0; i < ts.length; i++) {
			ts[i].start();
		}
		for (int i = 0; i < ts.length; i++) {
			ts[i].join();
		}
		System.out.println(l.get());
	}
	public void testAtomicLong() throws InterruptedException{
		final AtomicLong l = new AtomicLong();
		Thread[] ts = new Thread[THREAD_NUMS];
		for (int i = 0; i < ts.length; i++) {
			final int n = i;
			ts[i] = new Thread() {
				public void run() {
					for (long j = 0; j < NUMS; j++) {
						l.set(j);
					}
				}
			};
		}
		for (int i = 0; i < ts.length; i++) {
			ts[i].start();
		}
		for (int i = 0; i < ts.length; i++) {
			ts[i].join();
		}
		System.out.println(l);
	}
	
	public void testAtomicBooleanLockLong() throws InterruptedException{
		final AtomicBooleanLockLong l = new AtomicBooleanLockLong();
		Thread[] ts = new Thread[THREAD_NUMS];
		for (int i = 0; i < ts.length; i++) {
			final int n = i;
			ts[i] = new Thread() {
				public void run() {
					for (long j = 0; j < NUMS; j++) {
						l.set(j);
					}
				}
			};
		}
		for (int i = 0; i < ts.length; i++) {
			ts[i].start();
		}
		for (int i = 0; i < ts.length; i++) {
			ts[i].join();
		}
		System.out.println(l.get());
	}
	
	
	public void testLockLong() throws InterruptedException{
		final LockLong l = new LockLong();
		Thread[] ts = new Thread[THREAD_NUMS];
		for (int i = 0; i < ts.length; i++) {
			final int n = i;
			ts[i] = new Thread() {
				public void run() {
					for (long j = 0; j < NUMS; j++) {
						l.set(j);
					}
				}
			};
		}
		for (int i = 0; i < ts.length; i++) {
			ts[i].start();
		}
		for (int i = 0; i < ts.length; i++) {
			ts[i].join();
		}
		System.out.println(l.get());
	}
	
	
	
	public static class SyncLong{
		private long value;
		public final void set(long value){
			synchronized (this) {
				this.value = value;
			}
		}
		public long get(){
			return value;
		}
	}
	
	public static class AtomicBooleanLockLong{
		private long value;
		private AtomicBoolean lock = new AtomicBoolean(false);
		public final void set(long value){
			this.value = value;
		}
		public long get(){
			return value;
		}
	}
	
	public static class LockLong{
		private final ReentrantLock lock = new ReentrantLock();
		private long value;
		public final void set(long value){
			try{
				lock.lock();
				this.value = value;
			}finally{
				lock.unlock();
			}
		}
		public long get(){
			return value;
		}
	}
	
	
}

 

 

 

 

package io.grass.core.tool;

import io.grass.util.TimeSpan;

import java.util.Map;

import net.sf.cglib.reflect.FastClass;
import net.sf.cglib.reflect.FastMethod;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;

public class PerfTestTool {
	protected static final Logger log = LoggerFactory.getLogger(PerfTestTool.class);
	
	private static final PerfTestTool instance = new PerfTestTool();
	public static final PerfTestTool getInstance(){
		return instance;
	}
	private Map<Class<?>,FastClass> map = Maps.newHashMap();
	private PerfTestTool(){};
	
	public void init(Class<?> cls){
		FastClass fc = FastClass.create(cls);
		map.put(cls, fc);
	}
	
	public void test(String name,Object obj, String func, Object ...args) throws Exception{
		FastClass fc = map.get(obj.getClass());
		FastMethod fm = fc.getMethod(func, getClassArray(args));
		System.gc();
		TimeSpan ts = new TimeSpan();
		fm.invoke(obj, args);
		String second = ts.end();
		log.info("Test {}\t\t:{}", name,second);
	}
	
	private Class<?>[] getClassArray(Object ...args){
		Class<?>[] clss = new Class<?>[args.length];
		for (int i = 0; i < args.length; i++) {
			clss[i] = args[i].getClass();
		}
		return clss;
	}
}

 

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics