Multi-threaded and synchronized keywords

Source: Internet
Author: User

Before writing this article I read some articles about multithreading synchronization, from their articles summed up a lot of essence, also found some shortcomings. Many articles are analyzed from the following four aspects.

1. Synchronized modifies a non-static function, such as Synchronized method ()

2. Synchronized modifies a static function, such as static Synchronized method ()

3. Synchronized modifies an object, such as synchronized (obj) {}

4. Synchronized modifies a class, such as synchronized (Object.class) {}


In this paper, I want to analyze multi-threaded synchronization from a new angle: Object lock, Class lock.

Object Lock:

Question 1: When does a thread acquire an object lock on an object, and when does it release the object lock?


When a thread executes obj.synchronized method () or executes the synchronized (obj) {} code block, the thread obtains an object lock on the object obj. When the synchronization method or the code block finishes executing, the thread releases the object lock of obj.

Conclusion 1:Thread 1 Gets the object lock for object obj, and other threads need to wait until thread 1 releases the object lock if they want to get the object lock

Conclusion 2: Before thread 1 executes obj.synchronized method (), the object lock of obj is obtained first

Conclusion 3:The object lock for obj is obtained before thread 1 executes the synchronized (obj) {} code block


class Lock:

Question 1: When does a thread get a class lock for a class? When did you release the lock?

When a thread executes h.static synchronized method () or executes the synchronized (H.class) {} code block, the thread obtains the class lock of that class H. When the static synchronization method or the synchronization code block finishes executing, the thread releases the class lock of the H.

Conclusion 1: After thread 1 obtains the class lock of Class H, other threads need to wait until thread 1 releases the class lock if they want to acquire the class lock.

Conclusion 2: Before thread 1 executes h.static synchronized method (), the class lock of Class H is obtained first.

Conclusion 3:Before thread 1 executes the synchronized (H.class) {} code block, the class-H class lock is obtained first.

class locks and synchronous locks are not affected by each other

1, if an object of Class H H1 lock is idle, when a thread obtains class-H lock, the H1 object lock is still idle

2, if the class H lock is idle, when a thread obtains an object of Class H H1 object lock, H's class lock is still idle


The difference between a common method and a synchronous method or a synchronous code block

synchronous methods or synchronous blocks of code need to be executed by the thread that executes them (class or object locks) before they can be executed, otherwise they will block until the corresponding lock is obtained. the normal method, once invoked, is executed immediately, with no relation to whether the current thread obtains a synchronous lock.


If a piece of code F needs to obtain the object lock of Class A A1 object locks to execute, a piece of code G needs to obtain the object of Class A A2 object lock in order to know the line, ask when thread 1 executes F, the simultaneous execution of thread 2 G will be blocked?

The answer is no blocking. Because thread 1 only obtains the A1 object lock when the F is executed, A2 's object lock is still idle, so thread 2 can get A2 object lock immediately when the G is executed.








Multi-threaded and synchronized 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.