Thread Synchronization and thread asynchronous, thread synchronization thread asynchronous

Source: Internet
Author: User

Thread Synchronization and thread asynchronous, thread synchronization thread asynchronous

The thread-based task execution methods can be divided into thread synchronization and thread Asynchronization. For ease of understanding, "synchronous thread" is used in the subsequent description to refer to threads related to thread synchronization. Similarly, "asynchronous thread" is used to represent threads related to thread Asynchronization.

Thread Asynchronization solves the problem that interface controls cannot be used when time-consuming tasks are executed. For example, if you create a thread to specifically execute time-consuming tasks, and other tasks such as interface control response are handed over to another thread for execution (usually executed by the main thread ). In this way, switching between two threads within a short time (time slice) by the thread scheduler can simulate the effect of multiple tasks being executed simultaneously.

Thread Asynchronization is usually performed by creating multiple threads to execute multiple tasks, and multiple working lines start working at the same time. Similar to the simultaneous development of multiple vehicles on a wide road, they do not interfere with each other (readers must understand that there is no "at the same time" in essence, and it is only a blind eye for the operating system. However, this blind-eye method is useful for improving the interaction between our programs and users, and enhancing the program friendliness, isn't it ).

Before introducing thread synchronization, we first introduce a concept closely related to this-concurrency issues.

As mentioned above, threads are independent execution units that can access Shared data. That is to say, in a program with multiple threads, each thread can access the same shared data. If you think about it, you will find that there may be problems: Because the thread scheduler will randomly suspend a thread (switching between threads described earlier ), so when thread a's access to shared data D (modification, deletion, and other operations) is suspended, and thread B Just accesses data D, then thread B accesses an unstable data. In this way, it is very difficult to find bugs. Because they occur randomly, the results are unpredictable, and such bugs are difficult to reproduce and debug. This is the concurrency issue.

To solve the concurrency problem caused by multi-threaded joint access to a shared resource (also known as mutex access), thread synchronization came into being. The mechanism of thread synchronization is simply to prevent multiple threads from simultaneously accessing a shared resource. The method is very simple. Mark the part of the Code that accesses a shared resource. When the program runs to a marked place, CLR (the specific content can be ignored first, as long as you know that it can be controlled) adjust each thread: if an existing thread is accessing a resource, the CLR suspends other threads that access the resource until the previous thread ends access to the resource. This ensures that only one thread accesses the resource at a time. For example, if a resource is placed on an isolated island connected to only one zhuanqiao, if you want to use the resource, You have to queue up one by one. The previous one is back, and the next one is back, the previous one did not return, and the latter was on standby.

Here we will simply describe the basic concepts and principles, so that we won't be confused when looking at the subsequent programs. The following section describes how to write code.

Related Article

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.