C # thread.jion ()

Source: Internet
Author: User

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, and more threads consume more memory;
Multithreading requires coordination and management, so 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;

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.

Example:

Using System;

Using System.Threading;

Namespace Test
{
Class Testthread
{
private static void Firstthreadfun ()
{
for (int i = 0; i <; i++)
{
Console.WriteLine (Thread.CurrentThread.Name + "i=" + i);

}
Console.WriteLine (Thread.CurrentThread.Name + "execution complete");
}

static void Main (string[] args)
{
When the program invokes the main () function, it starts a process and also initiates a thread [the thread is the main path].
Thread.CurrentThread.Name = "Mainthread";

Create the first thread
Thread firstthread = new Thread (Newthreadstart (Testthread.firstthreadfun));
Name is Firstthread
Firstthread.name = "Firstthread";

for (int z = 0; z <; z++)
{
if (z = = 10)
{
Firstthread.start ();
Firstthread.join ();
}
Else
{
Console.WriteLine (Thread.CurrentThread.Name + "z=" + z);
}
}
Console.read ();
}
}

}

Operation Result:

Summarize:

1. There is at least one main thread at the time of execution of any program.

2.firstthread.start () After starting a thread, using Firstthread.join () This method to join a thread [i.e., pause the main thread of the run], then the operating system will immediately execute this new join thread

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

4. What happens if the Firstthread.join () method is commented out if it is just Firstthread.start ()?

The following figure is the result of the operation:

From the results of the operation can be seen:

1. If only Firstthread.start () is commented out, the main thread will not pause [that is, the Firstthread thread will not execute immediately] after commenting out the Firstthread.join () method.

2. So to think that a thread executes immediately after it is started, the Thread.Join () method must be called.

3. Here, the Thread.Join () method is obvious: when the Thread.Join () method is called, the current thread is executed immediately, and all other threads are paused.

When the thread finishes executing, the other threads will continue to execute.

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 (): Restores the execution of the thread that was suspended by the suspend () method;

Category: C # Tags: Thread

C # thread.jion ()

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.