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

Source: Internet
Author: User

Author: Steel Steel Source: Blog Park Published: 2008-09-20 18:42 read: 91,973 recommendations: 39 original link [Favorites]

Summary: Related concepts about threadingSeries Article Navigation:

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

C # multithreaded Learning (ii) How to manipulate a thread

C # multithreaded Learning (iii) producers and consumers

C # Multi-threaded Learning (four) multi-threaded automatic management (thread pool)

C # multithreaded Learning (v) multi-threaded automatic management (timers)

C # multithreaded Learning (vi) Mutex objects


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 Multithreading:

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 Multithreading:

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, May eventually cause a lot of 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:

Using System;


Namespace ThreadTest
{
Class Runit
{
[STAThread]
static void Main (string[] args)
{
Thread.currentthread.name= "System thread";//named "System thread" for the current 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 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 (): Resumes execution of a thread that was suspended by the suspend () method.

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.