Java Foundation-synchronized usage of keywords

Source: Internet
Author: User

As the name implies is used to synchronize the role of mutual exclusion.

Here's a concise note of how it's used and what it means:

1. When synchronized modifies This or a non-static method or An instance , the synchronized lock is added to this or the instance object reference. For example, a and a are instantiated objects for the main class, a calls the method that is synchronized, and B calls the method that is synchronized, and does not form a mutex. However, the method that the A-object call of different threads is synchronized is mutually exclusive.

Public synchronized void Method () {//...}  public void Method () {synchronized (this) {//..... }  }


2. Unlike 1, when synchronized modifies the class name. class or static method . Even if they are different objects, they will form mutual exclusion.

Class Main {public synchronized static void Method1 () {//...} public void Method2 () {synchronized (Main.class)//}}

Transfer from http://blog.csdn.net/cq361106306/article/details/38736551

Java Foundation-synchronized usage of keywords

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.