Java Synchronization related

Source: Internet
Author: User

When it comes to synchronization, it's about asynchrony, when it comes to synchronous asynchrony, it's about multithreading.

The reason is multi-threading, because it is better to do the control of resources, distribution, but also to make the user experience better

With Android For example, the UI is in the main thread, and the time-consuming operation is executed in other threads, so the result is that you may now need to load the buddy list, but the data for the buddy list is not yet available, but the user can still manipulate the app, and if it's all plugged into the same thread, when it gets the data, Users are also unable to do other things to the app, which is better for the user's experience

And the way this time-consuming operation is performed elsewhere is what we call asynchronous

So what is synchronization, so to speak, now there are many people have your bank account password, all want to withdraw money, everyone wants to take 100 and your balance is only 100, this time if you take money at the same time, will cause data problems or other consequences, that may appear you two have taken 100 pieces, Your account balance is 100, and so on.

In order to avoid this situation, we need to give the line loads lock, of course, where the lock can be discussed, such as a single user at the same time can only be logged in one place, that is, I login to your account password, the other person will not be able to login, or in the need to manipulate the data where the lock, For example, I and you can log in to this account, see information about the account, such as the balance, whether we are at the same time, no impact on the data, and when we all need to withdraw money, only in order, I do the operation of the time you can not execute, and when you do the operation of the time I can not execute. Ensure that the same operation is executed sequentially, which is called synchronous

In order to achieve synchronization, the more common method is to give the line loads lock, that is, the synchronous lock, that is, the Synchronized keyword

As to how to use, the predecessors have written very detailed, here also do not copy, the usual list of a blog post, although he did not write in detail, but I think this article is more easy to understand

Examples of use of multi-threaded synchronous locks

Java Synchronization related

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.