Multi-thread programming in. Net-1-thread mechanism Overview

Source: Internet
Author: User

Reading directory

I. Basic thread concepts

2. Process example

Iii. when to use a thread?

Iv. disadvantages of using threads

I. Basic thread concepts

1. The thread isProgramThe basic atomic unit of execution. When we went to school, we learned that a molecule contains countless atoms. Atoms cannot be separated. atoms are the smallest unit, A process can be composed of multiple threads. There are many battles in the war of liberation, such as the Huai Hai and Huai Hai battles. we can regard the Huai Hai Campaign as a process, many small battles in the Huaihai campaign are regarded as threads. From this perspective, why is efficiency improved? A process is divided into multiple threads, and a battle is divided into several small battles, so that they can be executed concurrently and asynchronously to improve the running efficiency. At the same time, both battles start at the same time, concurrent execution is not performed at the same time (CPU usage), because the CPU can only run one program in a certain period of time, that is, there is a general command headquarters in the haihai battle, the General Command Headquarters can only command one battle in a period of time. When the two battles start at the same time, only one battle can be commanded at a certain point in time, but it can be cross-directed, therefore, on the surface, the two battles started at the same time. That is to say, one thread can only occupy the CPU at a time point, but they compete for the CPU (equivalent to cross command) and feel like they are running at the same time.

2. A thread is the basic atomic unit of program execution. A process can be composed of multiple threads.

3. correct use of threads can greatly improve the performance and running efficiency of applications. The implementation mechanism is to divide a process into multiple threads and then let them run concurrently asynchronously, to improve application performance and operational efficiency.

4. Concurrent execution is not performed at the same time (CPU usage). At any time, only one thread can occupy the CPU, But they compete for the CPU more frequently. It seems that they are running at the same time.

2. Process example

Assume that there is a task. One task is to blow up the enemy's artillery battalion. A three-step battle can be completed. A1: Find the map of the artillery battalion, B1: lurks to the enemy camp, C1: blow up the artillery battalion. Task 2 is to destroy the enemy's highlands. The battle can be completed only after three steps are completed. A2: locate the dead corner of the highlands, B2: intercept leader to the dead corner of the enemy's highlands, C2: blocks the enemy. It is assumed that every step takes 10 minutes.

1. If synchronous execution is performed

Assume that machine-gun is used for both the first and second tasks. When Task 1 is executed, the second task needs to wait because machine-gun is required and cannot be completed without machine-gun, the two tasks will have to wait until they are handed over to my machine gun for execution. It takes one hour for the two tasks because they need a resource.

2. If asynchronous execution

Assuming that the first task and the second task respectively use different tools, they do not need to wait for each other and execute concurrently. The two tasks can be completed in 30 minutes, because they need different resources.

So it is obvious that the second asynchronous execution is more efficient.

Iii. when to use a thread?

1. assume that both tasks use one resource, such as accessing files and IO output, and do not use Asynchronous threads for execution, because they will seize Io resources at the same time, the efficiency of the entire program will be reduced, and they will compete for a resource such as I/O During Concurrent execution. In this case, the CPU will continue to execute someAlgorithmWhich thread has a high priority and consumes time? However, if the resources used are uniform, I/O output is used, and I/O is not used, concurrent asynchronous execution can be used, is to execute at the same time.

2. generally, if multiple threads need to seize a resource or several resources during execution, do not use concurrent asynchronous execution because they are concurrent execution and may compete for a resource at the same time, at this time, will this resource be for you or for him? At this time, the CPU will execute the resource allocation algorithm to determine which thread has a high priority, which takes time, for example, when we get up and go to work in the morning, we can use a microwave oven to heat milk and wash our faces. This is two concurrent operations that can be performed at the same time, because different resources are used, one of which is the microwave oven, one uses water. If you want to use a microwave oven for both hot milk and hot bread, either hot milk, hot bread, hot bread, or hot milk, cannot be executed concurrently, because these two behaviors use the same resource, that is, the microwave oven is used.

3. If two tasks use not one resource, they can be executed using an asynchronous thread. If the system resources required by multiple threads are uniform, they can be concurrently executed asynchronously.

4. While downloading files, listening to music seems to be executed at the same time, but in fact, the CPU can only run one task at a time point, but it seems to be running at the same time in a large period of time.

Iv. disadvantages of using threads

1. Because the context information of each thread to be saved will be used in the memory, it will be subject to memory restrictions.

2. Tracking a large number of threads will occupy a large amount of processor time.

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.