Welcome to Java Lock example tutorial. Usually when working with multi-threaded environment, we use synchronized for thread safety.

欢迎使用Java Lock示例教程。 通常,在多线程环境中使用时, 为了确保线程安全 ,我们使用sync 。

Java锁 (Java Lock)

Most of the times, synchronized keyword is the way to go but it has some shortcomings that lead the way to inclusion of Lock API in Java Concurrency package. Java 1.5 Concurrency API came up with java.util.concurrent.locks package with Lock interface and some implementation classes to improve the Object locking mechanism.

在大多数情况下,synchronized关键字是解决之道,但它存在一些缺点,导致将Lock API包含在Java Concurrency包中。 Java 1.5 Concurrency API带有带有Lock接口的java.util.concurrent.locks包和一些实现类,用于改进对象锁定机制。

Some important interfaces and classes in Java Lock API are:

Java Lock API中一些重要的接口和类是:

  1. Lock: This is the base interface for Lock API. It provides all the features of synchronized keyword with additional ways to create different Conditions for locking, providing timeout for thread to wait for lock. Some of the important methods are lock() to acquire the lock, unlock() to release the lock, tryLock() to wait for lock for a certain period of time, newCondition() to create the Condition etc.Lock :这是Lock API的基本接口。 它提供了synced关键字的所有功能以及创建不同锁定条件的其他方式,从而为线程等待锁定提供了超时。 一些重要的方法是使用lock()获取锁,使用unlock()释放锁,使用tryLock()等待一段时间的锁,使用newCondition()创建Condition等。
  2. Condition: Condition objects are similar to Object wait-notify model with additional feature to create different sets of wait. A Condition object is always created by Lock object. Some of the important methods are await() that is similar to wait() and signal(), signalAll() that is similar to notify() and notifyAll() methods.Condition :Condition对象类似于Object wait-notify模型,具有附加功能,可以创建不同的等待集。 Condition对象始终由Lock对象创建。 一些重要的方法是类似于wait()和signal()的await()和类似于notify()和notifyAll()方法的signalAll()。
  3. ReadWriteLock: It contains a pair of associated locks, one for read-only operations and another one for writing. The read lock may be held simultaneously by multiple reader threads as long as there are no writer threads. The write lock is exclusive.ReadWriteLock :它包含一对关联的锁,一个用于只读操作,另一个用于写入。 只要没有写程序线程,读锁就可以同时由多个读程序线程持有。 写锁是排他的。
  4. ReentrantLock: This is the most widely used implementation class of Lock interface. This class implements the Lock interface in similar way as synchronized keyword. Apart from Lock interface implementation, ReentrantLock contains some utility methods to get the thread holding the lock, threads waiting to acquire the lock etc.

    synchronized block are reentrant in nature i.e if a thread has lock on the monitor object and if another synchronized block requires to have the lock on the same monitor object then thread can enter that code block. I think this is the reason for the class name to be ReentrantLock. Let’s understand this feature with a simple example.

    public class Test{public synchronized foo(){//do somethingbar();}public synchronized bar(){//do some more}
    }

    If a thread enters foo(), it has the lock on Test object, so when it tries to execute bar() method, the thread is allowed to execute bar() method since it’s already holding the lock on the Test object i.e same as synchronized(this).

    ReentrantLock :这是Lock接口使用最广泛的实现类。 此类以与synced关键字相似的方式实现Lock接口。 除了Lock接口的实现之外,ReentrantLock还包含一些实用程序方法来获取持有锁的线程,等待获取锁的线程等。

    同步块本质上是可重入的,即,如果线程在监视对象上具有锁,并且如果另一个同步块需要在同一监视对象上具有锁,则线程可以输入该代码块。 我认为这是类名称为ReentrantLock的原因。 让我们通过一个简单的示例来了解此功能。

    如果线程输入foo(),则它具有Test对象的锁定,因此,当它尝试执行bar()方法时,由于该线程已经持有Test对象的锁定,因此允许该线程执行bar()方法。同步(this)。

Java锁示例– Java中的ReentrantLock (Java Lock Example – ReentrantLock in Java)

Now let’s see a simple example where we will replace synchronized keyword with Java Lock API.

现在,让我们看一个简单的示例,其中将用Java Lock API替换synced关键字。

Let’s say we have a Resource class with some operation where we want it to be thread-safe and some methods where thread safety is not required.

假设我们有一个Resource类,其中包含一些我们希望它是线程安全的操作,以及一些不需要线程安全的方法。

package com.journaldev.threads.lock;public class Resource {public void doSomething(){//do some operation, DB read, write etc}public void doLogging(){//logging, no need for thread safety}
}

Now let’s say we have a Runnable class where we will use Resource methods.

现在,我们有一个Runnable类,在其中我们将使用Resource方法。

package com.journaldev.threads.lock;public class SynchronizedLockExample implements Runnable{private Resource resource;public SynchronizedLockExample(Resource r){this.resource = r;}@Overridepublic void run() {synchronized (resource) {resource.doSomething();}resource.doLogging();}
}

Notice that I am using synchronized block to acquire the lock on Resource object. We could have created a dummy object in the class and used that for locking purpose.

请注意,我正在使用同步块来获取对Resource对象的锁定。 我们可以在类中创建一个虚拟对象,并将其用于锁定目的。

Now let’s see how we can use java Lock API and rewrite above program without using synchronized keyword. We will use ReentrantLock in java.

现在让我们看看如何使用Java Lock API并在不使用synced关键字的情况下重写上述程序。 我们将在Java中使用ReentrantLock。

package com.journaldev.threads.lock;import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class ConcurrencyLockExample implements Runnable{private Resource resource;private Lock lock;public ConcurrencyLockExample(Resource r){this.resource = r;this.lock = new ReentrantLock();}@Overridepublic void run() {try {if(lock.tryLock(10, TimeUnit.SECONDS)){resource.doSomething();}} catch (InterruptedException e) {e.printStackTrace();}finally{//release locklock.unlock();}resource.doLogging();}}

As you can see that, I am using tryLock() method to make sure my thread waits only for definite time and if it’s not getting the lock on object, it’s just logging and exiting. Another important point to note is the use of try-finally block to make sure lock is released even if doSomething() method call throws any exception.

如您所见,我正在使用tryLock()方法来确保我的线程仅等待确定的时间,并且如果它没有获得对对象的锁定,那么它只是在记录并退出。 要注意的另一个重要点是,即使doSomething()方法调用引发任何异常,也要使用try-finally块来确保释放锁定。

Java Lock与同步 (Java Lock vs synchronized)

Based on above details and program, we can easily conclude following differences between Java Lock and synchronization.

根据上述详细信息和程序,我们可以轻松得出Java Lock和同步之间的以下区别。

  1. Java Lock API provides more visibility and options for locking, unlike synchronized where a thread might end up waiting indefinitely for the lock, we can use tryLock() to make sure thread waits for specific time only.Java Lock API提供了更多的锁定可见性和选项,与同步机制不同,同步机制可能导致线程无限期地等待锁定,因此我们可以使用tryLock()来确保线程仅在特定时间等待。
  2. Synchronization code is much cleaner and easy to maintain whereas with Lock we are forced to have try-finally block to make sure Lock is released even if some exception is thrown between lock() and unlock() method calls.同步代码更简洁,易于维护,而使用Lock时,即使在lock()和unlock()方法调用之间引发了某些异常,我们也不得不尝试进行最后锁定,以确保释放Lock。
  3. synchronization blocks or methods can cover only one method whereas we can acquire the lock in one method and release it in another method with Lock API.同步块或方法只能覆盖一个方法,而我们可以使用Lock API在一个方法中获取锁并在另一方法中释放锁。
  4. synchronized keyword doesn’t provide fairness whereas we can set fairness to true while creating ReentrantLock object so that longest waiting thread gets the lock first.synced关键字不提供公平性,而我们可以在创建ReentrantLock对象时将公平性设置为true,以便等待时间最长的线程首先获得该锁。
  5. We can create different conditions for Lock and different thread can await() for different conditions.我们可以为Lock创建不同的条件,并且不同的线程可以为不同的条件使用await()。

That’s all for Java Lock example, ReentrantLock in java and a comparative analysis with synchronized keyword.

Java锁示例,Java中的ReentrantLock以及带有synced关键字的比较分析就可以了。

翻译自: https://www.journaldev.com/2377/java-lock-example-reentrantlock

Java锁示例– ReentrantLock相关推荐

  1. Java锁详解之ReentrantLock

    文章目录 写在前面 ReentrantLock的重要方法 ReentrantLock使用示例 ReentrantLock的公平和非公平锁 ReentrantLock的重入锁 ReentrantLock ...

  2. java lock unlock_详解Java中的ReentrantLock锁

    ReentrantLock锁 ReentrantLock是Java中常用的锁,属于乐观锁类型,多线程并发情况下.能保证共享数据安全性,线程间有序性 ReentrantLock通过原子操作和阻塞实现锁原 ...

  3. Java—synchronized和ReentrantLock锁详解

    关注微信公众号:CodingTechWork,一起学习进步. 1 synchronized 1.1 synchronized介绍 synchronized机制提供了对每个对象相关的隐式监视器锁,并强制 ...

  4. Java并发之ReentrantLock锁

    简介 一种可重入的互斥锁,经由Java5引入,支持一个线程对资源的重复加锁.它和synchronized语句和方法访问的隐式监视器锁,有相同的基本行为和语义,但是功能更强大.之所以存在synchron ...

  5. 死磕Java并发:J.U.C之重入锁:ReentrantLock

    ReentrantLock,可重入锁,是一种递归无阻塞的同步机制.它可以等同于synchronized的使用,但是ReentrantLock提供了比synchronized更强大.灵活的锁机制,可以减 ...

  6. Java并发编程-ReentrantLock可重入锁

    目录 1.ReentrantLock可重入锁概述 2.可重入 3.可打断 4.锁超时 5.公平锁 6.条件变量 Condition 1.ReentrantLock可重入锁概述 相对于 synchron ...

  7. java锁的概念,Java ReentrantLock锁机制概念篇

    分享Java锁机制实现原理,细节涉及volatile修饰符.CAS原子操作.park阻塞线程与unpark唤醒.双向链表.锁的公平性与非公平性.独占锁和共享锁.线程等待await.线程中断interr ...

  8. Java锁——Condition使用示例及讲解

    Condition简介 任何一个java对象都天然继承于Object类,在线程间实现通信的往往会应用到Object的几个方法,比如wait(),wait(long timeout),wait(long ...

  9. Java中的ReentrantLock和synchronized两种锁定机制的对比

    原文:http://www.ibm.com/developerworks/cn/java/j-jtp10264/index.html 多线程和并发性并不是什么新内容,但是 Java 语言设计中的创新之 ...

最新文章

  1. BCE或能成为BCH的一个侧链
  2. 【机器学习算法-python实现】决策树-Decision tree(2) 决策树的实现
  3. LeetCode 79 Word Search(单词查找)
  4. 2019年最新 Python 模拟登录知乎 支持验证码
  5. android之id统一管理
  6. JavaScript动态显示当前时间和倒计时的设计(附全码)_AX
  7. 【图像修复】基于matlab GUI维纳滤波图像复原【含Matlab源码 851期】
  8. 软件无线电的发展与展望
  9. 新版Cadence打开PSpice8.0工程文件
  10. Hibernate4.3在开发中的一些异常总结(持续更新)
  11. 【20保研】四川大学网络空间安全学院2019年优秀大学生暑期夏令营招生简章
  12. 海风的Linux开发环境介绍
  13. Qt【正则表达式】匹配中文汉字,和字母,过滤中英文符号
  14. Python实现批量修改图片名称并存入新文件夹
  15. Py西游攻关之正则表达式
  16. win10任务栏网络连接图标消失的解决办法
  17. 基于FreeRTOS与MQTT的物联网技术应用系列——步进电机控制(四)FreeRTOS系统下LwIP-1.4.1的移植
  18. 第19组 Beta(2/3)
  19. light Mode:real-time\mixed\Baked
  20. [python] say hi

热门文章

  1. 回调函数 EnumFontFamProc
  2. MySQL update慢问题解决
  3. verilog之按键消抖的理解
  4. LeetCode84 Largest Rectangle in Histogram
  5. hdu 4059 The Boss on Mars 容斥
  6. 通过JAVA操作SAE上的MY SQL数据库
  7. 若不能细水长流地书写内心的温柔,那轰轰烈烈的一幕一幕不过是日后回忆自己爱无能的证据罢了。...
  8. PAD-Net: Multi-Tasks Guided Prediction-and-Distillation Network for Simultaneous Depth Estimation an
  9. 数据结构上机实践第十周项目2 - 用二叉树求解代数表达式
  10. 数据结构上机实践第四周项目1 - 建立单链表