Java synchronized keyword

Source: Internet
Author: User

In Java, the synchronized keyword can be used as a method modifier orCodeBlock, for example:

Public synchronized void dosomething ()

Synchronized (semaphore ){
...
}

The Methods modifier has the following advantages:

    1. The synchronization policy is explicit and can be written to the API documentation.
    2. The method is relatively basic, thus reducing the opportunity of deadlock, although it is still easier to create

Disadvantages:

    1. Low method synchronization Performance
    2. Generally, developers do not have a clear and comprehensive lock policy when using the synchronized keyword, which leads to a lot of synchronization content.

The advantages of Synchronous Code blocks include:

    1. You can easily control the content to be locked.
    2. Better performance than method Synchronization

Disadvantages:

    1. It is easy to form nested synchronization code blocks, which may also cause deadlocks.
    2. The synchronization policy cannot be docized. You need to check the method code to understand it.

Http://codemonkeyism.com/java-interview-question-synchronized/.

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.