The basic review of C# learning articles (16)----- Process and multi-threaded

process

Process (Process) is a basic concept of Windows system. An application running in the operating system is considered a process, and a process can include one or more threads. Processes are relatively independent, one process cannot access the data of another process (unless distributed computing is used), and one process fails to affect another process.

thread

thread is the basic execution unit in the process, is a lightweight process, and is the basic unit for the operating system to allocate CPU time. A process can contain multiple threads, and the first thread executed at the process entry is treated as the main thread of the process. For example, in the C# program, the Main() method is used as the program entry. When this method is called, the system creates a main thread.

多thread

Multi-threading can complete multiple tasks at the same time, optimize the performance of the program according to the priority of the task, such as the task that takes a lot of time to give time to other tasks.

From a macro perspective, the CPU can implement multi-threading, but in fact a CPU can only run one thread at a time. Since the CPU runs very fast, it seems that different operations are performed at the same time. Of course, with the development of hardware, today's computers are multi-core CPUs, so it is possible to run multiple threads at the same time.

The life cycle of a thread begins when an object of the System.Threading.Thread class is created, ending when the thread is terminated or completed.

Before you create a thread, you first need to reference the System.Threading namespace, and then create it by extending the Thread class. The extended Thread class calls the Start() method to start the thread execution. Create a thread that calls the no-argument method as follows:

static void Name()
        {
            Console.WriteLine("This is a thread");
        }
        Static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(Name));
            thread.Start();
            Console.ReadLine();
        }

Run results: This is thread

If you need to create a thread with parameter method need to be modified to ParameterizedThreadStart, ParameterizedThreadStart is a parameter with a return value of void, And the parameter must be of the type Object, defined as follows:

static void Name(Object obj)
        {
            Console.WriteLine(obj);
            Console.WriteLine("This is a thread");
        }
        Static void Main(string[] args)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(Name));
            thread.Start("This is a parameter");
            Console.ReadLine();
        }

Run results: This is the parameter

This is the thread

Multi-threaded test

        static void OutPutA()
        {
            for (int i = 0; i < 1000; i++)
            {
                Console.Write("A");
            }
        }
        static void OutPutB()
        {
            for(int i=0;i<1000;i++)
            { 
                Console.Write("B");
            }
        }
        static void Main(string[] args)
        {
            Thread thread = new Thread(OutPutA);
            thread.Start();
            Thread thread1 = new Thread(OutPutB);
            thread1.Start();
            Console.ReadLine();
        }

Run results: