Multi-shadow--c# use of multiple threads (i)

Source: Internet
Author: User

First define several concepts:

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.

A program usually has only one process (not including EXE calls EXE).

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?

Popular even: Multithreading gives people the feeling is to do a lot of things at the same time, in fact, from a microscopic point of view, is not at the same time, but the effective use of the CPU idle time, order to do something. For example, the general procedure is to throw a small ball, and when it falls down, it will throw another ball. Multi-Threading is the time when it should wait for the landing to continue to throw, effectively using the waiting time.

Professional explanation: Multithreading refers to the program contains multiple execution flows, that is, in one program can run several different threads to perform different tasks, that is, allow a single program to create multiple parallel execution of threads to complete their own tasks.

The advantages and disadvantages of multithreading:

Benefit : You can increase the 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.

Cons : 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, 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;

Small example:

" Main Thread "  "  , Status:" + Thread.CurrentThread.ThreadState); Console.readkey ();

The value of the ThreadState property is as follows:

Aborted: Thread has stopped;

AbortRequested: The thread's Thread.Abort () method has been called, but the thread has not stopped;

Background: Threads are executed in the background, related to attribute thread.isbackground;

Running: thread is running properly;

Stopped: Thread has been stopped;

Stoprequested: Thread is being requested to stop;

Suspended: The thread has been suspended (in this state, it can be rerun by calling the Resume () method);

Suspendrequested: Thread is being requested to be suspended but not responding;

Unstarted: Thread.Start () is not called to start the thread run;

WaitSleepJoin: The thread is blocked by calling wait (), Sleep (), or join ().

Some of the methods commonly used in thread: (thread represents a static method, T represents a need to instantiate)

thread.sleep (int num) The current thread stops num milliseconds

T.start (); Start the thread

T.abort (); Terminate the thread

T.join (); Usually followed by T. Start (), which means wait for the thread to finish executing the other. It is usually used in cases where there is a sequential dependency relationship.

Multi-shadow--c# use of multiple threads (i)

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.