Common locking mechanism

Dimly lit room, the two men sat opposite,

A long time, the first to break the deadlock male eyes,

Male eyes, know what lock

Handsome man, to know,

Eyes M: What is the lock?

Two programmers play "lock", a man died a death

A protection mechanism, in the case of multi-threaded, ensure the accuracy / consistency of data operations,

Glasses Man: What are the different classification?

Pessimistic locking, optimistic locking, exclusive locks, shared locks, lock fair, unfair lock, distributed lock, spin locks

Eyes M: Tell me optimistic locking Pessimistic locking bar

Two programmers play "lock", a man died a death

Generally speaking like on the database (in fact, the two concepts are part of the computer, do not be misled), said it mysql, pessimistic locking, mainly table locks, row locks lock gap still leaves locks, read locks, because the lock is triggered when a thread is bound to cause obstruction, so called pessimistic

Also optimistic locking mysql actually does not exist in itself, but provides a mechanism for mysql kinds mvcc to support optimistic locking mechanism,

Eyes M: mvcc Editor's Note?

Two programmers play "lock", a man died a death

Only in innodb engine presence, mvcc to meet the transaction isolation, by way of the version number, to avoid competition between different transactions of the same data, said only optimistic locking in transaction level to read uncommitted read committed, to take effect,

Eyes M: What specific mvcc mechanisms?

Multi-version concurrency control to ensure data operations in a multi-threaded process, mechanisms to ensure transaction isolation, the pressure can be reduced lock contention, ensuring high concurrency, this process. At each turn a transaction generates a transaction version number, the data operation will generate a new row of data (provisional), but before submitting to other transactions is not visible success for the update data operations, will this update to the version number of rows of data, the transaction commits successfully, the new version number, the update to this data lines (permanent), so to ensure that the data for each transaction operations are not affect each other, nor problems lock;

Eyes M: So in multiple transactions (operating the same data) concurrent process, who should succeed?

mysql judgment, in fact, who should submit who counted success

Eyes M: Speaking of affairs, and talk about affairs

Two programmers play "lock", a man died a death

Transaction often said that a series of operations as a whole are either succeed or fail, the main characteristics of acid, to achieve the transaction depends mainly on two log redo-log, undo-log, data before every transaction recorded data modifications undo-log before the modified data into the redo-log, made successful use redo-log updates to disk fails then undo-log restore data to transaction data

Glasses men, ah, let me talk about exclusive locks, shared locks it

(嗯,独占,共享,公平,非公平,自旋锁这些都是广泛的概念,很多语言都有,包括操作系统,js的同学请回避)

独占锁很明显就是持锁的线程只能有一个,共享锁则可以有多个

眼睛男:独占可以理解,共享的意义在哪里?

共享锁是为了提高程序的效率,举个例子数据的操作有读写之分,对于写的操作加锁,保证数据正确性,而对于读的操作如果不加锁,在写读操作同时进行时,读的数据有可能不是最新数据,如果对读操作加独占锁,面对读多写少的程序肯定效率很低,所有就出现了共享锁,对于读的的操作就使用共享的概念,但是对于写的操作则是互斥的,保证了读写的数据操作都一致,在java中上述的锁叫读写锁

眼睛男:读写锁的机制是什么呢?(佯攻)

Two programmers play "lock", a man died a death

在java中读写锁(ReadWritelock)的机制是基于AQS的一种实现,保证读读共享,读写互斥,写写互斥,如果要说机制的话,还要从AQS说起,这是java实现的一种锁机制,互斥锁,读者写锁,条件产量,信号量,栅栏的都是它的衍生物,主要工作基于CHL队列,voliate关键字修饰的状态符stat,线程去修改状态符成功了就是获取成功,失败了就进队列等待,等待唤醒,AQS中还有很重要的一个概念是自旋,在等待唤醒的时候,很多时候会使用自旋(while(!cas()))的方式,不停的尝试获取锁,直到被其他线程获取成功

共享与独占的区别就在于,CHL队列中的节点的模式是EXCLUSIVE还是SHARED,当一个线程成功修改了stat状态,表示获取了锁,如果线程所在的节点为SHARED,将开始一个读锁传递的过程,从头结点,向队列后续节点传递唤醒,直到队列结束或者遇到了EXCLUSIVE的节点,等待所有激活的读操作完成,然后进入到独享模式(这部分尽力了,大家还是看源码)

公平与非公平的区别就在于线程第一次获取锁时,也就是执行修改stat操作时,是进队列还是直接修改状态,这是基本的工作机制,详细的估计可以再聊好几集

眼睛男:java 除了AQS 还有其他的锁支持么

Two programmers play "lock", a man died a death

在java中,synchronized关键字,是语言自带的,也叫内置锁,synchronized关键字,我们都知道被synchronized修饰的方法或者代码块,在同一时间内,只允许一个线程执行,是明显的独享锁,synchronized的实现机制?可以参考AQS的实现方式,只是AQS使用显示的用lock.lock调用,而sync作为关键字修饰,你可以认为在synchronized修饰的地方,自动添加了lock方法,结束的地方进行了unlock释放锁的方法,只是被隐藏了,我们看不到。

它本身实现有两部分:monitor对象,线程,工作机制还是线程抢占对象使用权,对象都有自己的对象头,存储了对象的很多信息,其中有一个是标识被哪个线程持有,对比AQS,线程从修改stat,变为修改monitor的对象头,线程的等待区域动 AQS中的队列,变为monitor对象中的某个区域。

Two programmers play "lock", a man died a death

眼睛男:能细说么?

导演,他自己加戏

导演:按照剧本来

锁一直是围绕线程安全来实现的,比如独占锁,它在内存里面的操作是怎么样的

This place involves a concept, memory model (this and jvm not to be confused, The Java memory model used internally in the JVM divides memory between thread stacks and the heap. This diagram illustrates the Java memory model from a logic perspective), the JVM with to distinguish thread stack and heap memory mode, each thread is running, the operating data storage space, there are two, one is the working memory is a main memory, main memory, in fact, jvm heap, working memory is the thread stack, each data manipulation, all the data from the main memory to read the work memory and then subjected to various treatments in the working memory, if modified, will write data back to main memory, and then the other thread the same operation, so the data in working memory and main memory, and out, enjoying themselves, in the case several times, because the order and out of the mess, is not expected to visit the order of threads, it is there has been inconsistent data problems, leading to insecurity multi-threaded; the whole operation also involves CPU, cache and other concepts skipped. . . .

Eyes M: What memory model can talk of

happen-befor principle, Volatile keyword (visibility threads), memory barrier

Eyes M: Oh (counsels counsels)

Two programmers play "lock", a man died a death

happen-befor principles define the law implementation process memory model, like 1 + 1 = 2, can not be broken jvm operating mechanism are dependent on this principle, is jvm Constitution! ! !

When Volatile keyword bit Diao, Volatile modified data after a thread has been modified, will be timely written back to main memory, and then re-acquire other threads, is the new data, it sounds very nice, but Volatile no way to control the order of thread, when a data (new data) is about to be modified to the main memory, just, another thread to read the data (old data) from the main memory, and has conducted a wave of operations, turn data ( updated data) written back to main memory, the whole process (new data) did not play the role of a dime, eventually led to the erroneous data, so after whistling call it a day! ! ! !

Guess you like

Origin www.cnblogs.com/caicz/p/11009474.html