C # Multithreading Learning (i) Multithreading related concepts

Source: Internet
Author: User
Tags readline thread thread class

What is a process?

When a program starts running, it is a process that includes the memory and system resources used by the running programs and programs.

And a process is made up of multiple threads.

What is a thread?

A thread is a stream of execution in a program, and each thread has its own proprietary registers (stack pointers, program counters, and so on), but the code area is shared, that is, different threads can perform the same function.

What is multithreading?

Multithreading is a program that contains multiple execution streams in which multiple different threads can be run at the same time to perform different tasks, that is, to allow a single program to create multiple threads executing in parallel to accomplish their tasks.

The benefits of Multithreading:

can increase CPU utilization. In multithreaded programs, when a thread must wait, the CPU can run other threads instead of waiting, which greatly increases the efficiency of the program.

The disadvantage aspect of multithreading:

Threads are also programs, so threads need to occupy memory, and the more threads occupy more memory;

Multithreading needs to be coordinated and managed, so CPU time tracking thread is required;

Access to shared resources between threads can affect each other, and the problem of competing shared resources must be resolved;

Too many threads can lead to too much control and can eventually cause many bugs;

Next, we will discuss the multithreading mechanism in C # programming. In order to eliminate the cumbersome steps of creating the 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 in order to see the output in the execution process.

There is at least one main thread for any program to execute.

An intuitive example of threading:

//SystemThread.cs
using System;
using System.Threading;
namespace ThreadTest
{
   class RunIt
   {
     [STAThread]
     static void Main(string[] args)
     {
       Thread.CurrentThread.Name="System 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 currently executing thread through the static property CurrentThread of the thread class, assign "System Thread" to its Name property, and finally output its current state (ThreadState).

A static property is a property that is public to all objects of this class, regardless of how many instances of this class you create, but the static properties of the class are only one in memory. It's easy to understand why CurrentThread is static--although there are multiple threads at the same time, at a certain point, the CPU can only execute one of them.

In the program's head, 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 multiple threads in your application, you must include this class.

We create and control threads through the thread class provided, and the ThreadPool class is used to manage thread pools, and so on.

(In addition, it provides mechanisms to solve practical problems such as thread execution scheduling, deadlock, and communication between threads.) )

The thread class has several critical methods, described below:

Start (): Start the thread;

Sleep (int): A static method that pauses the number of milliseconds specified by the current thread;

Abort (): This method is usually used to terminate a thread;

Suspend (): This method does not terminate the unfinished thread, it simply suspends the thread and can be recovered later;

Resume (): Restores the execution of threads suspended by the suspend () method;

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.