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, and more threads consume more memory;
Multithreading requires coordination and management, so CPU time is required to track threads;
Access to shared resources between threads affects each other, and the issue of competing shared resources must be resolved;
Too many threads can lead to too much control and can eventually cause many bugs;

Next, the multithreading mechanism in C # programming is 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:

//SystemThread.csusingSystem;usingSystem.Threading;namespacethreadtest{classRunit {[STAThread]Static voidMain (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, assign the "System Thread" to its Name property, and finally output its current state (ThreadState) CurrentThread.
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 use 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 below:

Start (): Start thread;
Sleep (int): Static method that pauses the specified number of milliseconds for 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 (): Restores the execution of the thread that was suspended by the suspend () method;

C # multithreaded Learning series:

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

Http://www.cnblogs.com/zpx1986/p/5571506.html

C # multithreaded Learning (ii) How to manipulate a thread
Http://www.cnblogs.com/zpx1986/p/5584256.html

C # multithreaded Learning (iii) producers and consumers
Http://www.cnblogs.com/zpx1986/p/5584305.html

C # Multi-threaded Learning (four) multi-threaded automatic management (thread pool)
Http://www.cnblogs.com/zpx1986/p/5584351.html

C # multithreaded Learning (v) multi-threaded automatic management (timers)
Http://www.cnblogs.com/zpx1986/p/5584370.html

C # multithreaded Learning (vi) Mutex objects
Http://www.cnblogs.com/zpx1986/p/5584387.html



Null

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

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.