C # multithreading (1) thread. Join ()

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.

 

 

Example:

Using system;

Using system. Threading;

Namespace Test
{
Class testthread
{
Private Static void firstthreadfun ()
{

For (INT I = 0; I <10; I ++)
{

Console. writeline (thread. currentthread. Name + "I
= "+ I );

 
}

Console. writeline (thread. currentthread. Name + "executed ");
}

Static void main (string [] ARGs)
{

// When the program calls the main () function, a process is started and a thread is also started [this thread is the main thread].

Thread. currentthread. Name = "mainthread ";


// Create the first thread

Thread firstthread = new thread (New
Threadstart (testthread. firstthreadfun ));

// Obtain the name firstthread

Firstthread. Name = "firstthread ";


For (INT z = 0; Z <20; Z ++)
{

If (Z = 10)

{

Firstthread. Start ();

Firstthread. Join ();

}

Else

{

Console. writeline (thread. currentthread. Name + "Z
= "+ Z );

}
}

Console. Read ();
}
}

}

Running result:

 

Summary:

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

2. firstthread. start () starts a thread and uses firstthread. join () This method adds a thread [that is, the main thread is suspended], then the operating system will immediately execute this new thread.

3. Join means to join, that is, the newly created thread is added to the process and executed immediately.

4. If only firstthread. Start () is used, what is the result of commenting out the firstthread. Join () method?

The following figure shows the running result:

 

The running results show that:

1. If it is only firstthread. Start (), comment out the firstthread. Join () method, the main thread will not pause [that is, the firstthread thread will not be executed immediately]

2. To run a thread immediately after it is started, you must call the thread. Join () method.

3. here, thread. the function of join () is obvious: When thread is called. after the join () method, the current thread is executed immediately, and all other threads are paused.

After this thread is executed, other threads will continue to execute.

 

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 (): resume 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.