Java Concurrency Programming--synchronized

Source: Internet
Author: User

Synchronized is a keyword in Java that is called a built-in lock or monitor lock in concurrent programming. When you use it to decorate a method or a block of code, you can guarantee that at most one thread at a time executes the code.

Java's built-in lock is equivalent to a mutex, where at most one thread can hold such a lock, the synchronous block of code protected by the lock is atomically executed, and multiple threads do not interfere with each other when executing the code.

However, because the lock-protected synchronization block code is accessed in serial form, that is, multiple threads access the object exclusively, which also results in concurrency undesirable if the lock-protected synchronization code block is scoped.

Here it is necessary to briefly talk about the principle of synchronous implementation of built-in locks: Every object in Java can be used as a lock, and the synchronized lock actually exists in the Java object Header (which is why it is called a built-in lock).

And according to the method of synchronized modification or the method block different, its lock form also varies.

    • For the normal synchronization method, the lock is the current instance object;
    • For the static synchronization method, the lock is the class object of the current classes;
    • For a synchronous method block, the lock is an object configured within synchronized brackets;

The following is a simple example of a synchronized that acquires different locks depending on the object it modifies.

 Public classTest {/*** Adornment method, the instance of the class is locked, all synchronized methods of the instance must wait for the current lock to be released before accessing*/     Public synchronized voidsyn1 () {//TODO            }        /*** Synchronous static method, the class object is locked, the synchronized method of other instances of the class must wait until the current lock is released to access*/     Public synchronized Static voidsyn3 () {//TODO    }    /*** Synchronizes the specified object, locks the object, and other synchronized methods that use the object must wait until the current lock is released to access*/     Public voidSyn4 () {
obj represents only the specific object name to be decoratedsynchronized(obj) {}}/*** Synchronizes the specified class, locks the class object, and the synchronized method of the other instance of the class must wait until the current lock is released to access*/ Public voidSyn5 () {
ClassName represents only the specific class to be modifiedsynchronized(ClassName.class) { //TODO } }}

In addition, the implementation of synchronization is a significant cost of the system, and may even cause a deadlock (lock nesting), so as far as possible to avoid unnecessary synchronization control. In addition, for concurrent performance considerations, when actually using the Synchronized keyword, it is important to make sure that the synchronized protected synchronization code block is reasonable in size.

Why focus on synchronized this keyword?

    1. As mentioned in Java concurrency Programming, synchronized, as a built-in property of the JVM, can perform optimizations such as lock-out optimizations for thread-closed lock objects, eliminate the synchronization of built-in locks by increasing the granularity of locks, and the possibility of using class library-based locks to implement these functions is unlikely.
    2. The performance of the built-in lock has been improved in JAVA6 and is already closer to Reentrantlock. (and Reentrantlock in the Java5 is far from winning)
    3. Built-in locks are compact and less risky (Reentrantlock may forget to call unlock)

So when the built-in lock can meet the requirements, use the built-in lock whenever possible, but only if the built-in lock does not meet the requirements (such as interruptible, timed, polling, fairness, etc.). In simple terms, consider using reentrantlock when there is a need for more precise control over the acquisition and release of Locks.

Java Concurrency Programming--synchronized

Related Article

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.