A word line and finished heavyweight lock, spin locks, lock lightweight, biased locking, pessimistic, optimistic locks, etc.

Heavyweight lock? Spin lock? Adaptive spin lock? Lightweight lock? Biased locking? Pessimistic lock? Optimistic locking? Implementation of a method ye so hard, full lock.

This article today, the popularity of these locks give you exactly what their origin, what's the relationship between them, what's the difference.

Heavyweight lock

If you learn too many threads, then you know for sure lock this thing, as to why you need a lock, I will not give you popularity, it is as you already know the.

We know that we have to enter a synchronized, thread safe method is to need to get a lock of this method, this method when you exit, it will release the lock. If you can not get the lock, it means that there are other threads in the implementation of this method, then we will immediately enter the state blocked, the waiting thread releases the lock that holds the lock, and then we wake up from a blocking state, we go to acquire a lock of this approach.

This not obtain the lock immediately locks into the blocked state, which we call the heavyweight lock.

Spinlocks

We know that the thread into the blocked state from running state of the process is very time-consuming, because not only need to save the execution state of the thread at this time, context and other data, but also to convert user mode to kernel mode. Of course, the thread from blocking state wake up, too, is very time-consuming.

I said threads get locked, it will immediately enter the blocked state, but the reality is that although at this moment it can not get the lock, it may be the next 0.0001 seconds, there are other threads to release the lock. If it's slow .0001 seconds to come and collect the lock, then you can probably get a smooth, do not need to go through the blocking / wake up this process took time.

However heavyweight lock hole is so, it is unwilling to wait for it, it is to get immediately into a blocking state. To solve this problem, we introduced yet another willing to wait for some time --- lock spin lock.

Spin locks is that if this time can not get the lock, it does not immediately enter the blocked state, but wait a while to see if this time there is no other person to release this lock. How so what? This is similar to thread the short loop in there, if a certain number of cycles also can not get a lock, then it will enter the blocked state.

As is the circular wait times, this can be manually specified a number.

Adaptive spin locks

We said above spin lock, each thread loop waiting times are the same, for example, I set 100, then the threads do not get locked in the air after 100 cycles, will enter the blocked state.

The adaptive spin lock Niubi, it does not need to specify our human cycle several times, it will be judged itself to be recycled several times, and the number of times each thread may cycle is not the same. The reason to do so, mainly because we feel that if a thread a while ago to get over this lock, or before it got too often this lock, then we think the chances of it again to get the lock is very large, so the number of cycles It will be more.

And if some threads never get over this lock, or that rarely get, then we believe that the probability to get it again is relatively small, so we just let the number of cycles less. Because you short cycle there is very CPU intensive.

So this can be adjusted according to the number of cycles spin lock state of the thread recently acquired lock, which we call adaptive spin lock.

Lightweight lock

Above we introduced three kinds of locks: heavyweight, spin locks and adaptive spin locks, they have a feature that is entering a method and they will add a lock to exit a method when it released correspondence locks.

The reason for the lock, because they are afraid of when this method of execution, was secretly others came in, it can only lock to prevent other threads in. This is equivalent to a time to leave his room, locked the door and have people come back and then release the locks.

This is too much trouble, if you simply do not lock thread to compete with them, that they are not locked up for nothing? You know, this process is the need to lock the operating system this big brother to help, is very time consuming. To address this quick to lock the overhead of lightweight lock appeared.

Lightweight lock think, when you execute a method in which, in fact, it was just too few to perform this method, so that when we enter a method of time simply do not lock, we just need to do a mark on it, that is to say, we can use a variable to record at this time if anyone in the implementation of the method. That is, if nobody in the implementation of this method, when we enter this method, the use of CAS mechanism, the state of this approach has already been marked for execution, when exiting the method, in this state changed to no one in executed.

The reason to use CAS mechanism to change the state, because we change this state, not an atomic operation, so it is necessary CAS mechanism to guarantee atomic operations.

Obviously, compared lock operation, the use of CAS to change the operating state, spending much smaller.

However, it may be said, no idea who to compete, and that is what you say it, that if in case someone say competition? That is, when a thread to execute a method when the method in which it has been executed.

If you really have competition, we will think lightweight lock is not suitable, we will upgrade the lightweight heavyweight lock locked.

So lightweight lock suitable for use in that situation multiple threads compete a little lock appears, that is to say, more suitable for the kind of thread is always a time lag to get the lock case.

Biased locking

Biased locking is even more Niubi, we already feel lightweight lock has been light enough, however, tend to lock more easy, biased locking think you lock every time you enter a lightweight method need to use CAS to change the state, need to quit changes much trouble.

Biased locking think, in fact, for a method, there are two threads is rarely performed, just kept on going, in fact, also a thread in the implementation of this method only, the case is equivalent to single-threaded, single-threaded actually, it no need to lock up.

But after all, the actual situation of multi-threaded, single-threaded but they think of it, so it, tend to lock into a method when handled this way: If this method no one had come in, then enter a thread for the first time this method of time, CAS will use the mechanism, this method is marked as someone executed, and lightweight lock locking somewhat similar, and will put the thread ID is also recorded in, the equivalent of a record in which thread execution.

Then, this thread exits but this method when it does not change the state of this approach, but quit directly, lazy to change, because it believes that in addition to its own thread, other threads to execute this method does not .

Then when this thread want to enter this method again when the judge will look at the state of this method, if the method has been marked as someone executed, and the thread ID is your own, then it directly into the implementation of this method, what do not do

You see, how convenient, for the first time into the CAS mechanisms need to set out on what the future would not have done, directly into the exit.

However, the reality is always cruel, after all, the actual situation is multi-threaded, so if there are other threads to enter the way to do that? If that happens, other thread ID is not a look yourself this approach, this time to explain, there are at least two threads to be executed this methodology, which means biased locking is no longer applicable, this time will be from bias lock escalation is a lightweight lock.

So, biased locking apply to the kind, always only one thread in the implementation of a method of oh.

Here I explain for the next, in order to facilitate understanding, I will lock and lightweight biased locking when, in fact, is to simplify a lot, otherwise involve the object's internal structure, layout, and I think those pulled out, you may want to faint, so I generally talk about their works.

Optimistic and pessimistic locking lock

Initially we said three locks, heavyweight lock, spin locks and adaptive spin locks, before entering a method, you must first add a lock, such as we call it pessimistic locking. Pessimistic locking always thought, if you do not lock in advance, it would crash, this idea really pessimistic point, it is estimated that the source of the pessimistic locking.

The optimistic lock on the contrary, be deemed not locked all right, we can not lock first, if a conflict arises, we are looking for a solution, such as CAS mechanism, the above said the lightweight lock is optimistic lock. It will not immediately locked, but there was really waiting for the conflict, in looking for a solution.

to sum up

Here also roughly finished, briefly popularized the moment, we have to focus on understanding their origin principle. Each lock has its applications and their respective advantages and disadvantages, if given the chance, I give you to talk about their own scenarios, advantages and disadvantages of this interview, seemed to also be frequently today, first wrote Here.

You can talk about the advantages and disadvantages of these locks, oh, for example, compared with the heavyweight lock, the problem of what happens spin lock capacity leads? Optimistic and pessimistic locking lock of comparison? We can also talk about the comments area Le, these are sure to get to know oh.

Guess you like

Origin www.cnblogs.com/cangqinglang/p/10977451.html