Multithreading Knowledge 1 Basic Concepts

When it comes to the concept of threads, we have to mention the process. So let's first look at what is a process? (PS: These concepts are also borrowed from the Internet)

1. The concept of process

      A process is the foundation of the operating system structure; is an executing program; an instance of a running program in a computer; an entity that can be assigned to and executed by a processor; displayed by a single sequential execution, a current state and a set of The unit of activity described by the associated system resource. Simply put, when we start an application, there is a process. You can view the current system processes through the task manager. The system allocates a separate memory space for the process

2. Thread ( Tread )

        A thread, sometimes referred to as a Lightweight Process ( LWP) , is the smallest unit of program execution flow. A standard thread consists of thread ID , current instruction pointer (PC) , register set and stack. In addition, a thread is an entity in the process, and it is the basic unit independently scheduled and dispatched by the system. The thread itself does not own system resources, but only has some resources that are essential for running, but it can share with other threads belonging to the same process. All resources owned by the shared process. A thread can create and cancel another thread, and multiple threads in the same process can execute concurrently. Due to the mutual constraints between the threads, the threads show discontinuity in their operation.

        Every program has at least one thread , which is the program itself. A thread is a single sequential flow of control in a program.   Running multiple threads simultaneously in a single program to complete different work is called multithreading.

3. The state of the thread

  1) New state ( new )

  2) Ready state ( Runnable )

  3) Running state ( Running )

  4) Blocked state ( Blocked )

  5) Dead state ( Dead )
4. The life cycle of threads 5. The benefits that threads bring to us


    Proper use of threads can reduce development and maintenance costs and even improve the performance of complex applications. For example, in GUI applications, events can be handled better through the asynchronous nature of threads; in application server programs, multiple threads can be established to process client requests. Threads can even simplify the implementation of virtual machines, such as the Java Virtual Machine ( JVM ) garbage collector ( garbage collector ) usually runs in one or more threads. Therefore, using threads will improve our application in five ways:

    1). Make full use of CPU resources

    Most computers in the world now have only one CPU. Therefore, it is particularly important to make full use of CPU resources. When executing a single-threaded program, the CPU may be idle when the program blocks . This will cause a lot of waste of computing resources. Using multithreading in a program can run other threads when one thread is dormant or blocked, and the CPU happens to be in an idle state. This makes it difficult for the CPU to have idle time. Therefore, CPU resources are fully utilized.

    2).  Simplify the programming model

    If the program only needs to do one task, then just write a single-threaded program and write the code according to the steps to perform the task. But to complete multiple tasks, if you are still using a single thread, you have to determine in the program whether and when each task should be executed. For example, it displays three hands of hour, minute and second of a clock. If you use a single thread, you have to judge the rotation time and angle of the three pointers one by one in the loop. If three threads are used to handle the display of the three pointers separately, then it is a separate task for each thread. This helps developers understand and maintain the program.

    3). Simplify the processing of asynchronous events

    The easiest way to handle when a server application is receiving different client connections is to create a thread for each client connection. Then the listening thread is still responsible for listening for requests from clients. If this application is processed by a single thread, when the listening thread receives a client request, it starts to read the data sent by the client. After reading the data, the read method is in a blocking state, that is, the thread will It is no longer possible to listen for client requests. To handle multiple client requests in a single thread, you must use non-blocking Socket connections and asynchronous I/O. But using asynchronous I/O is more difficult to control and more error-prone than using synchronous I/O . Therefore, asynchronous events like multi-requests can be handled more easily using multi-threading and synchronous I/O .

    4). Make the GUI more efficient

    When using a single thread to process GUI events, a loop must be used to scan for GUI events that may occur at any time. In addition to scanning GUI events, other program codes must be executed inside the loop. If the code is too long, the GUI events are " frozen " until the code is executed.

    A separate event dispatch thread (EDT) is used in modern GUI frameworks ( such as SWING , AWT and SWT ) to scan for GUI events. When we press a button, the button's click event function is called in this event dispatch thread. Since the task of EDT is only to scan for GUI events, the response to events in this way is very fast.

    5).  Cost saving

    There are generally three ways to improve the execution efficiency of a program:

    ( 1 ) Increase the number of CPUs of the computer .

    ( 2 ) Start multiple processes for one program

    ( 3 ) Use multiple processes in the program.

    The first method is the easiest to do, but at the same time the most expensive. This method does not need to modify the program, in theory, any program can use this method to improve the execution efficiency. Although the second method does not need to purchase new hardware, it is not easy to share data. If the task to be completed by the program needs to share data, this method is not very convenient, and starting multiple threads will consume a lot of money. system resources. The third method just makes up for the shortcomings of the first method, while inheriting their advantages. That is to say, there is no need to purchase CPU , nor will it take up a lot of system resources because too many threads are started (by default, the memory space occupied by a thread is much smaller than the memory space occupied by a process). Multithreading), and multithreading can simulate the operation of multiple CPUs , so using multithreading is the cheapest way to improve program execution efficiency.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327068754&siteId=291194637