Implementation mechanism of Java synchronized

Source: Internet
Author: User

Recently, when doing projects, encountered a lot of synchronization problems.

JAVA provides us with a convenient way to solve the problem of multi-threaded synchronization synchronized keyword

have been very puzzled synchronized how to do the synchronization of what is the principle of implementation?


1 Synchronized--Object lock

All objects automatically contain a single lock, and the JVM is responsible for tracking the number of times the object is locked.

If an object is unlocked, its count becomes 0.

When a task (thread) locks an object for the first time, the count changes to 1.

The count increments whenever this same task (thread) Gets a lock on this object.

Only the task (thread) that obtains the lock first can continue to acquire multiple locks on the object.

Each time the task leaves, the Count decrements, and when the count is 0, the lock is completely released.

Synchronized is based on this principle, at the same time synchronized depending on the number of a single lock technology to determine whether the lock, so there is no (and can not) manually interfere with the acquisition and release of Locks.

Principle.......... If the stack and frame are combined when the method is invoked (if the calling procedure for the method is unfamiliar with the suggestion to see HTTP://WUPUYUAN.ITEYE.COM/BLOG/1157548),

It is not difficult to speculate that the synchronized principle is based on an object in the stack to control a frame,

Therefore, the most common optimization for synchronized is the lock object is not locked method.

In fact, when synchronized acts on a method, the lock is "this",

When acting on a static method/property, the lock is a class with a Yunju band, which is equivalent to the global lock of this class, and locks the corresponding code block when the lock is applied to the general object.

In the JVM implementation in hotspot, the lock has a special name: the object monitor.

When multiple threads request an object monitor at the same time, the object monitor sets several states to distinguish between the requested thread contention List: All threads that request a lock are placed first into the competition queue, a virtual queue, not the data structure of the actual queue. Entry List:entrylist and contentionlist logically belong to the waiting queue, contentionlist are concurrently accessed by threads, and the contentionlist is established to reduce contention for the Entrylist team tail. , the thread that qualifies as a candidate in the contention list is moved to entry list wait set: Those threads that call the wait method blocked are placed in the wait set OnDeck: There can be at most one line at any moment one thread in the competitive lock, This thread is called OnDeck

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.