Java中的锁之间的区别是什么?

synchronized 和 java.util.concurrent.lock.Lock 之间的区别

  • 实现层面不一样。synchronized 是 Java 关键字,JVM层面 实现加锁和释放锁;Lock 是一个接口,在代码层面实现加锁和释放锁
  • 是否自动释放锁。synchronized 在线程代码执行完或出现异常时自动释放锁;Lock 不会自动释放锁,需要在finally {} 代码块显式地中释放锁
  • 是否一直等待。synchronized 会导致线程拿不到锁一直等待;Lock 可以设置尝试获取锁或者获取锁失败一定时间超时
  • 获取锁成功是否可知。synchronized 无法得知是否获取锁成功;Lock 可以通过 tryLock 获得加锁是否成功
  • 功能复杂性。synchronized 加锁可重入、不可中断、非公平;Lock 可重入、可判断、可公平和不公平、细分读写锁提高效率

java.util.concurrent.lock.Lock 与java.util.concurrent.lock.ReadWriteLock 之间的区别

  • ReadWriteLock 定义了获取读锁和写锁的接口,读锁之间不互斥,非常适合读多、写少的场景

适用场景

  • JDK 1.6 开始,对 synchronized 方式枷锁进行了优化,加入了偏向锁、轻量级锁和锁升级机制,性能得到了很大的提升。性能与 ReentrantLock 差不多
  • 读多写少的情况下,考虑使用ReadWriteLock

synchronized、ReentrantLock、ReentrantReadWriteLock 启动 990 个线程读共享变量,10 个线程写共享变量

package constxiong.concurrency.a020;import java.util.ArrayList;import java.util.List;import java.util.UUID;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantLock;import java.util.concurrent.locks.ReentrantReadWriteLock;/** * JDK 1.8 中锁性能的测试 * @author ConstXiong */public class TestLockPerformance {public static Object obj = new Object();//用于 synchronized 获取锁public static Lock lock = new ReentrantLock();//可重入锁public static ReadWriteLock readWriteLock = new ReentrantReadWriteLock();//读写锁public static final int READ = 0;public static final int WRITE = 1;// uuid,一个随机字符串public static String uuid = UUID.randomUUID().toString();public static void main(String[] args) throws InterruptedException {//testSynchronized(1000);testReentrantLock(1000);//testReadWriteLock(1000);}public static void testSynchronized(int threadNum) throws InterruptedException {long t1 = System.currentTimeMillis();List<Thread> tList = new ArrayList<Thread>();//启动 threadNum - 向上取整 (0.01 * threadNum) 个线程读 uuid, 向上取整 (0.01 * threadNum) 个线程写 uuidfor (int i = 0; i <threadNum; i++) {Thread t;if (i % 100 == 0) {t = new Thread(new WorkerSynchronized(WRITE));} else {t = new Thread(new WorkerSynchronized(READ));}t.start();//启动线程tList.add(t);}for (Thread t : tList) {t.join();}long t2 = System.currentTimeMillis();System.out.println("testSynchronized 耗时:" + (t2 - t1));}public static void testReentrantLock(int threadNum) throws InterruptedException {long t1 = System.currentTimeMillis();List<Thread> tList = new ArrayList<Thread>();//启动 threadNum - 向上取整 (0.01 * threadNum) 个线程读 uuid, 向上取整 (0.01 * threadNum) 个线程写 uuidfor (int i = 0; i <threadNum; i++) {Thread t;if (i % 100 == 0) {t = new Thread(new WorkerReentrantLock(WRITE));} else {t = new Thread(new WorkerReentrantLock(READ));}t.start();//启动线程tList.add(t);}for (Thread t : tList) {t.join();}long t2 = System.currentTimeMillis();System.out.println("testReentrantLock 耗时:" + (t2 - t1));}public static void testReadWriteLock(int threadNUm) throws InterruptedException {long t1 = System.currentTimeMillis();List<Thread> tList = new ArrayList<Thread>();//启动 threadNum - 向上取整 (0.01 * threadNum) 个线程读 uuid, 向上取整 (0.01 * threadNum) 个线程写 uuidfor (int i = 0; i <threadNUm; i++) {Thread t;if (i % 100 == 0) {t = new Thread(new WorkerReadWriteLock(WRITE));} else {t = new Thread(new WorkerReadWriteLock(READ));}t.start();//启动线程tList.add(t);}for (Thread t : tList) {t.join();}long t2 = System.currentTimeMillis();System.out.println("testReadWriteLock 耗时:" + (t2 - t1));}}//工作线程,使用 synchronized 关键字加锁class WorkerSynchronized implements Runnable {//0-read;1-writeprivate int type;WorkerSynchronized(int type) {this.type = type;}//加锁读 TestLockPerformance.uuid 变量,并打印private void read() {synchronized (TestLockPerformance.obj) {//休眠 20 毫秒,模拟任务执行耗时try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " read uuid = " +TestLockPerformance.uuid);}}//加锁写 TestLockPerformance.uuid 变量,并打印private void write() {synchronized (TestLockPerformance.obj) {//休眠 20 毫秒,模拟任务执行耗时try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}TestLockPerformance.uuid = UUID.randomUUID().toString();System.out.println(Thread.currentThread().getName() + " write uuid = " +TestLockPerformance.uuid);}}@Overridepublic void run() {//type = 0,线程读 TestLockPerformance.uuid 变量if (type == 0) {read();//type = 1,线程生成 uuid,写入 TestLockPerformance.uuid 变量} else {write();}}}//工作线程,使用 ReentrantLock 加锁class WorkerReentrantLock implements Runnable {//0-read;1-writeprivate int type;WorkerReentrantLock(int type) {this.type = type;}//加锁读 TestLockPerformance.uuid 变量,并打印private void read() {TestLockPerformance.lock.lock();try {//休眠 20 毫秒,模拟任务执行耗时try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " read uuid = " +TestLockPerformance.uuid);} finally {TestLockPerformance.lock.unlock();}}//加锁写 TestLockPerformance.uuid 变量,并打印private void write() {TestLockPerformance.lock.lock();try {//休眠 20 毫秒,模拟任务执行耗时try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}TestLockPerformance.uuid = UUID.randomUUID().toString();System.out.println(Thread.currentThread().getName() + " write uuid = " +TestLockPerformance.uuid);} finally {TestLockPerformance.lock.unlock();}}@Overridepublic void run() {//type = 0,线程读 TestLockPerformance.uuid 变量if (type == 0) {read();//type = 1,线程生成 uuid,写入 TestLockPerformance.uuid 变量} else {write();}}}//工作线程,使用 ReentrantReadWriteLock 关键字加锁class WorkerReadWriteLock implements Runnable {//0-read;1-writeprivate int type;WorkerReadWriteLock(int type) {this.type = type;}//加锁读 TestLockPerformance.uuid 变量,并打印private void read() {TestLockPerformance.readWriteLock.readLock().lock();try {//休眠 20 毫秒,模拟任务执行耗时try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " read uuid = " +TestLockPerformance.uuid);} finally {TestLockPerformance.readWriteLock.readLock().unlock();}}//加锁写 TestLockPerformance.uuid 变量,并打印private void write() {TestLockPerformance.readWriteLock.writeLock().lock();try {//休眠 20 毫秒,模拟任务执行耗时try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}TestLockPerformance.uuid = UUID.randomUUID().toString();System.out.println(Thread.currentThread().getName() + " write uuid = " +TestLockPerformance.uuid);} finally {TestLockPerformance.readWriteLock.writeLock().unlock();}}@Overridepublic void run() {//type = 0,线程读 TestLockPerformance.uuid 变量if (type == 0) {read();//type = 1,线程生成 uuid,写入 TestLockPerformance.uuid 变量} else {write();}}}

调用测试方法

testSynchronized(1000);

耗时

Thread-0 write uuid = b7fb63d7-79cc-4cc0-84ed-5a9cd4de6824Thread-252 read uuid = b7fb63d7-79cc-4cc0-84ed-5a9cd4de6824Thread-251 read uuid = b7fb63d7-79cc-4cc0-84ed-5a9cd4de6824...Thread-255 read uuid = d666bfe6-dc71-4df2-882a-d530a59d7e92Thread-254 read uuid = d666bfe6-dc71-4df2-882a-d530a59d7e92Thread-253 read uuid = d666bfe6-dc71-4df2-882a-d530a59d7e92testSynchronized 耗时:22991

调用测试方法

testReentrantLock(1000);

耗时

Thread-0 write uuid = 4352eb13-d284-47ec-8caa-fc81d91d08e1Thread-1 read uuid = 4352eb13-d284-47ec-8caa-fc81d91d08e1Thread-485 read uuid = 4352eb13-d284-47ec-8caa-fc81d91d08e1...Thread-997 read uuid = 9d7f0a78-5eb7-4506-9e98-e8e9a7a717a5Thread-998 read uuid = 9d7f0a78-5eb7-4506-9e98-e8e9a7a717a5Thread-999 read uuid = 9d7f0a78-5eb7-4506-9e98-e8e9a7a717a5testReentrantLock 耗时:22935

调用测试方法

testReadWriteLock(1000);

耗时

Thread-0 write uuid = 81c13f80-fb19-4b27-9d21-2e99f8c8acbdThread-277 read uuid = 81c13f80-fb19-4b27-9d21-2e99f8c8acbdThread-278 read uuid = 81c13f80-fb19-4b27-9d21-2e99f8c8acbd...Thread-975 read uuid = 35be0359-1973-4a4f-85b7-918053d841f7Thread-971 read uuid = 35be0359-1973-4a4f-85b7-918053d841f7Thread-964 read uuid = 35be0359-1973-4a4f-85b7-918053d841f7testReadWriteLock 耗时:543

通过耗时测试可以看出,使用 synchronized 和 ReentrantLock 耗时相近;但是由于 990 个线程读,10 个线程写,使用 ReentrantReadWriteLock 耗时 543 毫秒。

给TA打赏
共{{data.count}}人
人已打赏
Java

什么是线程?什么是进程?为什么要有线程?有什么关系与区别?

2020-7-31 0:03:20

Java

什么是线程池?

2020-7-31 0:06:40

本站所发布的一切源码、模板、应用等文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权。本站内容适用于DMCA政策。若您的权利被侵害,请与我们联系处理,站长 QQ: 84087680 或 点击右侧 私信:盾给网 反馈,我们将尽快处理。
⚠️
本站所发布的一切源码、模板、应用等文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权。本站内容适用于DMCA政策
若您的权利被侵害,请与我们联系处理,站长 QQ: 84087680 或 点击右侧 私信:盾给网 反馈,我们将尽快处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索