What is thread synchronization and what is thread async? Advantages and disadvantages of synchronization

Source: Internet
Author: User

Thread synchronization: Multiple threads accessing the same resource at the same time, waiting for resource access to end, wasting time, inefficient

Thread Async: Access other resources while idle waiting while accessing resources, implement multithreading mechanism

Asynchronous processing is, you ask me questions now, I can not answer you, wait for me to take the time to deal with your problem. Synchronization is not the reverse, the synchronization information is processed immediately-until the completion of information processing to return the message handle; asynchronous messages will be processed in the background after a period of time--and return the message handle before the end of processing

Differentiate between synchronous and asynchronous
A process initiates multiple unrelated threads that are related to each other asynchronously.
Synchronization must be executed before other operations can be performed, and asynchronous operation is arbitrary
Advantages and disadvantages of synchronization: Resolves thread security issues. Cons: Every time there is a judgment lock, reducing the efficiency. But between security and efficiency, the first thing to consider is security. The premise of synchronization one, multiple threads to perform the time required synchronization, if it is a single thread does not need synchronization. More than one thread is using the same lock during execution. If it is, it is synchronous. Otherwise, it is not synchronized. Synchronzied (obj) {} The same synchronzied (new Object) {} is not the same lock
Synchronize the code that needs to be synchronized in run (), and do not put it in the synchronization code block if there is no synchronization required for the code.

There are two representations of synchronization:

1, synchronous code block, the code that is synchronized keyword encapsulation is synchronous code block, 2, synchronous function, the function that is modified by the synchronous keyword is the synchronization function.

The synchronization code block can be any object, creating a good one lock object before executing it. So where is the lock on the sync function? The lock of a synchronization function is the object that invokes the synchronization function, which is this.

If the synchronization function is modified by static, then the lock of the synchronization function is the class file object formed in the heap memory in this class. This is not necessarily the object of the class, there must be a byte-code file object for that class alone.

What is thread synchronization and what is thread async? Advantages and disadvantages of synchronization

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.