C # Multithreading Learning (a) The related concepts of multithreading

Source: Internet
Author: User
Tags readline thread class

What is a process? When a program starts running, it is a process that includes the memory and system resources that are used by the programs and programs that are running. And a process is made up of multiple threads.What is a thread? A thread is a flow of execution in a program, each with its own proprietary register (stack pointers, program counters, and so on), but the code area is shared, meaning that different threads can execute the same function.What is multithreading? Multithreading refers to a program that contains multiple execution streams, in which multiple different threads can be run concurrently to perform different tasks, that is, allowing a single program to create multiple threads of parallel execution to accomplish their tasks.Benefits of Multi-threading:can improve CPU utilization. In multithreaded programs, when a thread has to wait, the CPU can run other threads instead of waiting, which greatly increases the efficiency of the program.The disadvantage of multi-threading:Threads are also programs, so threads need to consume memory, more threads consume more memory, multithreading needs to be coordinated and managed, CPU time is required to track threads, access to shared resources between threads affects each other, the problem of competing shared resources must be resolved, too many threads can cause control to be too complex, This can result in a lot of bugs, and then the multithreading mechanisms in C # programming are explored. In order to avoid the tedious steps of creating a GUI, and to more clearly approximate the nature of the thread, all the next programs are console programs, and the final console.readline () of the program is to stop the program halfway in order to see the output during execution. There is at least one main thread for any program to execute.a visual impression of the threading example:using System;using System.Threading;namespace ThreadTest{class Runit {[STAThread]static void Main (string[] args) {thread.currentthread.name= "System thread";//named "System thread" for the current threadConsole.WriteLine (thread.currentthread.name+ "' Status:" +thread.currentthread.threadstate);console.readline (); } }}The output is as follows:System thread ' s status:running here we get the currently executing thread through the static property of the thread class CurrentThread, assigning the "System thread" to its Name property, Finally, it also outputs its current state (ThreadState). A static property is a property that is common to all objects of this class, no matter how many instances of the class you create, but the static properties of the class are only one in memory. It is easy to understand why CurrentThread is static--although multiple threads exist at the same time, at some point the CPU can only execute one of them. At the head of the program, we used the following namespaces: Using System;using system.threading; in the. NET Framework class library, All classes related to multithreaded application are placed in the System.Threading namespace. If you want to use multi-threading in your application, you must include this class. We create and control threads through the thread class provided, the ThreadPool class is used to manage the thread pool, and so on. (It also provides a mechanism for solving practical problems such as thread execution scheduling, deadlock, and inter-thread communication.) The thread class has several critical methods, which are described as follows: Start (): Start thread, Sleep (int): Static method, pauses the number of milliseconds specified by the current thread; abort (): This method is usually used to terminate a thread; Suspend () : The method does not terminate the unfinished thread, it simply suspends the thread and can be resumed later; resume (): Resumes execution of the thread that was suspended by the suspend () method.

C # Multithreading Learning (a) The related concepts of multithreading

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.