Synchronized usage based on java, using ynchronized

Source: Internet
Author: User

Synchronized usage based on java, using ynchronized

First, reference: http://www.cnblogs.com/devinzhang/archive/2011/12/14/2287675.html

PS: The reference article is very long, but the content is very rich. If you are a beginner, you must read it with patience.

Second, my personal summary:

The role of the synchronized keyword:

1. In all the synchronized methods of an object, only one unique thread can access these synchronized methods at a certain time point.

2. If a method is synchronized, the synchronized keyword indicates locking the current object (this.

3. If a synchronized method is static, the synchronized keyword indicates locking the class object corresponding to the current object. (No matter how many objects are generated, each class has only one class object)

 

4. multithreading: if it is a member variable: multiple threads will share; if it is a local variable, multiple threads will not share



How to Use the synchronized keyword in Java?

In my understanding
Sychronized is used to prevent more than two threads from simultaneously accessing the same resource during concurrency. The parameters are used for identification. For example, in the first case: When a function sychronized (object A) and B function sychronized (object B) Call function, other threads can no longer access function a, but can access function B. The second case: When a function sychronized (object A) and B function sychronized (Object A) Call function a in a thread, other functions cannot access function, and cannot access function B.
Please correct me.

In java, how to use synchronized?

Synchronized is a synchronous consciousness,

The first is the synchronous method, that is, only one thread can operate on this method at the same time,

Type 2 synchronized (this ){
Read ();
Update ();}
Is a synchronization block,

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.