当前位置:网站首页>The difference between synchronized and reentrantlock
The difference between synchronized and reentrantlock
2022-07-27 15:43:00 【Skinny monkey 117】
Catalog
2. synchronized and ReentrantLock The difference between
juc Common subclasses under
1. Object lock juc.lock
Object lock juc.lock ——JDK1.0 There is , need JVM With the help of the operating system mutex System statement implementation .
JDK1.5 after ,Java The implementation of mutual exclusion implemented by the language itself , Without the help of the operating system monitor Mechanism .
stay Java In addition to synchronized Keywords can be implemented outside the object lock ,java.util.concurrent Medium Lock Interface You can also implement object locks .
Use Lock The interface needs to be displayed for locking and unlocking
And synchronized The difference is , There is one tryLock Method
Lock , The thread that failed to acquire the lock enters the blocking state , Wait for a while , After the time has passed, if the lock has not been obtained, resume execution , Give up locking , Execute other code
Unlock operation
Code demonstration
import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; public class LockTest { public static void main(String[] args) { ReentrantLock lock = new ReentrantLock(); Thread t1 = new Thread(() -> { lock.lock(); // The code locks from here , Until I met unlock Unlock // Mutually exclusive code block try { System.out.println(Thread.currentThread().getName() + " Get lock , Other threads wait "); Thread.sleep(8000); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { // Only the thread that successfully obtains the lock needs to execute unlock Method lock.unlock(); } System.out.println(Thread.currentThread().getName() + " Release the lock "); }, "1 Number "); t1.start(); Thread t2 = new Thread(() -> { boolean isLock = false; try { isLock = lock.tryLock(3000, TimeUnit.MICROSECONDS); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { if (isLock) { // Only the thread that successfully obtains the lock needs to execute unlock Method lock.unlock(); } } System.out.println(" Wait only 3 second "); }, "2 Number "); t2.start(); } }
2. synchronized and ReentrantLock The difference between
1. synchronized yes Java Key words of , from JVM Realization , You need to rely on what the operating system provides Thread mutex primitive (mutex);
Lock Classes and interfaces of the standard library , One of the most commonly used subclasses (ReentrantLock, Reentrant lock ), from Java It is realized by itself , There is no need to rely on the operating system .
2. synchronized Implicit Lock and unlock ,lock need Display progress Lock and unlock .
3. synchronized When the thread that failed to acquire the lock , Death etc. ;lock have access to tryLock Wait for a period of time and then give up automatically Lock , The thread resumes execution .
4. synchronized Fair lock ;
ReentrantLock Default is unfair lock , In the construction method Pass in true Open the fair lock .
5. synchronized Read / write locks are not supported ,Lock Subclass ReentrantReadWriteLock Read and write lock support .
General scene synchronized Enough , You need to wait for the lock with timeout , Fair lock , Consider using read-write lock juc.lock.
边栏推荐
- $router.back(-1)
- npm install错误 unable to access
- Network equipment hard core technology insider router Chapter 14 from deer by device to router (middle)
- flutter —— 布局原理与约束
- Set the position of the prompt box to move with the mouse, and solve the problem of incomplete display of the prompt box
- 使用Lombok导致打印的tostring中缺少父类的属性
- js运用扩展操作符(…)简化代码,简化数组合并
- QT (five) meta object properties
- [正则表达式] 匹配多个字符
- Network equipment hard core technology insider router Chapter 13 from deer by device to router (Part 1)
猜你喜欢
随机推荐
Spark 3.0 adaptive execution code implementation and data skew optimization
Summer Challenge harmonyos realizes a hand-painted board
聊聊面试必问的索引
Network equipment hard core technology insider router Chapter 17 dpdk and its prequel (II)
js寻找数组中的最大和最小值(Math.max()方法)
/dev/loop1占用100%问题
Implement custom spark optimization rules
【剑指offer】面试题54:二叉搜索树的第k大节点
/Dev/loop1 takes up 100% of the problem
Leetcode 240. search two-dimensional matrix II medium
Learn parquet file format
js运用扩展操作符(…)简化代码,简化数组合并
语音直播系统——提升云存储安全性的必要手段
Network equipment hard core technology insider router Chapter 11 Cisco asr9900 disassembly (V)
js使用for in和for of来简化普通for循环
Network equipment hard core technology insider router Chapter 18 dpdk and its prequel (III)
NPM install error unable to access
Network equipment hard core technology insider router 20 dpdk (V)
Spark3中Catalog组件设计和自定义扩展Catalog实现
QT (five) meta object properties
















