Thread refers to an execution process in a process. A process can run multiple threads. For example, the java.exe process can run many threads. Threads always enter a process, and multiple threads in a process share the process's memory.
Thread in Java refers to an instance of the java.lang.Thread class or the execution of a thread. Use the java.lang.Thread or java.lang.Runnable interface to write code to define, instantiate, and start new threads. (Recommended learning: java course )
Each thread in Java has a call stack. Even if no new thread is created in the program, the thread is running in the background. The main() method runs in a thread, called the main thread. Once a new thread is created, a new call stack is created.
Threads are divided into two categories: User threads and waiting threads. When all user threads have finished executing, the JVM automatically shuts down. However, the waiting thread is not independent of the JVM. The waiting thread is generally created by the operating system or the user.
The benefits that threads bring to us
If threads can be used rationally, development and maintenance costs will be reduced, and the performance of complex applications can even be improved. For example, in GUI applications, events can be better processed through the asynchronous nature of threads; in application server programs, multiple threads can be established to handle client requests.
Threads can even simplify the implementation of virtual machines. For example, the garbage collector of the Java Virtual Machine (JVM) usually runs in one or more threads. Therefore, using threads will improve our applications in the following five aspects:
1. Make full use of CPU resources
Most computers in the world now only have 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 while the program is blocked. This will cause a lot of waste of computing resources.
Using multi-threading in a program can run other threads when a thread is sleeping or blocked and the CPU happens to be idle. In this way, it is difficult for the CPU to be idle. Therefore, CPU resources are fully utilized.
2. Simplified programming model
If the program only completes one task, then just write a single-threaded program and write the code according to the steps to perform the task That’s it. But to complete multiple tasks, if you still use a single thread, you have to determine in the program whether and when each task should be executed. For example, it displays the hours, minutes, and seconds of a clock.
If you use a single thread, you have to judge the rotation time and angle of these three pointers one by one in the loop. If three threads are used to handle the display of these three pointers, then it means performing a separate task for each thread. 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 to handle it is to connect for each client Create a thread. The listening thread is then still responsible for listening for requests from the client.
If this kind of 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, also That is, this thread will no longer be able to listen to client requests.
If you want to handle multiple client requests in a single thread, you must use non-blocking Socket connections and asynchronous I/O. However, using asynchronous I/O is more difficult to control and more 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 the GUI more efficient
When using a single thread to handle GUI events, you must use a loop to scan for GUI events that may occur at any time. In the loop In addition to scanning GUI events internally, it also has to execute other program codes. If the code is too long, the GUI events will be "frozen" until the code is executed.
In modern GUI frameworks (such as SWING, AWT and SWT), a separate event dispatch thread (EDT) is used to scan GUI events. When we press a button, the button's click event function will be called in this event dispatch thread.
Since the task of EDT is only to scan GUI events, this method responds to events very quickly.
5. Save costs
There are generally three ways to improve the execution efficiency of the program:
(1) Increase the computer’s Number of CPUs.
(2) Start multiple processes for one program
(3) Use multi-threading in the program.
The above is the detailed content of What is a thread in java. For more information, please follow other related articles on the PHP Chinese website!