Thread pool control method and device, electronic equipment and storage medium

By judging and creating new core threads in the thread pool to allocate tasks, the problem of insufficient performance of native thread pools in high concurrency scenarios is solved, and task processing speed is improved.

CN118916168APending Publication Date: 2024-11-08CHINA MERCHANTS BANK
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411073050.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-06
Publication Date
2024-11-08

Smart Images

  • Figure CN118916168A_ABST
    Figure CN118916168A_ABST
Patent Text Reader

Abstract

The invention discloses a control method and device of a thread pool, electronic equipment and a storage medium, and belongs to the technical field of computers. After the thread pool receives a to-be-processed task, whether a core thread in an idle state exists or not is determined; creating a new core thread under the condition that the core thread in the idle state does not exist and the total quantity of the core threads is smaller than the upper limit of a preset thread number; and distributing the task to be processed for the new core thread. The method comprises the following steps: after a thread pool receives a to-be-processed task, firstly determining whether a core thread in an idle state exists at present, and if it is determined that the core thread in the idle state does not exist and the total amount of the core threads is smaller than the upper limit of a preset thread, directly creating a new core thread, namely preferentially increasing the number of the core threads; therefore, more to-be-processed tasks can be processed at the same time, the task processing speed is increased, and the task processing requirement of a high-concurrency task scene is met.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a thread pool control method, device, electronic device and storage medium. Background Art

[0002] A thread pool is a mechanism for managing and reusing threads. It allows a program to create a group of threads in advance and then reuse these threads to perform multiple tasks, rather than creating a new thread each time a task needs to be performed. Currently, in conventional task recommendation technology architectures, native thread pools are mostly used to complete related recommendation tasks. However, in the face of high-concurrency task scenarios, the performance of native thread pools in completing tasks is poor, and it is difficult to meet the task processing requirements of the above high-concurrency task scenarios.

[0003] The above contents are only used to assist in understanding the technical solution of the present application and do not constitute an admission that the above contents are prior art. Summary of the invention

[0004] The main purpose of this application is to provide a thread pool control method, device, electronic device and storage medium, aiming to solve the technical problem that the native thread pool has poor performance when completing tasks and is difficult to meet the task processing requirements of high-concurrency task scenarios.

[0005] To achieve the above purpose, the present application proposes a thread pool control method, which is applied to a thread pool, wherein the thread pool is configured with core threads, and the thread pool control method includes:

[0006] After receiving the task to be processed, determining whether there is a core thread in an idle state;

[0007] If there is no core thread in the idle state and the total number of core threads is less than a preset upper limit of the number of threads, create a new core thread;

[0008] The to-be-processed tasks are allocated to the new core thread.

[0009] Optionally, the thread pool is further configured with a task waiting queue. After the step of determining whether there is a core thread in an idle state, the method further includes:

[0010] When there is no core thread in the idle state and the total number of the core threads is equal to or greater than a preset thread upper limit, the to-be-processed task is added to the task waiting queue.

[0011] Optionally, the step of adding the to-be-processed task to the task waiting queue includes:

[0012] Determine the remaining memory in the heap of the virtual machine where the thread pool is located, and when the remaining memory in the heap is greater than a preset memory limit threshold, determine the actual queue length of the task waiting queue;

[0013] When the actual queue length is less than the maximum queue length of the task waiting queue, the to-be-processed task is added to the task waiting queue.

[0014] Optionally, the thread pool control method further includes:

[0015] If there is a core thread in the thread pool that changes from an active state to an idle state, the core thread that changes from an active state to an idle state is assigned a to-be-processed task in the task waiting queue.

[0016] Optionally, after the step of determining whether there is a core thread in an idle state, the method further includes:

[0017] In the case that the core thread in the idle state exists, the to-be-processed task is allocated to the core thread in the idle state.

[0018] Optionally, the thread pool is provided with thread pool configuration parameters, and the thread pool control method further includes:

[0019] Monitoring the thread pool configuration parameters;

[0020] After the thread pool configuration parameters are updated, the configuration is updated based on the updated thread pool configuration parameters, wherein the configuration includes at least one of a preset upper limit on the number of threads of the thread pool, a survival time of core threads in an idle state in the thread pool, a type of task waiting queue of the thread pool, a maximum queue length of the task waiting queue, a rejection strategy for pending tasks, and an alarm strategy of the thread pool.

[0021] Optionally, the thread pool control method further includes:

[0022] Generate a thread pool log based on the working status of the core thread and the working status of the task waiting queue configured by the thread pool;

[0023] The thread pool log is sent to a preset log topic, so that a preset consumer can obtain the thread pool log from the preset log topic and visualize the thread pool log.

[0024] In addition, to achieve the above purpose, the present application also proposes a thread pool control device, which is applied to a thread pool, wherein the thread pool is configured with core threads, and the thread pool control device includes:

[0025] A determination module, used to determine whether there is a core thread in an idle state after receiving the task to be processed;

[0026] A creation module, used for creating a new core thread when there is no core thread in the idle state and the total number of core threads is less than a preset upper limit of the number of threads;

[0027] The allocation module is used to allocate the to-be-processed tasks to the new core thread.

[0028] In addition, to achieve the above-mentioned purpose, the present application also proposes an electronic device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the thread pool control method as described above.

[0029] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by the processor, the steps of the thread pool control method described above are implemented.

[0030] One or more technical solutions proposed in this application have at least the following technical effects:

[0031] In the embodiment of the present application, after receiving the task to be processed, the thread pool determines whether there is a core thread in an idle state; if there is no core thread in an idle state and the total number of core threads is less than the preset thread number upper limit, a new core thread is created; and the task to be processed is assigned to the new core thread. That is, when the thread pool receives the task to be processed, it will first determine whether there is a core thread in an idle state. If it is determined that there is no core thread in an idle state and the total number of core threads is less than the preset thread upper limit, a new core thread is directly created, that is, the number of core threads is increased first, so that more tasks to be processed can be processed simultaneously, speeding up the task processing speed to meet the task processing requirements of high-concurrency task scenarios. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0033] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0034] Figure 1 This is a flow chart of the first embodiment of the thread pool control method of the present application;

[0035] Figure 2 This is a flow chart of the second embodiment of the thread pool control method of the present application;

[0036] Figure 3 This is a schematic diagram of the overall process of the control method of the thread pool in this application;

[0037] Figure 4 This is a flowchart of the third embodiment of the thread pool control method of the present application;

[0038] Figure 5 This is a schematic diagram of the results of different types of thread pools facing high-concurrency tasks in the thread pool control method of this application;

[0039] Figure 6 A schematic diagram of the configuration interface for the results of different types of thread pools facing high-concurrency tasks in the thread pool control method of this application;

[0040] Figure 7 This is a flowchart of the fourth embodiment of the thread pool control method of the present application;

[0041] Figure 8 This is a functional structure diagram of the thread pool management center in the thread pool control method of this application;

[0042] Fig. 9 This is a schematic diagram of the module structure of the control device of the thread pool in an embodiment of the present application;

[0043] Fig.10 This is a schematic diagram of the device structure of the hardware operating environment involved in the thread pool control method in the embodiment of the present application.

[0044] The purpose, features and advantages of this application will be further described in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0045] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.

[0046] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.

[0047] The main solution of the embodiment of the present application is: after receiving the task to be processed, determine whether there is a core thread in an idle state; if there is no core thread in an idle state and the total number of core threads is less than the preset thread number upper limit, create a new core thread; and assign the task to be processed to the new core thread.

[0048] At present, in the conventional task recommendation technology architecture, most of them use native thread pools to complete related recommendation tasks. The control logic of the native thread pool is that when there is a new task and there is no idle core thread, the native thread pool will give priority to adding the new task to the task queue to ensure that each core thread has sufficient CPU (Central Processing Unit / Processor,

[0049] CPU) resources, and when the number of tasks in the task queue reaches the queue limit, a new core thread will be added. It is understandable that when the native thread pool prioritizes the CPU resources of each core thread, the number of tasks that can be processed simultaneously by the native thread pool is limited when facing high-concurrency task scenarios, resulting in poor task processing performance and difficulty in meeting the task processing requirements of high-concurrency task scenarios.

[0050] The present application provides a solution, which is applied to a thread pool. Core threads are configured in the thread pool. When the thread pool receives a task to be processed, it will first determine whether there is a core thread in an idle state. If it is determined that there is no core thread in an idle state and the total number of core threads is less than the preset thread upper limit, a new core thread is directly created, that is, the number of core threads is increased preferentially, so that more tasks to be processed can be processed simultaneously, speeding up task processing to meet the task processing requirements of high-concurrency task scenarios.

[0051] It should be noted that the execution subject of this embodiment can be a computing service device with data processing, network communication and program running functions, such as a personal computer, server, tablet computer, mobile phone, etc., or an electronic device that can realize the above functions.

[0052] Based on this, the present application embodiment provides a thread pool control method, referring to Figure 1 , Figure 1 This is a flow chart of the first embodiment of the thread pool control method of the present application.

[0053] In this embodiment, the control method of the thread pool is applied to the thread pool, and the thread pool is configured with core threads. The control method of the thread pool includes steps S10 to S30:

[0054] Step S10, after receiving the task to be processed, determining whether there is a core thread in an idle state;

[0055] It should be noted that, in this embodiment, the above-mentioned thread pool control method can apply a thread pool, so that the thread pool can implement the control, management or scheduling of the core thread in the above-mentioned thread pool control method.

[0056] Exemplarily, core threads may be pre-configured in the thread pool, wherein the initial number of core threads in the thread pool may be set by a technician when creating the thread pool, which will not be described in detail here. After the thread pool receives a task to be processed, the thread pool obtains the current thread pool state, including the number of active threads and the number of core threads currently in existence. If the number of active threads is less than the number of core threads currently in existence, it indicates that there are core threads in an idle state. Conversely, if the number of active threads is equal to the number of core threads currently in existence, there are currently no idle core threads.

[0057] Step S20, creating a new core thread when there is no core thread in the idle state and the total number of core threads is less than a preset upper limit of the number of threads;

[0058] Exemplarily, when it is determined that there are no core threads in an idle state, it is further determined whether the total number of core threads currently existing is less than the preset upper limit of the number of threads. Similarly, the preset upper limit of the number of threads can be set by relevant technical personnel when creating a thread pool according to system resources (including the number of CPU cores, memory size, operating system restrictions, etc.), which will not be repeated here. If it is determined that the total amount is less than the preset upper limit of the number of threads, a new core thread is directly created. For example, the thread pool can create a new core thread through an internal thread factory (Thread Factory). The thread factory is responsible for creating threads and can set the name, priority, daemon status and other properties of the thread.

[0059] Step S30: assigning the to-be-processed task to the new core thread.

[0060] Exemplarily, after the new core thread is created, the to-be-processed task is assigned to the new core thread, and accordingly, the new core thread starts to process the to-be-processed task.

[0061] In this embodiment, after receiving the task to be processed, the thread pool determines whether there is a core thread in an idle state; if there is no core thread in an idle state and the total number of core threads is less than the preset thread upper limit, a new core thread is created; and the task to be processed is assigned to the new core thread. That is, when the thread pool receives the task to be processed, it will first determine whether there is a core thread in an idle state. If it is determined that there is no core thread in an idle state and the total number of core threads is less than the preset thread upper limit, a new core thread is directly created, that is, the number of core threads is increased first, so that more tasks to be processed can be processed simultaneously, speeding up the task processing speed to meet the task processing requirements of high-concurrency task scenarios.

[0062] In a feasible implementation manner, after the step of determining whether there is a core thread in an idle state, the method further includes step S11:

[0063] Step S11: when there is the core thread in the idle state, assign the to-be-processed task to the core thread in the idle state.

[0064] For example, if there is an idle core thread, the idle core thread can be directly assigned a pending task. Correspondingly, the idle core thread will then start to execute the assigned pending task. After the core thread starts to execute the task, its state will change from idle to active. A counter that records the thread state is maintained inside the thread pool to track the number of active threads.

[0065] Reference Figure 2 , is a flow chart of the second embodiment of the present application proposed based on the above first embodiment. In this embodiment, the same or similar parts as the above embodiment can refer to the above content and will not be repeated here. The thread pool is also configured with a task waiting queue. After the step of determining whether there is a core thread in an idle state, the method further includes step S200:

[0066] Step S200: When there is no core thread in the idle state and the total number of the core threads is equal to or greater than a preset thread upper limit, the to-be-processed task is added to the task waiting queue.

[0067] Exemplarily, the thread pool is also configured with a task waiting queue, i.e., a blocking queue. If there is no core thread in an idle state, and the total number of core threads is equal to or greater than the preset thread upper limit (usually, there will not be a situation where the number of core threads is greater than the preset thread upper limit), it means that there is no core thread in an idle state currently executing the received pending task, and a new core thread cannot be created to execute the received pending task. Accordingly, the pending task can be added to the task waiting queue to wait for further processing.

[0068] In a feasible implementation manner, the step of adding the to-be-processed task to the task waiting queue includes steps S210 to S220:

[0069] Step S210, determining the remaining memory in the heap of the virtual machine where the thread pool is located, and when the remaining memory in the heap is greater than a preset memory limit threshold, determining the actual queue length of the task waiting queue;

[0070] Step S220: When the actual queue length is less than the maximum queue length of the task waiting queue, the to-be-processed task is added to the task waiting queue.

[0071] It should be noted that in the Java Virtual Machine (JVM), the heap is the runtime data area, which is the memory area used by Java applications to store objects and arrays. On-heap memory refers to the memory allocated in the JVM heap, which is the main memory area used by Java applications. When Java programs create objects, these objects are usually allocated in the on-heap memory. Accordingly, the pending tasks added to the task waiting queue will occupy the remaining memory in the heap.

[0072] Exemplarily, for determining the remaining memory in the heap of the Java virtual machine, the Runtime class can be used. The Runtime class provides a total Memory() method and a freeMemory() method, which can be used to obtain the total heap memory and the remaining memory in the heap of the JVM respectively. Specifically, the method for obtaining the remaining memory in the heap can be selected by the technician, and will not be repeated here. In the case where the remaining memory in the heap is greater than the preset memory limit threshold, the actual queue length of the task waiting queue is determined again. For example, the size() method provided by the AbstractQueue class can be used to return the number of elements in the queue, so as to determine the actual queue length according to the number of elements returned. On the contrary, if the remaining memory in the heap is equal to the preset memory limit threshold or greater than the preset memory limit threshold, the pending task can be refused to enter the task waiting queue, and feedback information of refusing to enter the queue can also be sent to the sender of the pending task. After determining the actual queue length of the task waiting queue, if the actual length is less than the maximum queue length of the task waiting queue, the pending task can be added to the task waiting queue. Similarly, the maximum queue length of the task waiting queue can also be set by relevant technicians when creating a thread pool, so it will not be repeated here. On the contrary, if the actual length is equal to or greater than the maximum length of the queue, the to-be-processed task may also be refused to enter the task waiting queue.

[0073] It should be noted that in this embodiment, before adding the pending task to the task waiting queue, the remaining memory in the heap of the virtual machine where the thread pool is located will also be determined to avoid the virtual machine having an OOM error (Out Of Memory, which means that the JVM cannot allocate memory for the object for some reason, resulting in insufficient memory and inability to continue executing the program) due to the remaining memory in the heap of the virtual machine after the pending task is added to the task waiting queue. As a result, the maximum queue length of the task waiting queue can be set according to business needs without considering the problem of OOM error in the virtual machine.

[0074] In a feasible implementation manner, the thread pool control method further includes:

[0075] Step S201: If there is a core thread in the thread pool that has changed from an active state to an idle state, a to-be-processed task in the task waiting queue is allocated to the core thread that has changed from an active state to an idle state.

[0076] Exemplarily, for any core thread in the thread pool that is in an active state, after it completes the task, it will change from the active state to the idle state. For such core thread, the thread pool will assign it a pending task in the task waiting queue.

[0077] Reference Figure 3 , is a schematic diagram of the overall process of this application. As shown in the figure, after receiving the pending task, the thread pool will determine whether there is an idle core thread. If not (ie, no), it will determine whether the running thread has reached the maximum number of threads (ie, determine whether the total number of core threads has reached the upper limit of the core threads). If not, a new core thread will be created. If so, it will determine whether the task waiting queue is full. If not, the pending task will be added to the task waiting queue. If so, the corresponding rejection strategy will be triggered. It is worth noting that before the rejection strategy is actually executed, you can retry once, that is, submit the pending task again.

[0078] Reference Figure 4 , is a flow chart of the third embodiment of the present application proposed based on the first embodiment and the second embodiment. In this embodiment, the same or similar parts as the above embodiments can refer to the above content and will not be repeated here. The thread pool is provided with thread pool configuration parameters, and the control method of the thread pool also includes steps A10 to A20:

[0079] Step A10, monitoring the thread pool configuration parameters;

[0080] Step A20, after the thread pool configuration parameters are updated, update the configuration based on the updated thread pool configuration parameters, wherein the configuration includes at least one of a preset upper limit on the number of threads of the thread pool, a survival time of core threads in an idle state in the thread pool, a type of task waiting queue of the thread pool, a maximum queue length of the task waiting queue, a rejection strategy for pending tasks, and an alarm strategy of the thread pool.

[0081] Exemplarily, in actual applications, the thread pool and the related configuration server can establish a long-term communication request, and the configuration server can output the parameter configuration interface of the thread pool to the user. On the parameter configuration interface, the user can set specific configuration parameters according to their own needs. The thread pool can periodically send requests to the configuration server to monitor whether there are updates to the thread pool configuration parameters. After confirming that the configuration parameters have been updated, the thread pool can update its own configuration based on the updated configuration parameters. Among them, the configuration may include at least one of the preset upper limit of the number of threads in the thread pool, the survival time of the core threads in the idle state in the thread pool, the type of the task waiting queue of the thread pool, the maximum queue length of the task waiting queue, the rejection strategy of the pending tasks, and the alarm strategy of the thread pool. Among them, the survival time of the core thread in the idle state refers to the time the core thread is retained after entering the idle state. When a core thread in the idle state reaches the survival time, it will be destroyed; the rejection strategy refers to the strategy of refusing pending tasks to enter the task waiting queue. For example, if the remaining memory in the heap is equal to the preset memory limit threshold or greater than the preset memory limit threshold, the pending task can be refused to enter the task waiting queue; the alarm strategy refers to the strategy that the thread pool or the management center related to the thread pool sends an alarm to the relevant person in charge after the thread pool operation exceeds the threshold (the number of threads, queue length, etc. exceed the threshold). In addition, the configuration of the thread pool can also include thread pool type and custom variable length queues. Among them, the thread pool type can be divided into CPU type thread pool and IO type thread pool, and the CPU type thread pool refers to the thread pool that gives priority to adding new tasks to the task queue to ensure that each core thread has sufficient CPU resources, and adds core threads after the number of tasks in the task queue reaches the queue upper limit. The IO thread pool refers to a thread pool that creates new core threads directly when it is determined that there are no idle core threads and the total number of core threads is less than the preset thread upper limit, that is, it gives priority to increasing the number of core threads. The custom variable-length queue means that the maximum length of the task waiting queue can be changed. Figure 5 , is a schematic diagram of the results of different types of thread pools facing high-concurrency tasks in this application. As shown in the figure, the horizontal axis is the number of concurrent tasks, and the vertical axis is TPS (Transactions Per Second). Obviously, in the face of high-concurrency tasks, the IO type thread pool has a higher TPS, that is, a higher concurrent task processing performance.

[0082] Reference Figure 6 , is a schematic diagram of the configuration interface in this application, as shown in the figure, including the thread pool name, the number of core threads, the maximum number of threads, the idle thread survival time, the queue type, the queue capacity (i.e., the queue upper limit), etc. The specific parameters can be set by the technicians according to the actual needs, and will not be repeated here.

[0083] Reference Figure 7 , is a flowchart of the fourth embodiment of the present application proposed based on the first embodiment, the second embodiment, and the third embodiment. In this embodiment, the same or similar parts as the above embodiments can refer to the above content and will not be repeated here. The thread pool control method also includes steps B10 to B20:

[0084] Step B10, generating a thread pool log based on the working status of the core thread and the working status of the task waiting queue configured by the thread pool;

[0085] Step B20, sending the thread pool log to a preset log topic, so that a preset consumer can obtain the thread pool log from the preset log topic and visualize the thread pool log.

[0086] Exemplarily, the thread pool will monitor the working status of each core thread, wherein the working status of the core thread may include the number of core threads in an active state, the number of core threads in an idle state, the number of core threads in the thread pool, etc.; and monitor the working status of the task waiting queue configured by the thread pool, wherein the working status of the task waiting queue may be the number of pending tasks in the current task waiting queue, the upper limit of the number of pending tasks in the task waiting queue, etc. The thread pool may generate a thread pool log based on the working status of the core thread and the working status of the task waiting queue. Then the generated thread log is output to a specified preset log topic (such as a Kafka topic), that is, a consumable message is formed in the preset log topic, and a preset consumer (such as Flink, an open source stream processing framework) subscribes to the preset log topic, thereby consuming the message under the topic, that is, obtaining the thread pool log, and then the obtained thread log is displayed visually on the front-end page or the client.

[0087] In addition, refer to Figure 8, is a functional structure diagram of the thread pool management center in this application, that is, in this embodiment, the functions in the above embodiments can be implemented through the thread pool management center. As shown in the figure, the thread pool management center will provide four functions such as dynamic configuration update, thread pool service, Zhaohu alarm, and thread pool monitoring. Among them, the configuration part includes creating thread pool configuration, deleting thread pool configuration, modifying thread pool configuration, alarm strategy configuration, and monitoring strategy configuration; the alarm part may include thread activity alarm, queue capacity alarm, etc., and complete the alarm by calling the robot; the service part includes configuration parsing, creating thread pool, deleting thread pool, modifying thread pool, variable length queue, IO intensive thread pool (that is, after receiving the pending task, give priority to creating a thread pool of new core threads), notification alarm, and monitoring log; the monitoring part includes the number of active threads, the current length of the queue, etc., and displays relevant data through the monitoring platform.

[0088] The present application also provides a thread pool control device, which is applied to a thread pool, wherein the thread pool is configured with core threads, referring to Fig. 9 , the control device of the thread pool includes:

[0089] The determination module 10 is used to determine whether there is a core thread in an idle state after receiving the task to be processed;

[0090] A creation module 20, configured to create a new core thread when there is no core thread in an idle state and the total number of core threads is less than a preset upper limit of the number of threads;

[0091] The allocation module 30 is used to allocate the to-be-processed tasks to the new core thread.

[0092] Optionally, the thread pool is further configured with a task waiting queue, and the allocation module 30 is further used for:

[0093] When there is no core thread in the idle state and the total number of the core threads is equal to or greater than a preset thread upper limit, the to-be-processed task is added to the task waiting queue.

[0094] Optionally, the allocation module 30 is further used for:

[0095] Determine the remaining memory in the heap of the virtual machine where the thread pool is located, and when the remaining memory in the heap is greater than a preset memory limit threshold, determine the actual queue length of the task waiting queue;

[0096] When the actual queue length is less than the maximum queue length of the task waiting queue, the to-be-processed task is added to the task waiting queue.

[0097] Optionally, the allocation module 30 is further used for:

[0098] If there is a core thread in the thread pool that changes from an active state to an idle state, the core thread that changes from an active state to an idle state is assigned a to-be-processed task in the task waiting queue.

[0099] Optionally, the allocation module 30 is further used for:

[0100] In the case that the core thread in the idle state exists, the to-be-processed task is allocated to the core thread in the idle state.

[0101] Optionally, the thread pool is provided with thread pool configuration parameters, and the control device of the thread pool further includes an updating module 40, and the updating module 40 is used to:

[0102] Monitoring the thread pool configuration parameters;

[0103] After the thread pool configuration parameters are updated, the configuration is updated based on the updated thread pool configuration parameters, wherein the configuration includes at least one of a preset upper limit on the number of threads of the thread pool, a survival time of core threads in an idle state in the thread pool, a type of task waiting queue of the thread pool, a maximum queue length of the task waiting queue, a rejection strategy for pending tasks, and an alarm strategy of the thread pool.

[0104] Optionally, the control device of the thread pool further includes a visualization module 50:

[0105] Generate a thread pool log based on the working status of the core thread and the working status of the task waiting queue configured by the thread pool;

[0106] The thread pool log is sent to a preset log topic, so that a preset consumer can obtain the thread pool log from the preset log topic and visualize the thread pool log.

[0107] The control device of the thread pool provided in the present application adopts the control method of the thread pool in the above embodiment, which can solve the technical problem that the performance of the native thread pool is poor when completing tasks and it is difficult to meet the task processing requirements of high-concurrency task scenarios. Compared with the prior art, the beneficial effects of the control device of the thread pool provided in the present application are the same as the beneficial effects of the control method of the thread pool provided in the above embodiment, and other technical features in the control device of the thread pool are the same as the features disclosed in the above embodiment method, which will not be repeated here.

[0108] The present application provides an electronic device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the thread pool control method in the above-mentioned embodiment one.

[0109] Reference below Fig.10 , which shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiments of the present application. The electronic devices in the embodiments of the present application may include but are not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Displays), PMPs (Portable Multimedia Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Fig.10 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.

[0110] like Fig.10As shown, the electronic device may include a processing device 1001 (e.g., a central processing unit, a graphics processor, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read On ly Memory) 1002 or a program loaded from a storage device 1003 to a random access memory (RAM: Random Access Memory) 1004. In RAM1004, various programs and data required for the operation of the electronic device are also stored. The processing device 1001, ROM1002, and RAM1004 are connected to each other via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems may be connected to the I / O interface 1006: input devices 1007 including, for example, a touch screen, a touchpad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; output devices 1008 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; storage devices 1003 including, for example, a magnetic tape, a hard disk, etc.; and communication devices 1009. The communication device 1009 may allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. Although the figure shows an electronic device with various systems, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems may be implemented or have instead.

[0111] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.

[0112] The electronic device provided by the present application adopts the control method of the thread pool in the above embodiment, which can solve the technical problem that the performance of the native thread pool is poor when completing tasks and it is difficult to meet the task processing requirements of high-concurrency task scenarios. Compared with the prior art, the beneficial effects of the electronic device provided by the present application are the same as the beneficial effects of the control method of the thread pool provided in the above embodiment, and the other technical features in the electronic device are the same as the features disclosed in the method of the previous embodiment, which will not be repeated here.

[0113] It should be understood that the various parts disclosed in this application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in any one or more embodiments or examples in a suitable manner.

[0114] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

[0115] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the thread pool control method in the above-mentioned embodiment.

[0116] The computer-readable storage medium provided in the present application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, systems or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM: Random Access Memory), a read-only memory (ROM: Read On ly Memory), an erasable programmable read-only memory (EPROM: Erasable Programmab le Read On ly Memory or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM: CD-Read On ly Memory), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by or in combination with an instruction execution system, system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency: Radio Frequency), etc., or any suitable combination of the above.

[0117] The computer-readable storage medium may be included in the electronic device, or may exist independently without being installed in the electronic device.

[0118] The computer-readable storage medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device controls the thread pool.

[0119] The computer program code for performing the operation of the present application can be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as an independent software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computer (for example, using an Internet service provider to connect through the Internet).

[0120] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0121] The modules involved in the embodiments described in this application may be implemented by software or hardware, wherein the name of the module does not constitute a limitation on the unit itself in some cases.

[0122] The readable storage medium provided by the present application is a computer-readable storage medium, which stores computer-readable program instructions (i.e., computer programs) for executing the control method of the above-mentioned thread pool, and can solve the technical problem that the performance of the native thread pool is poor when completing tasks and it is difficult to meet the task processing requirements of high-concurrency task scenarios. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided by the present application are the same as the beneficial effects of the control method of the thread pool provided in the above-mentioned embodiment, and will not be repeated here.

[0123] The present application also provides a computer program product, including a computer program, which implements the steps of the thread pool control method as described above when executed by a processor.

[0124] The computer program product provided by this application can solve the technical problem of thread pool control. Compared with the prior art, the beneficial effects of the computer program product provided by this application are the same as the beneficial effects of the thread pool control method provided by the above embodiment, which will not be repeated here.

[0125] The above descriptions are only some embodiments of the present application, and are not intended to limit the patent scope of the present application. All equivalent structural changes made using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect applications in other related technical fields are included in the patent protection scope of the present application.

Claims

1. A thread pool control method, characterized in that: Applied to a thread pool, the thread pool is configured with core threads, and the control method of the thread pool includes: After receiving the task to be processed, determining whether there is a core thread in an idle state; If there is no core thread in the idle state and the total number of core threads is less than a preset upper limit of the number of threads, create a new core thread; The to-be-processed tasks are allocated to the new core thread.

2. The thread pool control method according to claim 1, characterized in that: The thread pool is also configured with a task waiting queue. After the step of determining whether there is a core thread in an idle state, the method further includes: When there is no core thread in the idle state and the total number of the core threads is equal to or greater than a preset thread upper limit, the to-be-processed task is added to the task waiting queue.

3. The thread pool control method according to claim 2, characterized in that: The step of adding the pending task to the task waiting queue comprises: Determine the remaining memory in the heap of the virtual machine where the thread pool is located, and when the remaining memory in the heap is greater than a preset memory limit threshold, determine the actual queue length of the task waiting queue; When the actual queue length is less than the maximum queue length of the task waiting queue, the to-be-processed task is added to the task waiting queue.

4. The thread pool control method according to claim 2, characterized in that: The thread pool control method also includes: If there is a core thread that changes from an active state to an idle state in the thread pool, the core thread that changes from an active state to an idle state is assigned a to-be-processed task in the task waiting queue.

5. The thread pool control method according to claim 1, characterized in that: After the step of determining whether there is a core thread in an idle state, the method further includes: In the case that the core thread in the idle state exists, the to-be-processed task is allocated to the core thread in the idle state.

6. The thread pool control method according to claim 1, characterized in that: The thread pool is provided with thread pool configuration parameters, and the control method of the thread pool further includes: Monitoring the thread pool configuration parameters; After the thread pool configuration parameters are updated, the configuration is updated based on the updated thread pool configuration parameters, wherein the configuration includes at least one of a preset upper limit on the number of threads of the thread pool, a survival time of core threads in an idle state in the thread pool, a type of task waiting queue of the thread pool, a maximum queue length of the task waiting queue, a rejection strategy for pending tasks, and an alarm strategy of the thread pool.

7. The thread pool control method according to claim 1, characterized in that: The thread pool control method also includes: Generate a thread pool log based on the working status of the core thread and the working status of the task waiting queue configured by the thread pool; The thread pool log is sent to a preset log topic, so that a preset consumer can obtain the thread pool log from the preset log topic and visualize the thread pool log.

8. A thread pool control device, characterized in that: Applied to a thread pool, the thread pool is configured with core threads, and the control device of the thread pool includes: A determination module, used to determine whether there is a core thread in an idle state after receiving the task to be processed; A creation module, used for creating a new core thread when there is no core thread in the idle state and the total number of core threads is less than a preset upper limit of the number of threads; The allocation module is used to allocate the to-be-processed tasks to the new core thread.

9. An electronic device, characterized in that: The device comprises: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the thread pool control method according to any one of claims 1 to 7.

10. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the thread pool control method according to any one of claims 1 to 7 are implemented.