Often encountered in the work of the need for concurrent programming instances, has not time to organize, now empty, personal collation of the concurrent understanding.
several misunderstandings about concurrent programming
- misunderstanding one: Concurrent programming is multithreading
In fact, multi-line is only one of the forms of concurrent programming, there are many more practical and convenient concurrent programming techniques in C #, including asynchronous programming, parallel programming, TPL data flow, responsive programming, etc.
- Myth Two: Only large server programs need to consider concurrency
Large programs on the server side respond to requests for data from a large number of clients, with the full consideration of concurrency. However, desktop programs and mobile applications such as mobile phones and tablets also need to consider concurrent programming because they are directed towards end users, and now users are increasingly demanding the experience. The program must be able to respond to the user's actions at any time, especially when working in the background (reading and writing data, communicating with the server, etc.), which is one of the purposes of concurrent programming.
- misunderstanding three: Concurrent programming is complex and must master many of the underlying technologies
C # and. NET provide many libraries, and concurrent programming has become much simpler. In particular,. NET 4.5 introduces a new async and await keyword, which minimizes the code for concurrent programming.
Concurrency
The simplest and most straightforward explanation: doing multiple things at the same time , this explanation indicates concurrency, and the server application takes advantage of concurrency to respond to the second request while processing the first one. As long as you want the program to do multiple things at the same time, you need concurrency. So almost every software program will benefit from concurrency.
Multithreading
Many people see concurrency will think of multithreading , here to explain, multithreading it is only a form of concurrency, it takes a number of threads to execute the program, which is divided into the main thread and sub-thread. Executing the first sentence of the program will open a main thread, the main thread can create child threads to implement concurrency. Multithreading is a form of concurrency, but not the only form. In fact, using the underlying thread type directly does not work in modern programs. Using a high-level abstraction is more powerful than old-fashioned multithreading. Higher efficiency. The thread or BackgroundWorker is no longer used here. For example, your code enters new Thead (), which indicates that the code in the project is obsolete. However, do not think that multithreading has been completely eliminated! Because the thread pool requires that multithreading continue to exist. The thread pool holds the queue for the task, which can be adjusted to suit its needs. Correspondingly, the thread pool produces another important concurrency form: Parallel processing
Parallel Processing
Divide the large number of tasks that are being performed into small chunks and assign them to multiple threads running concurrently. In order to maximize the efficiency of processor utilization, parallel processing (or parallel programming) uses multithreading. When modern multi-core CPUs perform a large number of tasks, it is clearly unreasonable to perform all tasks with one core, while others remain idle. Parallel processing divides the tasks into small chunks and assigns them to multiple threads, allowing them to run independently on different cores. parallel processing is one of multithreading, and multithreading is a kind of concurrency . In modern programs, there is a very important concurrency type that many people are unfamiliar with: Asynchronous Programming
Asynchronous Programming
A form of concurrency, and a multithreaded sibling, which generally employs a callback (callback) mechanism to avoid unnecessary threading.
In. NET, the new version has task and task. Previously, it was usually a callback or event. The core idea of asynchronous programming is the asynchronous operation: the initiated operation will be completed after a certain period of time. When this operation is executing, the original thread is not blocked. The thread that initiated the operation can continue to perform other tasks. When the operation is complete, his callback function is notified so that the program knows that the operation has ended NET4.5 already supports async and await, making async as easy as synchronizing programming. The purpose of the await keyword is to start a task that will be executed (the task will be executed in the new thread) and return immediately so that the function in which the await is located is not blocked. After the task completes, continue executing the code after the await keyword
Responsive Programming
A declarative programming pattern in which the program responds to events. If you think of a program as a large state machine, the behavior of the program can be considered as a response to a series of events, that is, each time you change an event, it updates its state. It sounds abstract and empty, but it's not. With the modern program framework, responsive programming has been widely used in practical development. Responsive programming is not necessarily concurrent, but it is closely related to concurrent programming.
Typically, a concurrent program uses multiple techniques. Most programs use at least multithreading (through the thread pool) and asynchronous programming. To boldly mix and match various concurrent programming forms, use the appropriate tools in each part of the program.
The copyright of this article is shared by the author, welcome to reprint, must retain this paragraph statement, and give the original link, thank you!
C # Concurrent Programming Examples Tutorial-Overview (01)