C # threads from unfamiliar to familiar (1)

Source: Internet
Author: User

 

When talking about threads, we should be familiar with them. Similar to him, there is also a Process concept ). First, familiarize yourself with their concepts and relationships.

What is a process?

When a program starts to run, it is a process, including the memory and system resources used by the 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.), but the code zone is shared, that is, different threads can execute the same function.

Their relationship?

The root process of a thread has both connections and differences. A process is the execution of code in a dataset and the basic unit of system resource allocation. Any thread must run in a process. A process must have at least one thread, otherwise it cannot be executed. In fact, each process has a main thread, and other threads owned by this thread are created by the main thread. If we end the main thread of a process, all other threads of the process will be ended, and the whole process will be ended!

We can understand that running the code is a thread! The process only configures the system resources for Running code. Several threads run together to complete a function and organize a code execution process! So there is a process!

C # The Thread class is used to represent the Thread! Let's take a look at the definition of the Thread class.

[ComVisibleAttribute (true)]

[ClassInterfaceAttribute (ClassInterfaceType. None)]

Public sealed class Thread: CriticalFinalizerObject,

_ Thread

{

...

 

 

}

[ComVisibleAttribute (true)] This indicates the access to com by individual managed types, members, or all types in the control set.

[ClassInterfaceAttribute (ClassInterfaceType. None)] specifies the type of the class interface to be generated (if an interface is generated) for the class exposed to COM ).

In a class derived from the CriticalFinalizerObject class, the Common Language Runtime Library (CLR) ensures that all key termination code has the opportunity to be executed, even when the CLR forcibly unmounts the application domain or terminates the thread (as long as the terminator complies with the CER rules );

_ Thread this interface is used to access managed classes from unmanaged code and should not be called from managed code.

Note that the Thread class declares sealed in. net and cannot be inherited! This is quite different from Java!

He has four constructor functions. Let's take a look at two of them!

[SecuritySafeCritical]

Public Thread (ParameterizedThreadStart start );

[SecuritySafeCritical]

Public Thread (ThreadStart start );

ParameterizedThreadStart and ThreadStart are both delegates. The first delegate indicates a delegate with parameters. The type is obj, and the latter is a delegate without parameters. Both of them are marked with [ComVisible (false)], indicating that they cannot access COM +!

Some static attributes of the Thread can be used to obtain the current context, running processes, and so on! His static method can be used to obtain his running application domain!

The following is an example,

1 class Program

2 {

3 static void Main (string [] args)

4 {

5 ThreadStart tspx = new ThreadStart (PrintX );

6 ThreadStart tspy = new ThreadStart (PrintY );

7 ParameterizedThreadStart ptsp = new ParameterizedThreadStart (PrintParam );

8 Thread t1 = new Thread (tspx );

9 Thread t2 = new Thread (tspy );

10 Thread t3 = new Thread (ptsp );

11

12 t1.Start ();

13 t2.Start ();

14 t3.Start ("123 ");

15 Thread. Sleep (300 );

16

17}

18 public static void PrintX ()

19 {

20 int I = 1;

21 while (I ++ <50)

22 {

23 Console. Write ("X ");

24}

25}

26 public static void PrintY ()

27 {

28 int I = 1;

29 while (I ++ <50)

30 {

31 Console. Write ("Y ");

32}

33}

34 public static void PrintParam (object s)

35 {

36 int I = 1;

37 while (I ++ <50)

38 {

39 Console. Write (s. ToString ());

40}

41}

42

43}

The example is relatively simple, and the results after running are often random! You only need to increase the number of running times! Note that ParameterizedThreadStart is of the Object type! From this example, we can see that these threads run in disorder! As long as the main thread ends, the entire application will end, and other threads will not end!

See the following example,

1 using System;

2 using System. Collections. Generic;

3 using System. Linq;

4 using System. Text;

5 using System. Threading;

6

7 namespace leleapplication3

8 {

9 class Program

10 {

11 static void Main (string [] args)

12 {

13 Thread t = new Thread (PrintX );

14 t. Start ();

15 Thread. CurrentThread. Name = "Main ";

16 Console. WriteLine ("main Thread application domain {0}, ID: {1}", Thread. GetDomain (). ToString (), Thread. GetDomainID ());

17 Console. WriteLine ("main Thread Name: {0}, HashCode: {1}", Thread. CurrentThread. Name, Thread. CurrentThread. GetHashCode ());

18

19}

20 static void PrintX ()

21 {

22 Thread. CurrentThread. Name = "t1 ";

23 Console. WriteLine ("t1 Thread application domain {0}, ID: {1}", Thread. GetDomain (). ToString (), Thread. GetDomainID ());

24 Console. WriteLine ("t1 Thread Name: {0}, HashCode: {1}", Thread. CurrentThread. Name, Thread. CurrentThread. GetHashCode ());

25}

26}

27}

 

 

Shows how to run this example,

 

 

Here, some static attributes of Thread can be used to get what we want! This is also commonly used!

Now, I have written so much today!


From ENUO

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.