C # multithreading Basics

Source: Internet
Author: User

Taking the opportunity of this examination system, I learned to learn multithreading technology, which is the same as the previous learning process: before learning new knowledge, first, you need to know what the knowledge is, where it comes from, where it is used, and what its advantages and disadvantages are. In this way, you can establish a direction for the knowledge and will not be confused. Before multithreading, you must first understand what a thread is and how a thread is the basic unit of program execution. A process can be composed of one or more threads. The idea of using multiple threads for concurrent processing is multi-thread processing. multithreading does not actually occupy the CPU at the same time. At any time, only one thread can occupy the CPU, but compete for the CPU more frequently, because the CPU usage of each thread is very short, the macro view is performed simultaneously, and the thread priority is not for the process to be executed first, instead, the CPU allocates more time to the thread. Generally, we use multithreading because multithreading is more efficient. If A process needs to complete two tasks: A is A1 → B1 → C1, B is A2 → B2 → C2, for synchronous and asynchronous processing, if the total length is used to represent the time used by the process, Asynchronization takes much less time than synchronization. Therefore, when multi-thread programming is used correctly, it can improve the performance and running efficiency of applications. Context includes all information required for thread execution, including CPU registers and stacks. The application domain is similar to the variable and must have an application scope. The application domain is used to prevent other programs from being stopped due to a program, isolation improves system security and stability. The namespace System. Threading and class Thread cannot be separated because classes and namespaces use multiple threads. The most common functions of Thread: Create Thread: [csharp] // without the Thread ts parameter new Thread (new ThreadStart (ThreadProc )); // with the parameter Thread SuperMan = new Thread (new ParameterizedThreadStart (RunnerWork); // without the parameter Thread ts = new Thread (new ThreadStart (ThreadProc )); // Thread SuperMan = new Thread (new ParameterizedThreadStart (RunnerWork); termination Thread: [csharp] view plaincopyprint? // Terminate the process Thread ts = new Thread (new ThreadStart (ThreadProc); ts. abort (); // terminate the process Thread ts = new Thread (new ThreadStart (ThreadProc); ts. abort (); pause thread: [csharp] view plaincopyprint? Thread. Sleep (1000); Thread. Sleep (1000); Thread suspension and wake-up, because of its own security and stability risks, has become Microsoft's obsolete usage. When is multithreading used? The main advantage of Multithreading is high efficiency and time saving. If the system resources required by multiple threads are relatively uniform, they can be executed asynchronously and concurrently. At the same time, multithreading also has multiple disadvantages: difficult to control and resource theft. Therefore, if multiple threads want to seize one or more resources, it is best not to use Asynchronous threads for execution. Because of concurrent execution, it is very likely to compete for a certain resource at the same time, it will take a lot of time to execute the resource allocation algorithm; it is easy to create a thread but difficult to control the thread. If multithreading is used, a fatal error may occur, we don't need the time it saves. The more threads, the more resources it will definitely occupy. This will be subject to hardware restrictions such as memory and CPU, we still need to consider these hardware problems when using multithreading. In general, multithreading means changing the space for time. How many threads are suitable? Everything has a degree, moderate. Display the number of threads. By default, the task manager hides the number of threads of a process. You can use the following methods to visualize the number of threads:

The 14 Thread of the process Everything is the Thread that is being run, regardless of the number of Thread objects in the program. The next blog will combine these basic content to write a multi-thread race instance.

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.