Java synchronized (1)

Source: Internet
Author: User

1. Problem solving

Multithreading concurrency issues.

2. Principle

3. Attention Points

A. What is the difference between a. Synchronized (class, this, object)?

Synchronized (class) is very special and allows another thread to wait in any place where it needs to get the class as monitor. class and this can be used at the same time .

1. For the instance synchronization method, lock the current instance object;

2. For the static synchronization method, lock the current object's class object;

3. For the method synchronization block, synchronized locks the object that is configured in parentheses.

Class and this are several cases:

Synchronized (Class)

Synchronized (This)

Threads each get monitor, do not wait;

Synchronized (This)

Synchronized (This)

If a different thread monitors the same instance object, it waits, and if a different instance, it does not wait;

Synchronized (Class)

Synchronized (Class)

If different threads listen to the same instance or different instance objects, they will wait.

Java synchronized (1)

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.