Java multithreading series_ Thread Introduction (1)

1、 Thread overview

Thread is the basic execution unit of a program. When the operating system (excluding the single threaded operating system, such as Microsoft's early DOS) executes a program, it will establish a process in the system, and in this process, at least one thread must be established (this thread is called the main thread) as the entry point for the program to run. Therefore, any program running in the operating system has at least one main thread.

Process and thread are two essential running models in modern operating system. There can be multiple processes in the operating system, These processes include system processes (processes established within the operating system) and user processes (a process established by a user program); a process can have one or more threads. Memory is not shared between processes, that is, processes in the system run in their own independent memory space. Lines in a process can share the memory space allocated by the system to this process.

Threads can not only share the memory of the process, but also have their own memory space. This memory space is also called thread stack, which is allocated by the system when creating a thread. It is mainly used to save the data used inside the thread, such as the variables defined in the thread execution function.

Note: any thread will execute a function when it is established. This function is called thread execution function. This function can also be regarded as the entry point of the thread (similar to the main function in the program). This function must be executed no matter what language or technology is used to establish the thread (the expression of this function may be different, but there will always be such a function). For example, in windows, the third parameter of the API function createthread used to create a thread is the pointer of the execution function.

After the operating system divides the process into multiple threads, these threads can execute concurrently under the management of the operating system, which greatly improves the running efficiency of the program. Although the execution of threads is performed by multiple threads at the same time from a macro point of view, in fact, this is only a cover for the operating system. Because a CPU can only execute one instruction at a time, it is impossible to execute two tasks at the same time on a computer with one CPU. In order to improve the running efficiency of the program, the operating system will remove this thread when a thread is idle and let other threads execute. This method is called thread scheduling. On the surface, multiple threads execute at the same time because the switching time between different threads is very short, and in general, the switching is very frequent. Suppose we have threads a and B. at run time, it may be that after a executes for 1 millisecond, after switching to B, B executes for 1 millisecond, then switches to a, and a executes for 1 millisecond. Since the time of 1 millisecond is difficult for ordinary people to perceive, it looks like a and B execute at the same time, but in fact, a and B execute alternately.

2、 Benefits of threads

If threads can be used reasonably, it will reduce development and maintenance costs, and even improve the performance of complex applications. For example, in GUI applications, events are better handled through the asynchronous characteristics of threads; In the application server program, multiple threads can be established to process the client's requests. Threads can even simplify the implementation of virtual machines. For example, the garbage collector of Java virtual machine (JVM) usually runs in one or more threads. Therefore, using threads will improve our applications from the following five aspects:

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 is blocked. This will cause a lot of waste of computing resources. Using multithreading in a program can run other threads when a thread is dormant or blocked and the CPU is just idle. This makes it difficult for the CPU to have free time. Therefore, CPU resources are fully utilized.

2. Simplify programming model

If the program only completes one task, just write a single threaded program and write code according to the steps of executing the task. However, to complete multiple tasks, if you still use single thread, you have to judge whether and when each task should be executed in the program. For example, three pointers of hour, minute and second of a clock are displayed. Using 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 process the display of these three pointers separately, each thread refers to a separate task. This helps developers understand and maintain the program.

3. Simplify the processing of asynchronous events

When a server application receives different client connections, the simplest way is to establish a thread for each client connection. Then the listening thread is still responsible for listening for requests from the client. If this kind of application is handled 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 blocked, that is, this thread will no longer be able to listen to the client request. To process multiple client requests in a single thread, you must use non blocking socket connection and asynchronous I / O. however, using asynchronous I / O is more difficult to control and error prone than using synchronous I / O. Therefore, asynchronous events like multiple requests can be handled more easily using multithreading and synchronous I / O.

4. Make GUI more efficient

When using a single thread to handle GUI events, you must use a loop to scan GUI events that may occur at any time. In addition to scanning GUI events, you have to execute other program codes inside the loop. If the code is too long, GUI events will be "frozen" until the code is executed.

A separate event dispatch thread is used in modern GUI frameworks such as swing, AWT, and SWT (event dispatch thread, EDT) to scan GUI events. When we press a button, the button click event function will be called in the event dispatch thread. Because the task of EDT is only to scan GUI events, the reflection of events in this way is very fast.

5. Cost saving

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

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

(2) Start multiple processes for a program

(3) Use multiple processes in programs.

The first method is the easiest to do, but it is also the most expensive. This method does not need to modify the program. Theoretically, any program can use this method to improve the execution efficiency. Although the second method does not need to buy new hardware, it is not easy to share data in this way. If the task to be completed by this program needs to share data, this method is not very convenient, and starting multiple threads will consume a lot of system resources. The third method just makes up for the shortcomings of the first method and inherits their advantages. In other words, there is no need to buy CPUs, nor will it occupy a lot of system resources because too many threads are started (by default, the memory space occupied by a thread is much smaller than that occupied by a process), and multithreading can simulate the running mode of multiple CPUs. Therefore, using multithreading is the cheapest way to improve program execution efficiency.

3、 Java thread model

Because Java is a pure object-oriented language, Java's thread model is also object-oriented. Java encapsulates the necessary functions of threads through the thread class. To create a thread, you must have a thread execution function that corresponds to the run method of the thread class. The thread class also has a start method, which is responsible for creating threads, which is equivalent to calling the windows thread creation function createthread When the start method is called, if the thread is established successfully, the run method of thread class will be called automatically. Therefore, any Java class that inherits thread can establish a thread through the start method of thread class. If you want to run your own thread execution function, you need to override the run method of the thread class.

In the java thread model, in addition to the thread class, there is also an interface runnable that identifies whether a Java class can be used as a thread class. This interface has only one abstract method run, that is, the thread execution function of the java thread model. Therefore, the only criterion for a thread class is whether the class implements the run method of the runnable interface, that is, the class with thread execution function is the thread class.

It can be seen from the above that there are two methods to establish threads in Java: one is to inherit the thread class, the other is to implement the runnable interface and establish threads through threads and runnable classes. In fact, these two methods are essentially one method, that is, they both establish threads through the thread class and run the run method. However, the big difference between them is that threads are created by inheriting the thread class. Although it is easier to implement, Java does not support multiple inheritance. Therefore, if this thread class inherits threads, it can no longer inherit other classes. Therefore, the java thread model provides a method to create threads by implementing the runnable interface, In this way, thread classes can inherit business-related classes when necessary, rather than thread classes

Original text: http://java.chinaitlab.com/line/778508.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>