Introduction to multithreading (1) Basic Concepts

Source: Internet
Author: User
ArticleDirectory
    • Preface
    • Concepts
    • Benefits of Multithreading
    • Advantages of Multithreading
    • Advantages and disadvantages
    • Let's look at the first multithreading example.
    • Conclusion
Preface

I often see that some candidates need to be familiar with multithreading in recruitment. Although I often hear the concept of multithreading for cainiao, it is rarely used in actual operations, it is also easy to understand, so take the day off to learn and make up for this knowledge.

PS: This article is only a record of multithreading, not an in-depth discussion of multithreading. If you and I are not familiar with multithreading, you can study with me. Thank you for your criticism and correction!

Concepts

I have never liked or remember concepts. I prefer to combine concepts and actual operations to impress me. But before learning, let's take a look at several concepts so that they can leave some shadows in our minds.

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.

Thread: a thread, sometimes called a lightweight process, is the smallest unit of the program execution flow.

Multithreading: A thread is a single sequential control flow in a program. It is called multithreading.

Benefits of Multithreading

Multithreading is used to synchronize multiple tasks, not to improve the running efficiency, but to improve the resource usage efficiency to improve the system efficiency. The thread is implemented when multiple tasks need to be completed at the same time. By using a thread, you can place tasks that take a long time to a separate thread in the background, so that the program runs faster. At the same time, multithreading can be used to develop a more user-friendly interface, such as displaying the processing progress through multithreading when we submit a data item.

The simplest analogy is that multithreading is like a factory worker, while a process is a workshop in a factory. When a worker leaves the workshop, production is impossible. Similarly, there cannot be only one employee in the workshop. Multithreading is designed to improve efficiency.

Advantages of Multithreading

More threads mean more memory consumption. exiting a thread may cause trouble to the program. improper processing may lead to more deadlocks; too many threads will affect the performance (because the operating system needs to switch between threads)

Advantages and disadvantages

Since multithreading has both advantages and disadvantages, we should develop strengths and circumvent weaknesses, give full play to its advantages, and avoid its disadvantages. In the future study process, we will gradually sum up and find out what is wrong!

Let's look at the first multithreading example.

We write multi-threadedCodeMust be introducedUsing system. Threading namespace!

Let's take a look at a simple example of multithreading. Let's take a look at what it is. Let's discuss the specifics of Dongdong's younger brother's blog later.

 1   Using  System;  2  Using  System. Threading;  3   4   Namespace  Leleapplication14  5   {  6       Class  Program  7   {  8   [Stathread]  9           Static  Void Main ( String  [] ARGs)  10   {  11 Console. writeline ( "  Start testing my thread  "  );  12 Thread test = New Thread ( New  Threadstart (mythread ));  13  Test. Start ();  14 Thread. currentthread. Name = "  Damaged snow in broken bridges  "  ;  15 Console. writeline (thread. currentthread. Name + Thread. currentthread. managedthreadid );  16 Console. writeline ( "  Testing my thread ends  "  );  17  Console. Readline ();  18   }  19   20           Public   Static   Void  Mythread ()  21   {  22               For ( Int I = 0 ; I < 100 ; I ++)  23   {  24   Console. writeline (I );  25   }  26   }  27   }  28 }

The output is as follows:

Next, add a line of code to the console. writeline ("test my thread:

1Thread. Sleep (1);2Console. writeline ("Testing my thread ends");

Run the following command:

I read the written code and running results. Is it a bit strange? Why is my last line of code console. writeline ("test my thread ended") executed in advance? To be honest, I cannot explain it clearly now. I believe I can come back to explain it later. <Here is a big question mark?> In addition, a thread attribute managedthreadid is used in the Code. In the subsequent study, I will introduce more related attributes and methods. I also hope that you will see something wrong, even if it is not correct, the hard-earned cainiao ,,,

Conclusion

Next, I will write a series of essays on multithreading. If you are interested, you can study with me. Thank you !!!

I am about to go to Shanghai soon, with all kinds of perseverance and nostalgia. Wuhan is gone. I wish my and my loved ones happy every day, always healthy and happy.

------If you think this article is helpful to you, don't forget to click the recommendation in the lower right corner. Thank you!------

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.