Java multithreading and thread pool

Source: Internet
Author: User

1. What is 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.

 

Multithreading is a mechanism that allows concurrent execution of Multiple Instruction Streams in a program. Each instruction stream is called a thread and is independent of each other. A thread is also called a lightweight process. It has independent execution control like a process and is scheduled by the operating system. The difference is that the thread does not have independent storage space, instead, it shares a storage space with other threads in the process, which makes the communication between threads much simpler than the process. The execution of multiple threads is concurrent, that is, logically "at the same time", regardless of whether it is physically "at the same time ". If the system only has one CPU, the real "at the same time" is impossible, but because the CPU speed is very fast, users do not feel the difference, so we do not need to care about it, you only need to imagine that each thread is executed at the same time.
The biggest difference between multithreading and traditional single-Thread Programming is that the control flow of each thread is independent of each other, so that the code between each thread is executed in disorder, the thread scheduling and synchronization issues will be discussed later.

 

 

Therefore, using threads will improve our applications in the following five aspects:

1. Make full use of CPU resources

Most computers in the world only have one CPU. Therefore, it is particularly important to make full use of CPU resources. When a single-threaded program is executed, the CPU may be idle due to program congestion. This will cause a lot of waste of computing resources. Multithreading can be used in a program to run other threads when a thread is in sleep or blocked State while the CPU is in idle state. In this way, it is difficult for the CPU to be idle. Therefore, the CPU resources are fully utilized.

2. Simplified Programming Model

If the program completes only one task, you only need to write a single-threaded program and write code according to the steps for executing the task. However, to complete multiple tasks, if you still use a single thread, You have to determine in the program whether each task should be executed and when to execute it. For example, the hour, minute, and second of a clock are displayed. When using a single thread, You must 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 the three pointers separately, each thread is used to execute a separate task. This helps developers understand and maintain programs.

3. Simplified asynchronous event processing

When a server application receives different client connections, the simplest solution is to create a thread for each client connection. Then the listening thread is still responsible for listening for requests from the client. If such an application uses a single thread for processing, when the listening thread receives a client request, it starts to read the data sent from the client. After reading the data, the read method is blocked. That is to say, this thread will no longer be able to listen to client requests. To process multiple client requests in a single thread, you must use a non-blocking socket connection and asynchronous I/O. however, asynchronous I/O is more difficult to control and error-prone than synchronous I/O. Therefore, using multithreading and synchronous I/O makes it easier to process asynchronous events similar to multiple requests.

4. Make the 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 must also run other program code within the loop. If the code is too long, the GUI event will be 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 GUI events. When we press a button, the Click Event function of the button will be called in the event dispatch thread. Because EDT tasks only scan GUI events, this method reflects events very quickly.

5. Cost Saving

There are three methods to improve program execution efficiency:

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

(2) Start Multiple processes for a program

(3) Use multiple processes in a program.

The first method is the easiest, but also the most expensive. This method does not need to be modified. Theoretically, any program can use this method to improve execution efficiency. The second method is not to purchase new hardware, but it is not easy to share data. If the task to be completed by this program needs to share data, this method is not convenient, in addition, starting multiple threads will consume a large amount of system resources. The third method makes up for the shortcomings of the first method, and inherits their advantages. That is to say, neither the purchase of CPU nor the occupation of a large amount of system resources due to too many threads (by default, the memory space occupied by a thread is much smaller than the memory space occupied by a process), and multithreading can simulate the running mode of multiple CPUs. Therefore, multithreading is the cheapest way to improve program execution efficiency.

Iii. Java thread model

Because Java is a pure object-oriented language, the Java thread model is also object-oriented. Java encapsulates all the functions required by the thread through the Thread class. To create a thread, you must have a thread to execute the function. The thread execution function corresponds to the run method of the thread class. The thread class also has a start method, which is used to create a thread, which is equivalent to calling the createthread function of windows. after the start method is called, if the thread is successfully established and the run method of the thread class is automatically called. Therefore, any Java class that inherits the thread can use the start method of the thread class to create a thread. If you want to run your own thread to execute a function, you must overwrite the run method of the thread class.

In the Java thread model, besides the Thread class, there is also a runnable interface 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 standard for a Thread class is whether the class implements the run method of the runnable interface. That is to say, the class with the thread execution function is the Thread class.

We can see from the above that there are two ways to create a thread in Java: one is to inherit the Thread class, and the other is to implement the runnable interface,

 

 

 

 

For example, the download function on a mobile phone:

1. You need to download an interface. You also need to download it when you jump to another interface. multithreading is required at this time. Start several different threads. Because the previous thread may have not stopped.

 

 

 

 

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.