Server programs use thread technology to respond to customer requests that they have been used, it may be considered that this is very efficient, but have you ever thought about optimizing how to use threads. This article will introduce you how the server programs use the thread pool to optimize performance and provide a simple thread pool implementation.
Technical background for thread pool
In object-oriented programming, creating and destroying objects is time-consuming, because you create an object to get a memory resource or more resources. This is even more such in Java, and the virtual machine will try to track every object so that garbage recovery can be used after the object is destroyed. So a means of improving service provider is to minimize the number of times of creating and destroying objects, especially some resource-consuming objects creation and destruction. How to use an existing object to serve a key issue that needs to be resolved, in fact, this is why some "pool-based resources" technology. For example, the database connection pool familiar to everyone is arguing in this idea, and the thread pool technology will be introduced in this article also in line with this idea.
At present, some famous big companies are particularly optimistic about this technology, and this technology has been applied in their products. For example, IBM's WebSphere, Iona ORBIX 2000 in the Sun's JINI, Microsoft's MTS (Microsoft Transaction Server 2.0), COM , etc.
Do you want to apply this technology in the server program now?
How to improve the performance of server programs
I mentioned that the server program refers to a program that can accept a client request and can process a request, not just a network server program that accepts a network client request.
Multi-threading technology mainly solves the problem of multiple threads performed within the processor unit, which can significantly reduce the idle time of the processor unit, increase the throughput of the processor unit. However, if the multi-threaded application is improper, the processing time for a single task is increased. Can give a simple example:
Assume that the time is tab for a server
T1 Create a thread
T2 performs the task in the thread, including the time required to synchronize the thread
T3 thread destroyed time
Obviously T = T1 T2 T3. Note that this is an extremely simplified assumption.
It can be seen that T1, T3 is the overhead of multi-threaded itself, and we are eager to reduce T1, T3, thereby reducing T time. But the user of some threads did not pay attention to this, so it frequently creates or destroy the thread in the program, which results in considerable proportion of T1 and T3 in T. Obviously this highlights the weaknesses of the thread (T1, T3), not the advantage (concurrency).
Thread pool technology is concerned about how to shorten or adjust T1, T3 time technology, thereby improving server program performance. It arranges T1, T3, respectively, or some time periods or some idle time periods, such as idle time, which will not have T1 and T3 overhead when the server program handles the customer request.
The thread pool not only adjusts the time period generated by T1, T3, but it also significantly reduces the number of creating threads. Looking at an example:
Suppose one server has to process 50,000 requests one day, and each request requires a separate thread to complete. We compare the total number of threads generated when using thread pool technology and server that is not conducive to thread pool technology. In the thread pool, the number of threads is generally fixed, so the total number of threads does not exceed the number of threads in the thread pool (hereinafter referred to as thread pool size), and if the server does not use the thread pool to handle these requests Total number of threads 50000. The general thread pool size is much smaller than 5000. So the server program that utilizes the thread pool does not waste time during processing the request, thereby increasing efficiency.
These are assumptions, not fully explain the problem, below I will discuss the simple implementation of the thread pool and compare the program to illustrate the technical advantages and application areas.
Simple implementation of thread pool and comparison test
Generally, a simple thread pool includes at least the following components. Threadpool Manager: used to create and manage thread pools
WorkThread: Thread pool
Task Interface (Task): Interfaces that each task must be implemented to perform the execution of the working thread scheduling task.
Task queue: Task for storing unprocessed tasks. A buffer mechanism is provided.
The thread pool manager has at least the following functions: Create a thread pool, destroy the thread pool, and add a new task to create a threaded pool part of the code as follows:
...
// Create Threads
Synchronized (WorkthreadVector)
{
For (int J = 0; j
{
Threadnum ;
Workthread Workthread = New Workthread (TaskVector, Threadnum);
WorkthreadVector.AddeElement (Workthread);
}
}
...
Note that synchronization WorkthreadVector does not reduce efficiency, and the opposite increases efficiency, please refer to Brian Goetz's article. Some code for the destruction of the thread pool is as follows:
...
While (! WorkthreadVector.Isempty ())
{
IF (Debuglevel> 2)
System.out.println ("STOP:" (i));
i ;
Try
{
Workthread Workthread = (Workthread) WorkthreadVector.Remove (0);
Workthread.closethread ();
CONTINUE;
}
Catch (Exception Exception)
{
IF (Debuglevel> 2)
Exception.printStackTrace ();
}
Break;
}
...
The part of the code to add a new task is as follows:
...
Synchronized (TaskVector)
{
Taskvector.addelement (taskobj);
Taskvector.notifyAll ();
}
...
Working thread is a thread that can loopulate tasks, waiting for a task. Since the code is more than this.
The task interface provides a unified interface for all tasks to work thread processing. The task interface mainly specifies the entrance of the task, the end of the task, and the execution status of the task, etc. There is a download of related code at the end of the article.
The thread pool structure described above is simple, and some complex thread pool structures will no longer discuss this.
In the download code, there is a test driver (TestthReadpool), I use this test program to count the following test results. There are two parameters to set:
The thread pool is thread, that is, the thread size.
The number of tasks to be completed.
A parameter is fixed, and the other parameter changes to examine the different results generated by the two parameters. The test machines used are common PCs (Win2000 JDK1.3.1) and Sun Server (Solaris Unix JDK1.3.1), and the machine configuration is inconvenient to indicate.
Table 1: Test data and corresponding results
Thread pool size tasks No time (units: milliseconds, OS: win) Application thread pool (unit: milliseconds, OS: win) does not apply the time (unit: milliseconds, OS: Solaris) The time used in the application thread pool (unit: milliseconds, OS: Solaris)
1 5000 3896 130 6513 3272 5000 3455 151 6221 659
4 5000 3425 120 5448 433
8 5000 3475 160 5769 1478
16 5000 3505 211 5785 1970
32 5000 3455 251 6403 875
64 5000 3595 501 5182 1103
128 5000 3515 881 5154 405
256 5000 3495 3104 5502 1589
512 5000 3425 5488 5667 1262
16 1 20 0 22 3
16 2 20 20 21 13
16 4 20 10 27 10
16 8 20 20 22 24
16 16 30 20 29 48
16 32 40 20 46 108
16 64 60 20 72 199
16 128 110 20 148 335
16 256 20120 252 132
16 512 411 40 522 382
16 1024 811 71 1233 610
16 2048 1552 80 2045 135
16 4096 2874 250 4828 787
Figure 1. Influence of the performance of the thread pool on the server program
According to the above statistics, the figure is obtained:
Figure 2. Immacity of the task number on server programs
Data analysis is as follows:
Figure 1 is a change in changing the size of the thread pool on the performance of the server, during which the number of tasks to be completed is fixed to 5000. As can be seen from Figure 1, it is possible to improve the efficiency of a large number of task processing, but once the size is unreasonable (too large or too small), it will seriously reduce the performance of server performance. In theory, "too small" will have a task that cannot be handled in time, but it shows that some small-sized thread pool performance is very good, because there are many threads in the test driver, and this overhead is compared to completing a single The task time cannot be ignored. "Excessive" will have a problem with the synchronous overhead of the thread, and the CPU time is switched between the thread, and it is clear that the chart is displayed. It can be seen that any good technology is visible, if abuse can cause catastrophic consequences.
Figure 2 is an impact server program with a different number of tasks, during which the server thread pool is fixed to 16. It can be seen that the advantages of the thread pool when dealing with a small amount of tasks. Therefore, thread pool technology has a certain range of adaptation, and will be discussed later. However, for a large number of tasks, the advantage of the thread pool is very excellent, and the server program handles the request time has fluctuations, but its average is relatively small.
It is worth noting that in the test scheme, the completion time of the statistical task does not contain the time of creating a thread pool. When working in actual thread pool, the time to create a thread pool is not necessary to use the thread pool processing task.
Since the test driver has a lot of synchronous code, especially the synchronization of the waiting thread, the code is called for the SleepTowait (long L) method), which reduces the code execution efficiency, which is a shortcoming of the test driver, but this test driver You can explain the advantages of the thread pool relative to the simple use of threads.
Discussion on advanced thread pool
There are some problems with simple thread pools. For example, if there is a large number of customer requirements servers for their services, but because the thread pool work thread is limited, the server can only be submitted by some customer services, other customers, can only be in the task queue Wait for processing. Some system designers may dissatisfaction because they have strict response time for server programs, so it may suspect the feasibility of thread pool technology when designing, but the thread pool has a corresponding solution. Adjusting the optimized thread pool size is a problem that the advanced thread pool should be solved. There are main solutions:
Solution 1: Dynamic increased work thread
In some advanced thread pools, a function of a number of working threads that can be dynamically changed is generally provided to accommodate burst requests. Once the request, it will gradually reduce the number of working threads in the thread pool. Of course, the thread increases can be used in a forehead, ie, increases a batch of work threads instead of a request to create a thread. Batch creation is a more effective way. The program also has the upper limit and lower limit of the number of working threads in the thread pool. Otherwise, this flexible approach becomes a wrong way or disaster, because frequent creating threads or short-time generating a large number of threads will deviate from the original original original intention to use the thread. Example: TaskManager in Jini is a brilliant pool manager that is dynamically increasing working thread. SQL Server uses a single process multi-thread system structure, 1024 quantities of thread pools, dynamic thread allocation, theoretical upper 32767.
Scenary 2: Optimizing the number of working threads
If you don't want to apply a complex strategy in the thread pool to ensure that the number of work threads meet the application requirements, you should count the number of requests according to the principles of statistics, such as how many mission requirements in the average period of one second, and according to the peak hours The system's tolerance and customer endurance ability balance the estimate of a reasonable thread pool size. The size of the thread pool is really difficult to determine, so sometimes the experience value is simply used.
Example: The size of the thread pool in the MTS is fixed to 100.
Solution 3: A server offers multiple thread pools
This solution is adopted in some complex system structures. This can use different thread pool processing according to different tasks or task priorities.
Example: COM uses multiple thread pools.
These three programs have advantages and disadvantages. Different programs or simply combine these three programs may be used in different applications to solve practical problems.
Thread pool technology scope and should pay attention
Here is some of my thread pool applications I summarized, which may be uncomfortable.
Application range of thread pool:
Need a large number of threads to complete the task, and the time to complete the task is short.
The web server completes the task of requesting such a web page, using thread pool technology is very suitable. Because a single task is small, and the number of tasks is huge, you can imagine a hot website to click on the number of clicks.
But for a long task, such as a Telnet connection request, the advantages of the thread pool are not obvious. Because Telnet session time is mostly the creation time of threads.
The demanding applications requiring performance requirements, such as requiring the server to quickly request the client.
Accept a large number of requests, but not allow the server to generate a large number of threads.
The sudden large number of customer requests, will generate a large number of threads without a thread pool, although the most operating system thread number is not a problem, generating a large number of threads in a short time may enable memory to reach the limit, and "OutofMemory" mistake.
Conclude
This article is just a brief introduction of thread pool technology. It can be seen that the performance improvement of the server program is remarkable. Thread pool technology has a wide range of application prospects in the server field. I hope this technology can be applied to your multi-threaded service.
Reference
Danny Ayers, etc .: Professional Java Server Programming
TARAK MODI: Why Thread Pools Are Important in Java
Brett Spell: Professional Java Programming
JINI (TM) V1.2.1 code
Brian Goetz Java Theory and Practice Columns
Microsoft Knowledge Base Article: Q282490