9. C # knowledge point: First knowledge of Thread and first knowledge of Thread (1 ),

Source: Internet
Author: User

9. C # knowledge point: First knowledge of Thread and first knowledge of Thread (1 ),
Knowledge Point directory ==========> Portal

A brief summary of threads and processes.

1. A process is a "active" program. A program is an inanimate entity. It can become an active entity only when the processor gives it life. We call it a process. Processes are independent of each other. A program is a set of commands. It is a static description text of a process running. A process is an execution activity of a program and is a dynamic concept.

2. threads are sometimes referred to as lightweight processes and are the smallest unit of program execution. As mentioned above, a process can correspond to multiple threads, and a thread only belongs to one process. The execution of a process is measured in threads.

If the above statement is not clear, it is also normal. The thread and process are a little abstract. The following example should be clear. This example is obtained by Daniel's article.

1.

The core of a computer is the CPU, which undertakes all the computing tasks. It is like a factory that is always running.

2.

If the power supply of the plant is limited, it can only be used in one workshop at a time. That is to say, when a workshop starts, all other workshops must be shut down. The meaning is that a single CPU can only run one task at a time.

3.

A process is like a factory workshop. It represents a single task that the CPU can process. At any time, the CPU always runs a process, and other processes are not running.

4.

There can be many workers in a workshop. They work together to complete a task.

5.

A thread is like a worker in a workshop. A process can contain multiple threads.

6.

The workshop space is shared by workers. For example, many rooms are accessible to every worker. This indicates that the memory space of a process is shared and can be used by every thread.

7.

However, the size of each room is different. Some rooms can accommodate only one person at most, such as a toilet. When there is someone in it, other people cannot go in. This means that when a thread uses some shared memory, other threads must wait until it ends to use this memory.

8.

A simple way to prevent others from entering is to lock the door. The first person locks the door, and then the person who sees the lock, queues at the door, wait until the lock is opened and then go in. This is called Mutex, which prevents multiple threads from simultaneously reading and writing a memory area.

9.

There are also some rooms that can accommodate n people at the same time, such as the kitchen. That is to say, if the number of people is greater than n, more people can only wait outside. This is like some memory areas, which can only be used by a fixed number of threads.

10.

The solution at this time is to mount n keys at the door. The person who goes in gets a key and then hangs the key back to the original place. The next person found that the key had been overhead and he knew that he had to wait in queue at the door. This method is called Semaphore to ensure that multiple threads do not conflict with each other.

Mutex is a special case of semaphore (n = 1 ). That is to say, the latter can be used to replace the former. However, because mutex is relatively simple and efficient, this design is still used when resources must be exclusive.

11.

The operating system design can be attributed to three points:

(1) Allow multiple tasks to run simultaneously in the form of multi-process;

(2) In the form of multithreading, a single task can be divided into different parts for running;

(3) provide a coordination mechanism to prevent conflicts between processes and threads, and allow resources to be shared between processes and threads.

------------- From Daniel's blog

The above is a good image. I believe I can understand all programs I have never learned.

The following describes our Thread class. Use Thread in. Net to create and control threads and obtain their statuses.

        [SecuritySafeCritical]        public Thread(ThreadStart start);        [SecuritySafeCritical]        public Thread(ParameterizedThreadStart start);

First, let's look at these two major constructors.

public delegate void ThreadStart();
ThreadStart is a delegate without parameters.
public delegate void ParameterizedThreadStart(object obj);
ParameterizedThreadStart is also a delegate, but there is a delegate for the Obj parameter.
It can be seen that the first constructor needs to input a method without parameters when creating a thread, and the second is a method with parameters.
Now let's take a look at the actual use of the two.
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading; using System. threading. tasks; namespace ThreadTest {class Program {static void Main (string [] args) {Thread t1 = new Thread (Test); t1.Start (); Console. writeLine ("this is the main thread"); Console. readKey ();} static void Test () {Thread. sleep (1, 5000); Console. writeLine ("This Is A subthread ");}}}

It is obvious that the main thread is printed first, and then the subthread is printed. If the sub-thread is not enabled. The main thread will sleep for five seconds and wait until the Test method is executed.

Next I will run it using a constructor with parameters.

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading; using System. threading. tasks; namespace ThreadTest {class Program {static void Main (string [] args) {Thread t1 = new Thread (Test); t1.Start ("This Is A subthread"); Console. writeLine ("this is the main thread"); Console. readKey ();} static void Test (object obj) {Thread. sleep (1, 5000); Console. writeLine (obj. toString ());}}}

The same is true.

Summarize the main usage of threads

Thread. Start () Start the Thread.

Thread. Sleep () Thread Sleep, that is, waiting for execution. The parameter is the time.

Thread. Aboort () Terminate the Thread.

Thread. Join () blocking Thread waiting for Thread execution.

It mainly describes the difference between Sleep and Join.

Let's give a brief summary. Sleep is the Sleep of the current thread, which hinders the execution of the current thread and does not affect other threads. The Join operation is performed only after the execution of the current thread is completed.

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading; using System. threading. tasks; namespace ThreadTest {class Program {static void Main (string [] args) {// create Thread t1 = new Thread (Test ); t1.Start ("this is a sub-thread"); t1.Join (); // blocking the Console. writeLine ("this is the main thread"); Console. readKey ();} static void Test (object obj) {Console. writeLine (obj. toString ());}}}

Join is called here, indicating that the code execution to Join will wait until the execution of thread t1 ends, that is, the execution of the Test method ends. Can be executed.

This is the end. Here, the blog mainly describes what is thread, process, and so on, and briefly describes Thead class. And main operations.

 

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.