Java thread pool learning 1

Such as web servers, database servers, file servers and mail servers, and many other server applications for processing a large number of short tasks from some remote source. A simplistic model to build server applications is: whenever a request arrives to create a new service object, and then in the new service targets for service requests. But when there are concurrent access servers constantly creating and destroying objects of great spending a lot of requests. Therefore, to improve server efficiency is a means to minimize the number of creating and destroying objects, especially some very resource-intensive objects are created and destroyed, thus introducing the concept of "pool", the concept of "pool" makes it possible to customize certain the amount of resources, then these resources are multiplexed, rather than frequent creation and destruction.
Thread pool thread is a technique previously created. Before the thread pool task has not yet come, to create a certain number of threads put into the free queue. These threads are in a sleep state, that were started, do not consume CPU, but only take up less memory space. When a request comes in, the buffer pool assigned to this request for an idle thread, the request comes running in this thread, for processing. When the pre-created threads are running, that is not enough pre thread, the thread pool is free to create a new thread a certain amount, for handling more requests. When the system is relatively idle time, that can be left disabled by removing the thread part.
Note the thread pool
, although the thread pool is a powerful mechanism to build multi-threaded applications, but using it is not without risks. To be a problem when using a thread pool thread pool size and pay attention to the relationship between performance, pay attention to concurrent risk of deadlock, lack of resources and thread leakage.
(1) thread pool size. Multi-threaded applications thread is not better, we need to determine the size of the thread pool according to the system operating environment and application hardware and software features of its own. Generally, if the rational structure of the code words, the number of threads to fit with the number of CPU. If the thread running possible obstruction, the corresponding increase in the size of the pool; adaptive algorithm may be used if necessary to dynamically adjust the size of the thread pool to improve the overall performance of the system and the effective utilization of the CPU.
(2) concurrent errors. Pay special attention to multi-threaded applications concurrent error, to ensure the correctness of the program from the logic, pay attention to avoid the occurrence of deadlock.
(3) the thread leak. This thread pool is a serious problem in the application, when the task is finished and did not return to the thread pool thread will occur leakage.
Simple thread pool design
a typical thread pool it should include the following parts:
1, the thread pool manager (the ThreadPool), for activating, deactivating, the thread pool manager
2, worker thread (WorkThread), the thread pool thread
3, request interface (WorkRequest), the object creation request, for the implementation of a scheduled task threads
4, request queue (the RequestQueue), requests for storing and extracting
5, result queue (ResultQueue), execution returns to the storage request the result of
thread pool manager, by the method of adding (PutRequest) addition request to the request queue (the RequestQueue) requests, which need to implement request interface in advance, i.e., the work transfer function, parameters, results of the processing functions, and exception handling function. After initialization work a certain number of threads that continue to see by way of polling request queue (RequestQueue), as long as there is a request, it will extract the request for execution. Then, the thread pool manager calls the method (poll) Check result queue (resultQueue) has a value, if the value is taken, the result of calling the function execution process. By the above teachings, not difficult to find, the core of this system is that the resource request queue and the queue result, worker threads obtained by polling figures requestQueue, the main thread queues by looking at the results, the results obtained. Therefore, this cohort design to achieve thread synchronization, and the design must blocking and timeout mechanisms to prevent excessive cpu overhead as a result of constantly polling. In this article, the language will be used python, python's Queue, it is to achieve a good thread synchronization mechanisms.

发布了95 篇原创文章 · 获赞 0 · 访问量 1917

Guess you like

Origin blog.csdn.net/qq_42894864/article/details/104099972