In the modern operating system, there is a very important concept--thread, almost all the current popular operating systems support threads, threads from the concept of the process in the operating system, the process has its own virtual address space and body segments, data segments and stacks, and each occupies a different system resources (such as files, environment variables, etc.). Unlike this, a thread cannot exist alone, it is attached to a process and can only be derived from a process. If a process derives two threads, the two threads share the global variables and code snippets of the process, but each thread has its own stack, so they have their own local variables, Threads are further divided into user-level threads (managed by processes themselves) and system-level threads (managed by the operating system's scheduler) in UNIX systems. Now that you have a process, why do you want to propose the concept of threading? Because, compared to the creation of a new process, creating a thread will consume much less system resources and may not be felt for some small applications, but for applications that have a particularly heavy number of concurrent processes, threading can be more performance-intensive than using a process, thus reducing the burden on the operating system. In addition, the thread shares the global variables of the process that created it, so the communication programming between threads will be simpler, and it is entirely possible to discard the traditional IPC programming of interprocess communication and use shared global variables to communicate between threads.
With the above concept, we go to the bottom of the matter, to see what the thread pool is what? In fact, the thread pool principle is very simple, similar to the concept of a buffer in the operating system, its process is as follows: Start a number of threads first, and leave these threads asleep, and when the client has a new request, it wakes up a sleep thread in the thread pool to handle the client's request. When the request is processed, the thread is in a sleep state. Perhaps you might ask: why bother, if I create a new thread every time the client has a new request? This may be a good idea, because it makes it easier for you to write code, but you overlook an important problem-performance! Just take my unit, my unit is a provincial-level data-intensive Banking Network Center, the peak per second client request concurrency of more than 100, if the request for each client to create a new thread, the CPU time and memory will be amazing, if the use of a thread pool with 200 threads, That would save a lot of system resources, allowing more CPU time and memory to handle actual business applications rather than frequent thread creation and destruction.
Now that everything is clear, let's begin to implement a real thread pool, thread programming can be implemented in many languages, such as C, C + +, Java, and so on, but different operating systems provide different thread API interfaces, so that you can better understand the thread pool principle and avoid getting bogged down in cumbersome API calls. I use the Java language to implement it, because the Java language is a cross-platform language, so you do not have to use a different operating system to compile and run this program, as long as you have installed more than JDK1.2 version, can compile and run the program correctly. In addition, the Java language itself has a thread object built into it, and the Java language is completely object-like, so it gives you a clearer idea of how the thread pool works, and if you look at the title of this article, you'll find that the entire sample program has only about 100 lines of code.
This sample program consists of three classes, the first is the Testthreadpool class, it is a test program to simulate the client's request, when you run it, the system will first display the thread pool initialization information, and then prompts you to enter a string from the keyboard, and press ENTER, At this point you will notice that a message is displayed on the screen telling you that a line one thread is processing your request, if you quickly enter a line of string, you will find that threads in the thread pool are awakened to handle your request, and in this case I have created a thread pool with 10 threads, and if there are no threads in the thread pool, The system prompts you for a warning, but if you wait a moment, you will notice that the cable is on the way to sleep, and you can send a new request.
The second class, the Threadpoolmanager class, as the name suggests, is a class for managing the thread pool, its primary responsibility is to initialize the thread pool and assign different threads to the client's request, and if the thread pool is full, it will send a warning message to you.
The last class is the Simplethread class, which is a subclass of the thread class, that actually handles the client's request, Simplethread is asleep when the sample program initializes, But if it receives the dispatch information sent by the Threadpoolmanager class, it wakes itself up and processes the request.
First, let's look at the source code of the Testthreadpool class:
Testthreadpool.java
1 Import java.io.*;
2
3
4 public class Testthreadpool
5 {
6 public static void main (string[] args)
7 {
8 try{
9 BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
Ten String S;
One Threadpoolmanager manager = new Threadpoolmanager (10);
while ((s = br.readline ())!= null)
13 {
Manager.process (s);
15}
}catch (IOException e) {}
17}
18}