Treasure map, a bunch of problems caused by lightweight locks puzzled (Synchronized lock upgrade) ~


background

Take a look at the principle of synchronized upgrade, the results dug such a treasure map:
Here Insert Picture Description
There is a puzzling, is the last of lightweight lock is released, why wake up other threads here? Expands to many places heavyweight lock after lock release failed here?

Finally, learn to draw the human figure
the god blog URL: https: //blog.dreamtobe.cn/
blog Signature:

I will have a life no remorse, in the future, we must accomplish something.
My life will not regret in the future, we must be successful.

Regrettably

The figure seems to lightweight when biased locking lock step is less lock owner pointer to the object, but I think the main thing is that the three-step labeling it.

When thread 1 release, why, when failure is to awaken those threads to be suspended?

Because if it fails, then lock has been inflated.

When a running thread, the thread 2 comes in, this time replacing the thread 2 when the pointer cas fail, so the thread 2 spin operation, already at this time does not reach a certain success, the thread 2, the expansion will cause the lock to lock heavyweight , which may itself (thread 2) will be suspended.

When thread 1 release, has been expanded into a heavyweight lock, while heavyweight lock will lead to mark word pointing heavyweight lock monitor, change the value of the mark word lock object, so when the thread 1 released by attempting to operate cas Displaced mark word switched back to object mark word failed, so they need to wake up again to compete in the heavyweight lock.

But I think there may be an exception, if the thread 2 spin process (not yet reach the maximum number of spins) at this time does not expand into a heavyweight lock, so this time if the thread 1 is released, then it can be successfully released , and spin the thread 2 is also still not been suspended, so do not wake up thread 2

Also like a long time, finally found the answer in the book is still in.

In the "Java concurrent programming art" on page 15 of the book - a lightweight lock and expansion of a flow chart, got the answer

Expanded flowchart

Lightweight lock

Here Insert Picture Description

Biased locking
Here Insert Picture Description

Why copy mark word?

Actually very simple, because do not want to synchronize together on the lock and unlock this underlying operating.

After completion of the copy object mark word, JVM step switching operation done pointer, i.e. a first rectangular frame orange content of the process.
The object mark word in the lightweight lock pointer to stack pointer lock record is located, it is to allow other threads to know that the object monitor is already taken.
lock record in the object mark word pointer to the owner's role is to take over the operation in the process, identifying which object is locked.

Summarized by cas

https://www.bbsmax.com/A/A2dmM7lbde/

doubt? Lightweight lock in the end is more than two threads or two threads compete for the same lock expands heavyweight lock?

Before looking blog, more than a mention 如果有两条以上的线程争用同一个锁,那轻量级锁就不再有效,要膨胀为重量级锁,, but the picture above to see two threads can be named as a heavyweight lock expansion.

Check out books in the "in-depth understanding of the Java Virtual Machine," is so that I, well I guess right, more like a blog written in exactly the same words, mostly copied from the book over.

If more than two threads competing for the same lock, lightweight lock that is no longer valid, is to be expanded heavyweight lock, the lock status of the flag value becomes "10", Mark Word is stored in pointing heavyweight (mutex) pointer.

See the blog post and I like the confusion, many people support the view, temporarily references come, as a reference, because I really have not found other better answer may indeed be ambiguous, right here, not this dead end a.

Lightweight lock that there is competition, but the degree of competition is very light, usually two threads for the same lock operations will stagger, or wait a little (spin), another thread releases the lock. But when in addition to the thread owns the lock spin more than a certain number of times, or a thread holding a lock, a spin, while another third visit, lightweight lock inflation heavyweight lock, the lock heavyweight the threads are blocked, preventing the CPU idle.

Original link: https: //blog.csdn.net/choukekai/article/details/63688332

Finally, here to say quite good重量级锁使除了拥有锁的线程以外的线程都阻塞,防止CPU空转。

to sum up:

From my knowledge is indeed two, but the textbooks say it should be justified, for the time being understood as: three threads, one holding a lock, a spin-long (why did not take into account the adaptive spin it, silent), the third thread, and this time the virtual machine to inform themselves, this situation is bound to have a spin for a long time, so the judge should be expanded, and we know that is not the way spin n times (default 10 times) not obtain a lock, you quit? To do so is to protect the lock.

In the end when the expansion heavyweight lock?

  1. Spin n times (default 10 times, there are adaptive jdk1.6 after a spin lock) can not get a lock, this time there should be two threads
  2. Three threads, one holding a lock, a spin for a long time, the third thread, and this time is expanded
  3. Lightweight release lock time, this should be strictly and Article 1 of the same, because this situation is: if the current thread 1 release, the thread 2 during the execution of the thread 1, the spin n times failed, hang himself, and for the expansion heavyweight lock because expansion for a heavyweight lock, so mark word points to the heavyweight monitor pointer lock, thread 1 cas will repalced mark word mark word replacement will object when the failure of so many blog said after the failed expansion heavyweight lock, in fact, it is wrong, before this has been expanded as a heavyweight lock, so the release failed . So in essence here, or spin thread 2 failure cause swelling, which is the first.

What is the point of global security?

Global security requires the release point in time tend to release lock, what is the point of global security?

On this point there is no byte code being executed

At this point it does not execute any code

Global Security Point (Safe Point): Global security point of understanding will involve some knowledge of the underlying C language, simple to understand SafePoint here is the location of a threaded Java code may suspend execution.
https://www.jianshu.com/p/0f31a14373a2

Ah it is not very understanding, which literally means to understand, to remember it ~

Supplements

If the update fails, first check the object of Mark Word points to the stack frame for the current thread, if it represents is that this is a lock re-entry, then add to the stack frame for the current thread in a Displaced Mark Word is null, Object reference field points lock Record lock object, to count the number of re-entry, as shown below. If Mark Word inspection target does not point to the current thread's stack frame, the process proceeds to step 6;

Here Insert Picture Description

https://www.loongzee.com/2019/04/25/JavaSynchronized_2/

Supplements Summary: added by re-replace

Published 545 original articles · won praise 3209 · Views 2.56 million +

Guess you like

Origin blog.csdn.net/dataiyangu/article/details/104979817