线程测试--疑惑

今天看了下线程中ReentrantLock类,便根据网上的资料写了个测试代码?
结果发现每次运行速度都不一样很纳闷。。。。。特来求教--哪出问题了?
ps:网上说  Tim Peierls 用一个简单的线性全等伪随机数生成器(PRNG)构建了一个简单的评测
    哪位大虾有这评测,能否提供,感激不尽!

public class ReentrantLockTest {
	    private CyclicBarrier barrier;
	    private Object objTest;
	    private int threadNum;

	    public ReentrantLockTest(int threadNum) {
	        barrier = new CyclicBarrier(threadNum + 1);
	        this.threadNum = threadNum;
	    }

	    public static void main(String args[]) {
	    	int threadCount = 5000;
	    	ReentrantLockTest test4 = new ReentrantLockTest(threadCount);
	    	test4.objTest = test4.new ReentrantLockUnFair();
	    	test4.test();
	    	
	    	ReentrantLockTest test1 = new ReentrantLockTest(threadCount);
	    	test1.objTest = test1.new ReentrantLockFair();
	    	test1.test();
	    	ReentrantLockTest test2 = new ReentrantLockTest(threadCount);
	    	test2.objTest = test2.new ReentrantLockUnFair();
	    	test2.test();	    	

	    	ReentrantLockTest test3 = new ReentrantLockTest(threadCount);
	    	test3.objTest = test3.new SynchronizedTest();
	    	test3.test();
	    }

	    public void test() {
	        try {
	            for (int i = 0; i < threadNum; i++) {
	                new TestThread(objTest).start();
	            }
//	            barrier.await(); // �ȴ����������̴߳���
//	            long start = System.currentTimeMillis();
//	            barrier.await(); // �ȴ���������������
//	            long end = System.currentTimeMillis();
	            
	           //����һ��д��
	            long start = System.currentTimeMillis();
	            barrier.await(); // �ȴ����������̴߳���
	            barrier.await(); // �ȴ���������������
	            long end = System.currentTimeMillis();	
	            
	            System.out.println("����ʱ��:" + (end - start) + "����");
	        } catch (Exception e) {
	            throw new RuntimeException(e);
	        }
	    }

	    class TestThread extends Thread {

	    	private Object obj;
	        public TestThread(Object obj) {
	        	this.obj = obj;
	        }

	        public void run() {
	            try {
	                barrier.await();
	                obj.equals("");
	                barrier.await();
	            } catch (Exception e) {
	                throw new RuntimeException(e);
	            }
	        }
	    }
	    class ReentrantLockFair {
	    	private final Lock lock = new ReentrantLock(true);
	    	public boolean equals(Object obj){
				lock.lock();
				try {
					for(int i=0 ; i<10 ; i++){
					}
				} finally {
					lock.unlock();
				}
	    		return true;
	    	}
	    }
	    class ReentrantLockUnFair {
    		final Lock lock = new ReentrantLock(false);
	    	public boolean equals(Object obj){
	    		lock.lock();
	    		try {
	    			for(int i=0 ; i<10 ; i++){
	    			}
	    		} finally {
	    			lock.unlock();
	    		}
	    		return true;
	    	}
	    }
	    class SynchronizedTest {
	    	public synchronized boolean equals(Object obj){
	    			for(int i=0 ; i<10 ; i++){
	    			}					
	    		return true;
	    	}
	    }
}

你可能感兴趣的:(多线程,thread)