java多线程编程——显示锁ReentrantLock(一)
2014-10-30 11:33
507 查看
package test; /** * 内部锁 * * @author Administrator * */ public class SynchronizedBenchmarkDemo implements Counter { private long count = 0; @Override public long getValue() { return count; } @Override public synchronized void increment() { count++; } }
package test; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 不公平重入锁 * * @author Administrator * */ @SuppressWarnings("all") public class ReentrantLockUnfairBeanchmarkDemo implements Counter { private volatile long count = 0; private Lock lock; public ReentrantLockUnfairBeanchmarkDemo() { // 使用不公平锁 lock = new ReentrantLock(false); } @Override public long getValue() { return count; } @Override public void increment() { lock.lock(); try { count++; } finally { lock.unlock(); } } }
package test; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 公平重入锁 * * @author Administrator * */ @SuppressWarnings("all") public class ReentrantLockFairBeanchmarkDemo implements Counter { private volatile long count = 0; private Lock lock; public ReentrantLockFairBeanchmarkDemo() { // 创建公平锁 lock = new ReentrantLock(true); } @Override public long getValue() { return count; } @Override public void increment() { lock.lock(); try { count++; } finally { lock.unlock(); } } }
package test; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; /** * 三种类型的同步锁:内部锁、不公平锁、公平锁,每种类型的锁各自创建2000个线程,然后每个线程访问带有锁得变量100次,并加1。 * 统计各种锁并发访问时消耗的时间 * * @author Administrator * */ @SuppressWarnings("all") public class BenchmarkTest { private Counter counter; // 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 private CyclicBarrier barrier; private int threadNum; private int loopNum; private String testName; /******************************************************************* * 用于记录由该对象创建的threadNum线程统开始循环和结束循环的起止时间 */ public long begin; public long end; private BenchmarkTest(Counter counter, int threadNum, int loopNum, String testName) { this.counter = counter; this.threadNum = threadNum; this.loopNum = loopNum; this.testName = testName; barrier = new CyclicBarrier(threadNum, new BarrierTime(this)); } public void test() { try { for (int i = 0; i < threadNum; i++) { new TestThread(counter, loopNum, this).start(); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { try { int threadNum = 2000; int loopNum = 100; new BenchmarkTest(new SynchronizedBenchmarkDemo(), threadNum, loopNum, "内部锁").test(); Thread.sleep(5000); new BenchmarkTest(new ReentrantLockUnfairBeanchmarkDemo(), threadNum, loopNum, "不公平锁").test(); Thread.sleep(5000); new BenchmarkTest(new ReentrantLockFairBeanchmarkDemo(), threadNum, loopNum, "公平锁锁").test(); } catch (InterruptedException e) { e.printStackTrace(); } } class TestThread extends Thread { int loopNum = 100; private Counter counter; private BenchmarkTest benchmarkTest; private TestThread(final Counter counter, int loopNum, BenchmarkTest benchmarkTest) { this.counter = counter; this.loopNum = loopNum; this.benchmarkTest = benchmarkTest; } public void run() { try { // 等待所有线程开始 barrier.await(); for (int i = 0; i < this.loopNum; i++) { counter.increment(); } // 等待所有线程结束 barrier.await(); } catch (Exception e) { throw new RuntimeException(); } } } class BarrierTime implements Runnable { private BenchmarkTest benchmarkTest; public BarrierTime(BenchmarkTest benchmarkTest) { this.benchmarkTest = benchmarkTest; } @Override public void run() { if (benchmarkTest.begin == 0) { benchmarkTest.begin = System.currentTimeMillis(); } else { benchmarkTest.end = System.currentTimeMillis(); } if (benchmarkTest.end != 0) { System.out.println(benchmarkTest.testName + "共花费时间:" + (benchmarkTest.end - benchmarkTest.begin) + "毫秒"); } } } }
内部锁共花费时间:260毫秒
不公平锁共花费时间:26毫秒
公平锁锁共花费时间:1471毫秒
相关文章推荐
- Java 多线程并发编程之互斥锁 Reentrant Lock
- Java并发编程之显示锁ReentrantLock和ReadWriteLock读写锁
- Java并发编程之显示锁ReentrantLock和ReadWriteLock读写锁
- java多线程编程——显示锁ReadWriteLock(二)
- 深入Java多线程和并发编程之ReentrantLock
- java并发编程-线程同步-显示锁ReentrantLock
- Java并发编程之显示锁ReentrantLock和ReadWriteLock读写锁
- 【Java并发编程实战】-----“J.U.C”:ReentrantLock之一简介
- Java多线程(九)之ReentrantLock与Condition
- Java多线程(九)之ReentrantLock与Condition
- 【Java并发编程实战】-----“J.U.C”:ReentrantLock之三unlock方法分析
- Java中的显示锁 ReentrantLock 和 ReentrantReadWriteLock
- Java多线程系列--“JUC锁”08之 共享锁和ReentrantReadWriteLock
- Java多线程并发编程之显示锁ReentrantLock和读写锁
- Java多线程(十)之ReentrantReadWriteLock深入分析
- 【Java并发编程实战】-----“J.U.C”:ReentrantLock之二lock方法分析
- java 多线程 ReentrantReadWriteLock 使用
- Java多线程的~~~Lock接口和ReentrantLock使用
- Java并发编程 ReentrantLock 源码分析
- Java多线程(五) ReentrantLock、Lock和Condition的用法