当前位置:网站首页>Understand the locks that can't
Understand the locks that can't
2022-07-24 00:55:00 【wzf6667】
Lock data reorganization
Java Multithreading wait() Methods and notify() Method
These two methods appear and are used in pairs , To implement these two methods , There is a premise that , The current thread must get its object's monitor( Be commonly called “ lock ”), Otherwise it will throw IllegalMonitorStateException abnormal , So these two methods must be called in the synchronization block code .
Reentrant lock
You can get your own internal lock again . For example, a thread obtains the lock of an object , At this time, the object lock has not been released , When it wants to acquire the lock of this object again, it can still acquire , If it's not lockable for re-entry , It causes a deadlock . The same thread gets the lock every time , The counters of the lock are all self increasing 1, So wait until the lock counter goes down to 0 To release the lock .
Deadlock of non reentrant lock
Non reentrant locks cause deadlocks : Threads A Have , Threads B Waiting for the , At this point, the thread A The method in calls again , If it is not reentrant ,A The method of cannot be executed, so the lock is not released ,B If you don't get it, you can't implement it , It causes a deadlock .
Fair lock and unfair lock
Fair lock
It indicates that the order in which the thread obtains the lock is allocated according to the order in which the lock is added , And first come, first served , First in, first out .
Not fair lock
Represents the preemption mechanism for obtaining locks , It's a random acquisition of locks , Unlike the fair lock, the one who comes first may not get the lock ,
It's possible that I can't get the lock all the time , So the result is unfair .
It's like a group of people waiting to withdraw money , Everyone queuing to withdraw money is a fair lock , Who is the first to get ATM It's unfair who takes the money first .
synchronized and ReentrantLock Method difference
Master the basic knowledge of the above lock , Let's take a look at two different ways to implement locks (java guide) link : link.
① Both are reentrant locks
Both are reentrant locks .“ Reentrant lock ” The concept is : You can get your own internal lock again . For example, a thread obtains the lock of an object , At this time, the object lock has not been released , When it wants to acquire the lock of this object again, it can still acquire , If it's not lockable for re-entry , It causes a deadlock . The same thread gets the lock every time , The counters of the lock are all self increasing 1, So wait until the lock counter goes down to 0 To release the lock .
② synchronized Depend on JVM and ReentrantLock Depend on API
synchronized It depends on JVM Realized , We also talked about The virtual machine team is in JDK1.6 by synchronized Keywords are optimized a lot , But these optimizations are all implemented at the virtual machine level , Not directly exposed to us .ReentrantLock yes JDK Level realized ( That is to say API level , need lock() and unlock() Method coordination try/finally Statement block to complete ), So we can check its source code , See how it works .
③ ReentrantLock Than synchronized Added some advanced features
comparison synchronized,ReentrantLock Added some advanced features . There are mainly three points :① Wait for interruptible ;② Fair lock can be achieved ;③ Optional notification can be achieved ( Locks can bind multiple conditions )
ReentrantLock Provides a mechanism to interrupt threads waiting for locks , adopt lock.lockInterruptibly() To implement this mechanism . That is to say, the waiting thread can choose to give up waiting , Deal with other things instead .
ReentrantLock You can specify whether the lock is fair or not . and synchronized Only unfair lock . The so-called fair lock is that the thread waiting first obtains the lock first . ReentrantLock The default is not fair , Can pass ReentrantLock Class ReentrantLock(boolean fair) Construct methods to determine whether it is fair .
synchronized The key words and wait() and notify()/notifyAll() The combination of methods can realize waiting / A notification mechanism ,ReentrantLock Class, of course , But with the help of Condition Interface and newCondition() Method .Condition yes JDK1.5 Only after that , It has good flexibility , For example, multiple notifications can be implemented in one Lock Object can create multiple Condition example ( Object monitor ), Thread objects can be registered in the specified Condition in , In this way, thread notification can be carried out selectively , More flexible on scheduling threads . In the use of notify()/notifyAll() Method for notification , The thread to be notified is by JVM Select the , use ReentrantLock Class combination Condition Instances can implement “ Selective notice ” , This function is very important , And it's Condition The interface is provided by default . and synchronized Keywords are equivalent to the whole Lock There is only one object Condition example , All threads are registered on one of them . If you execute notifyAll() Method will notify all threads in the waiting state, which will cause great efficiency problems , and Condition Example of signalAll() Method Will only wake up registration in this Condition All waiting threads in the instance .
If you want to use the above functions , So choose ReentrantLock It's a good choice .
( My understanding is that synchronized Wake up (notify) Cannot specify which thread to wake up , and ReentrantLock Can combine condition Instance implements selective notification .)
④ Performance is no longer the standard of choice
volatile keyword
2.1. Let's talk about Java Memory model
stay JDK1.2 Before ,Java The memory model implementation is always from main memory ( Shared memory ) Read variables , There is no need for special attention . And in the present Java Memory model , Threads can save variables to local memory ( For example, the machine register ) in , Instead of reading and writing directly in main memory . This may cause a thread to modify the value of a variable in main memory , Another thread continues to use copies of its variable values in registers , Cause data inconsistency .
To solve this problem , You need to declare the variable as volatile, This is the instruction JVM, This variable is not stable , Every time I use it, I read it in main memory .
To put it bluntly , volatile The main function of keywords is to ensure the visibility of variables, and then another function is to prevent the reordering of instructions .
2.2 Three important features of concurrent programming
Atomicity : One or more operations , Or all operations will be executed and will not be interrupted by any interference , Or all operations are performed , Either not .synchronized Can guarantee the atomicity of the code fragment .
visibility : When a variable modifies a shared variable , Then other threads can immediately see the latest value after modification .volatile Keywords guarantee the visibility of shared variables .
Orderliness : The sequence of code execution ,Java Optimization in compiler and runtime , The order in which code is executed is not necessarily the order in which code is written .volatile Keywords can prevent instructions from reordering optimization .
2.3. say something synchronized Key words and volatile The difference between keywords
synchronized Key words and volatile Keywords are two complementary beings , Instead of being antagonistic :
volatile Keyword is a lightweight implementation of thread synchronization , therefore volatile Performance is definitely better than synchronized Keywords are better . however volatile Keywords can only be used for variables and synchronized Keywords can modify methods and code blocks .synchronized The key word in JavaSE1.6 After that, it mainly includes biased lock and lightweight lock introduced to reduce the performance consumption caused by lock acquisition and lock release, as well as other various optimizations, and the execution efficiency has been significantly improved , Used in actual development synchronized There are more scenarios for keywords .
Multithreaded access volatile Keywords don't block , and synchronized Keywords may block
volatile Keywords ensure data visibility , But it doesn't guarantee the atomicity of the data .synchronized Keywords both guarantee .
volatile Keyword is mainly used to solve the visibility of variables among multiple threads , and synchronized Keyword solves the synchronization of accessing resources among multiple threads .
link : link.
Atomicity : Do you want to finish running or don't run .synchronized To ensure the cpu After the time slice runs out , Do not release the lock object , Other threads cannot acquire locks , Wait until the current thread gets the time slice again , Can continue from the current code ( Program counter ).
边栏推荐
- Table custom table encapsulation
- Memory forensics nssctf otterctf 2018 (replay)
- Tutorial on principles and applications of database system (039) -- MySQL query (I): syntax analysis of select command
- 这是一道大水题
- MySQL data query (select)
- MySQL's heart index
- 【LeetCode第 83 场双周赛】
- Bean Validation自定义容器验证篇----06
- NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘
- T-seda code
猜你喜欢

Memory forensics nssctf otterctf 2018 (replay)

Easy gene | target gene DNA methylation sequencing (target BS)

NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘

网络系统实验:ping不通的问题解决

MariaDB database upgrade version

Off screen rendering & FBO

Idea hot deployment (hot load)

黑馬程序員-接口測試-四天學習接口測試-第四天-Postman讀取外部數據文件,讀取數據文件數據,iHRM項目實戰,員工管理模塊,添加員工,批量運行測試用例,生成測試報告,

mysql 分支语句case报错

如何在自动化测试中使用MitmProxy获取数据返回?
随机推荐
Expérience du système réseau: résoudre les problèmes de ping
What impact does the European "gas shortage" have on China?
Accelerating matrix vector multiplication of special matrices with FFT
Application of SCA on devsecops platform
Graphic pipeline (I) post-processing stage alpha test template test depth test mix
Small farmers also have big goals in the test, and the latest big bat interview summary (constantly updating...)
JS related knowledge
项目场景:nvidia-smi Unable to datemine the device handle for GPU 0000:01:00.0: Unknow Error
Comparison of image preprocessing between pytorch opencv pil
Leetcode set the intersection size to at least 2
【LeetCode第 83 场双周赛】
Classic example of C language - print the input two digits in reverse order
《天幕红尘》笔记与思考(五)强势文化与弱势文化
Idea hot deployment (hot load)
GLIB-CRITICAL g_ file_ test:assertion ‘filename != null‘ failed
Scroll view realizes drop-down refresh (to avoid the drop-down problem triggered by the initial refresh triggered value of true when onload enters the page)
postman测试接口在URL配置正确的情况下出现404或者500错误
The way to access global variables in multi-source file mode (extern usage)
网络系统实验:ping不通的问题解决
There are various signs that apple is expected to support AV1