C # multithreading (I) concepts related to multithreading

Source: Internet
Author: User

 

What is a process?
WhenProgramIt is a process that includes the memory and system resources used by running programs and programs.
A process is composed of multiple threads.

What is a thread?
A thread is an execution stream in a program. Each thread has its own proprietary register (Stack pointer, program counter, etc.),CodeZones are shared, that is, different threads can execute the same function.

What is multithreading?
Multithreading means that a program contains multiple execution streams, that is, a program can run multiple different threads to execute different tasks at the same time, that is to say, a single program is allowed to create multiple parallel threads to complete their respective tasks.

Advantages of multithreading:
CPU utilization can be improved. In a multi-threaded program, when a thread has to wait, the CPU can run other threads instead of waiting, which greatly improves the program efficiency.

Disadvantages of multithreading:
The thread is also a program, so the thread needs to occupy the memory. The more threads occupy the memory, the more;
Multithreading requires coordination and management, so it requires CPU time to track threads;
Inter-thread access to shared resources will affect each other, and the problem of competing to share resources must be solved;
Too many threads will lead to too complex control, which may lead to many bugs;

Next, we will discuss the multithreading mechanism in C # programming. In order to save the tedious steps for creating a GUI and more clearly approach the essence of the thread, all subsequent programs are console programs and the final console of the program. readline () is used to stop the program midway through, so that you can see the output in the execution process clearly.

Any program must have at least one main thread during execution.

A thread example with an intuitive impression:

// Systemthread. CS
Using System;
Using System. Threading;

Namespace Threadtest
{
Class Runit
{
[Stathread]
Static   Void Main ( String [] ARGs)
{
Thread. currentthread. Name = " System thread " ; // Name the current thread "system thread"
Console. writeline (thread. currentthread. Name + " 'Status: " + Thread. currentthread. threadstate );
Console. Readline ();
}
}
}

The output is as follows:
System thread's status: Running

Here, we get the thread currently being executed through the static attribute currentthread of the thread class, assign the value "system thread" to its name attribute, and finally output its current state (threadstate ).

The so-calledStatic attributesIt is the public attribute of all objects in this class. No matter how many instances of this class are created, the static attribute of the class has only one in the memory. It is easy to understand why currentthread is static-although multiple threads exist at the same time, the CPU can only execute one of them at a certain time point.

In the program header, we use the following namespace:
Using system;
Using system. Threading;
 
In. NET Framework class library, all classes related to multithreading applications are stored in the system. Threading namespace. If you want to use multithreading in your application, you must include this class.

We use the Thread class provided to create and control threads. The threadpool class is used to manage thread pools.
(In addition, it provides a mechanism to solve actual problems such as thread execution arrangements, deadlocks, and inter-thread communication .)

The thread class has several important methods, which are described as follows:
Start (): Start the thread;
Sleep (INT): a static method that pauses the specified number of milliseconds of the current thread;
Abort (): This method is usually used to terminate a thread;
Suspend (): This method does not terminate the unfinished thread. It only suspends the thread and can be recovered later;
Resume (): resumes the execution of threads suspended by the suspend () method;

 

C # multi-thread Learning Series:

C # multi-thread Learning (2) how to manipulate a thread
Http://www.cnblogs.com/xugang/archive/2008/04/06/1138841.html

 

C # multithreading (3) producer and consumer
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118594.html

 

C # multithreading (4) Automatic Management of multithreading (thread pool)
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118584.html

 

C # multi-thread Learning (5) automatic management of multiple threads (timer)
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118537.html

 

C # multi-thread Learning (6) mutually exclusive objects
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118530.html

 

RelatedArticle:
DOTNET thread Problems

Use threads in Visual C #

C # Summary of thread resource Synchronization Methods

C # thread Series Lectures (1): begininvoke and endinvoke Methods

 

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.