100 lines of Java code build a thread pool

xiaoxiao2021-03-06  91

In a modern operating system, there is a very important concept - threads, almost all current popular operating systems support threads, threads from the concept of processes in the operating system, have their own virtual address space, data, data Sections and stacks, and each occupy different system resources (such as files, environment variables, etc.). Unlike this, threads cannot exist alone. It is attached to the process and can only be derived from the process. If a process is born out of two threads, the two threads share the global variables and code segments of this process, but each thread has their own stacks, so they have their own local variables, threads are still in the UNIX system Further divided into user-level threads (managed from process from process) and system-level threads (managed by the operating system's scheduler).

Since there is a process, why do you want to make a thread concept? Because you create a small system resource for some small applications than to create a new process, you may not feel this, but use threads for applications that are particularly available in concurrent processes. A better performance is achieved than the usage process, thereby reducing the burden on the operating system. In addition, the thread sharing creates a global variable of its process, so communication programming between threads will be more simple, fully abandoned the IPC programming of the traditional processes, while using shared global variables to make thread interval communication.

With this concept, we will enter the topic below, let's take a look at how the thread pool is? In fact, the principle of thread pool is simple. It is similar to the concept of buffer in the operating system. Its process is as follows: first start a number of threads and let these threads are in sleep state, when the client has a new request, Wake the thread in the thread pool, let it handle this request for the client. After processing this request, the thread is in sleep. Maybe you may ask: Why do you want to get so trouble, if you have a new request, I will create a new thread is not finished? This may be a good way, because it can make your code relatively easy, but you ignore an important issue - performance! Take my own unit, my unit is a bank network center in the provincial data, the peak period, the client requests and exceeds 100, if each client is requested to create a new thread, That consumable CPU time and memory will be amazing, if you use a thread pool with 200 threads, it will save a lot of system resources, making more CPU time and memory to handle actual commercial applications, and Not frequent thread creation and destruction.

Full text reading:

100 lines of Java code build a thread pool

转载请注明原文地址:https://www.9cbs.com/read-106692.html

New Post(0)