CN106325977B - Method and system for executing task based on thread pool - Google Patents

Method and system for executing task based on thread pool Download PDF

Info

Publication number
CN106325977B
CN106325977B CN201510342900.9A CN201510342900A CN106325977B CN 106325977 B CN106325977 B CN 106325977B CN 201510342900 A CN201510342900 A CN 201510342900A CN 106325977 B CN106325977 B CN 106325977B
Authority
CN
China
Prior art keywords
thread
threads
task
pause state
value
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201510342900.9A
Other languages
Chinese (zh)
Other versions
CN106325977A (en
Inventor
李连志
陈有存
赖晨东
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information Technology Co Ltd
Original Assignee
Beijing Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Jingdong Century Trading Co Ltd, Beijing Jingdong Shangke Information Technology Co Ltd filed Critical Beijing Jingdong Century Trading Co Ltd
Priority to CN201510342900.9A priority Critical patent/CN106325977B/en
Publication of CN106325977A publication Critical patent/CN106325977A/en
Application granted granted Critical
Publication of CN106325977B publication Critical patent/CN106325977B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Landscapes

  • Debugging And Monitoring (AREA)
  • Management, Administration, Business Operations System, And Electronic Commerce (AREA)

Abstract

It is a primary object of the present invention to provide a method and system for executing tasks based on a thread pool that facilitates enabling the thread pool to suspend processing tasks and resume processing tasks after suspension. The method comprises the following steps: the management device issues a pause state identifier to each thread in the thread pool according to external operation, wherein the value of the pause state identifier represents a pause state or a non-pause state; the thread checks the pause state identifier in the thread in the state of not executing the task, suspends the thread and starts timing when the value of the pause state identifier indicates the pause state, and performs the check again after the timing reaches the preset time length until the value of the pause state identifier indicates the non-pause state; in the case where the value of the suspended state flag is indicative of an un-suspended state, the thread acquires a task from the task queue and executes the task, and the check is performed again after the execution completes the task.

Description

Method and system for executing task based on thread pool
Technical Field
The invention relates to the technical field of computers, in particular to a method and a system for executing tasks based on a thread pool.
Background
Thread pools are a form of multi-threaded processing in which tasks are added to a queue and then automatically started after a thread is created.
For the existing thread pool processing mechanism, the core thread number or the maximum thread number of the thread pool is determined when the thread pool is started and initialized, the thread can only be started and stopped, the forced stopping of the thread pool easily causes task loss or incomplete processing, and meanwhile, the number of the threads in the thread pool cannot be manually adjusted under the condition that the existing thread pool is not destroyed and re-created. The function of limiting the current and the speed is not supported, and the functions of suspending and resuming the processing after the suspension are not supported.
Disclosure of Invention
It is therefore a primary object of the present invention to provide a method and system for executing tasks based on a thread pool, which facilitates the thread pool to suspend processing tasks and resume the processing tasks after suspension. It is another object of the present invention to facilitate flexible adjustment of the number of threads within a thread pool. It is yet another object of the present invention to facilitate achieving a single line control of flow rate. Other objects of the invention can be achieved according to the embodiments.
To achieve the above object, according to one aspect of the present invention, there is provided a method of executing a task based on a thread pool.
The method for executing tasks based on the thread pool comprises the following steps: the management device issues a pause state identifier to each thread in the thread pool according to external operation, wherein the value of the pause state identifier represents a pause state or a non-pause state; the thread checks the pause state identifier in the thread in the state of not executing the task, suspends the thread and starts timing when the value of the pause state identifier indicates the pause state, and performs the check again after the timing reaches the preset time length until the value of the pause state identifier indicates the non-pause state; in the case where the value of the suspended state flag is indicative of an un-suspended state, the thread acquires a task from the task queue and executes the task, and the check is performed again after the execution completes the task.
Optionally, the method further comprises: after receiving a thread quantity set value provided by external operation, the management device judges whether the quantity of the threads in the thread pool is greater than the thread quantity set value; if so, selecting the threads and then setting the working state identifier of the selected threads as end, wherein the number of the selected threads is a difference value obtained by subtracting the thread number setting value from the number of the threads in the thread pool; if not, creating a plurality of threads and then adding the threads into a thread pool, wherein the number of the threads is a difference value obtained by subtracting the number of the threads in the thread pool from the thread number setting value; after executing the task, the thread judges whether the working state identification of the thread is finished, if so, the thread is deleted; otherwise, continuing to acquire the task from the task queue and executing the task.
Optionally, the step of selecting a thread includes: and traversing the threads in the thread pool by the management device, and taking the threads which are not traversed as the selected threads when the number of the traversed threads reaches the thread number set value.
Optionally, the method further comprises: the management device issues the sleep duration to the threads in the thread pool according to external operation; after the executing completes the task and before the determining whether the working state identifier of the thread is finished, the method further includes: and the thread sleeps according to the sleeping time length.
According to another aspect of the present invention, a system for performing tasks based on a thread pool is provided.
The system for executing tasks based on the thread pools comprises a task queue, a management device and a system which comprises a plurality of thread pools, wherein: the task queue is used for storing a plurality of tasks; the management device is used for issuing a pause state identifier to each thread in the thread pool according to external operation, and the value of the pause state identifier represents a pause state or a non-pause state; the thread is used for checking the pause state identifier in the thread in the state of not executing the task, suspending the thread and starting timing under the condition that the value of the pause state identifier represents the pause state, and performing the check again after the timing reaches the preset time length until the value of the pause state identifier represents the non-pause state; in the case where the value of the suspended state flag is indicative of an un-suspended state, the thread acquires a task from the task queue and executes the task, and the check is performed again after the execution completes the task.
Optionally, the management apparatus is further configured to: after receiving a thread quantity set value provided by external operation, judging whether the quantity of threads in the thread pool is greater than the thread quantity set value; if so, selecting the threads and then setting the working state identifier of the selected threads as end, wherein the number of the selected threads is a difference value obtained by subtracting the thread number setting value from the number of the threads in the thread pool; if not, creating a plurality of threads and then adding the threads into a thread pool, wherein the number of the threads is a difference value obtained by subtracting the number of the threads in the thread pool from the thread number setting value; the thread is further to: after executing the completed task, judging whether the working state identification of the thread is finished, if so, deleting the thread; otherwise, continuing to acquire the task from the task queue and executing the task.
Optionally, the management device is further configured to traverse the threads in the thread pool, and when the number of traversed threads reaches the thread number set value, take the threads that are not traversed as the selected threads.
Optionally, the management device is further configured to issue the sleep duration to the thread in the thread pool according to an external operation; and the thread is also used for sleeping according to the sleeping time length after the execution completes the task and before the judgment of whether the working state mark of the thread is finished is carried out.
To achieve the above object, according to still another aspect of embodiments of the present invention, there is provided an electronic apparatus including: one or more processors; the storage device is used for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the method for executing tasks based on the thread pool provided by the embodiment of the invention.
To achieve the above object, according to still another aspect of the embodiments of the present invention, there is provided a computer readable medium having a computer program stored thereon, where the computer program is used to implement a method for executing tasks based on a thread pool provided by the embodiments of the present invention when the computer program is executed by a processor.
According to the technical scheme of the invention, the suspended state identification is uniformly issued to the threads in the thread pool to control the threads to be in the suspended or non-suspended state, so that the thread pool can suspend the processing task and resume the processing task after suspension; the number of threads in the thread pool is set, and then the working state identifier is set to be the end of the designated thread, or the threads are added to the thread pool, so that the number of the threads in the thread pool can be reduced or increased; the dormancy duration is uniformly set for the threads, and the single thread control flow rate can be realized.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
FIG. 1 is a diagram illustrating the basic architecture of a system for performing tasks based on thread pools, according to an embodiment of the present invention;
FIG. 2 is a diagram illustrating a flow of a method for performing tasks based on thread pools according to an embodiment of the present invention.
Detailed Description
Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered exemplary only. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
Fig. 1 is a diagram illustrating a basic structure of a system for executing tasks based on a thread pool according to an embodiment of the present invention. As shown in fig. 1, a system 10 for executing tasks based on thread pools mainly includes a task queue 11, a thread pool 12, and a management device 13. There are multiple threads in the thread pool 12, i.e., thread 1, thread 2, … …, and thread N shown in the figure. The system 1 is available for use by an external application system 20. When the system 1 is initialized, an operator sets the initial size of the thread pool and the interval time of the execution of the tasks in the threads, and establishes an example of the system 1, wherein the example comprises an initialized task queue, the thread pool and a management device. After the initialization is completed, the external application system 2 acquires a batch of tasks, then adds the tasks into the task queue 11 of the system 1, and the thread pool 12 acquires the tasks in the queue to start execution.
In the process of running the thread pool, the management device 13 records the working states of all threads, records the number of tasks executed in the current batch, calculates the number of tasks completed in unit time, calculates the TPS, and grasps the execution speed of the tasks in the current batch or in the latest period of time. These data are stored in the state values of the management device 13, and the external application system 2 can directly obtain the relevant data from the management device 13 so as to adjust the working state of the system 1.
The task acquisition and addition support two ways: one mode is that the tasks to be executed in the current batch are added into the task queue 11 at one time, and the next batch of tasks are added after the tasks are executed; another way is to continuously add tasks into the task queue 11, the task queue 11 has a maximum limit of the queue, if the number of unconsumed tasks in the queue reaches the maximum upper limit, the external part is refused to continuously add new tasks into the queue, and if the number of consumed tasks in the queue is less than the maximum number, the new tasks are allowed to be continuously added into the queue. The consumption of the queue and the addition of the tasks are ready and safe, and the threads of the tasks of each consumption queue are in a blocking state when the queue is empty.
When a batch task is executed at one time, a synchronous auxiliary class can be used, and when other threads do not consume the current task, the external thread executing the task waits until all tasks are executed. Therefore, the external thread can control the number of tasks in a planned mode and evaluate the execution time of the batch tasks, and the execution speed of the tasks can be adjusted conveniently and externally. Each thread in the thread pool 12 tries to acquire a task from the task queue 11, and if no task exists in the task queue 11, each thread blocks waiting until the task is added to the task queue 11, and each thread takes the task and executes the task.
The following description is made of a scheme for realizing suspension of thread processing by a thread pool and resuming the processing after the suspension in the embodiment of the present invention. Each thread has a pause status flag and a thread start-stop flag, which are issued by the management device according to external operations of personnel and indicate a pause status or a non-pause status. After the management device issues the pause flag, each thread is started and then circularly acquires and executes the task from the task queue, that is, a task is acquired first and then executed, then a task is acquired from the task queue and then executed, and so on. Before each task is taken, the thread checks the pause state identifier to see whether the value of the pause state identifier represents the pause state, if so, the thread is suspended, the task is not obtained from the task queue to be executed, after the suspension time length reaches the preset value, the thread automatically wakes up and then continuously checks whether the value of the pause identifier is changed (because the processing is possibly resumed after the pause due to the control of the management device, at the moment, the value of the pause identifier is changed), and if so, the thread starts to obtain the task from the task queue to resume the normal operation. The suspended state is uniformly managed by the management device, the threads in all the thread pools share the suspended state, and once the suspended state is changed, when all the threads in the thread pools run to the suspended state needing to be checked, whether the threads need to be suspended or resume to continue to execute downwards is confirmed. The management device achieves the functions of controlling the suspension and the recovery of all threads in the thread pool according to the change of the value of the issued suspension state identifier. After the task is executed, before the next task is acquired and the execution is started, the suspended state needs to be checked, the suspended state is not immediately executed in the task execution process, and the suspended state and the recovery state do not exist in the task execution. Once the task is executed, it must wait until it is executed, and it cannot be interrupted, i.e. the task is an atomic unit of operation.
The following describes a technical solution for adjusting the number of threads in a thread pool in an embodiment of the present invention. And after receiving the set value, the management device can determine whether the number of threads in the thread pool needs to be reduced or increased according to the size relationship between the set value and the number of threads in the current thread pool. And if the number of the threads in the thread pool is greater than the set value, selecting some threads and setting the working state marks of the threads as ending, wherein the number of the threads is the difference value obtained by subtracting the set value from the number of the threads in the thread pool. When each thread runs a task, whether the working state identification of the thread is finished or not needs to be checked, if the working state identification of the thread is finished, the thread runs and is finished, the task is not taken from the task queue, the thread is deleted, and the method in the thread pool can be called back to specifically enable the thread pool to delete the thread. After each thread is thus processed, the number of threads within the thread is reduced to the number set by the personnel.
In the above process, the management apparatus may select and count the threads until the number of selected threads reaches the difference. The management device may also traverse the threads in the thread pool, and when the number of traversed threads reaches the thread number setting value, the threads that are not traversed are taken as the selected threads, and at this time, the threads may be traversed continuously and the working state flags of the threads are set to end during traversal.
If the thread quantity in the thread pool is smaller than the thread quantity set value, the management device creates threads with the quantity being the difference value according to the difference value between the thread quantity set value and the thread quantity in the thread pool and then adds the threads into the thread pool, and the newly added threads participate in normal work, namely are executed according to the flow.
To achieve a single thread control flow rate, the management device may issue a sleep duration to some or all of the threads in the thread pool, depending on the operation of the person. After the thread finishes the task, the thread sleeps according to the sleeping time length, and then whether the working state identification of the thread is finished or not is judged. In the case of sleep, the processing speed of the thread pool is limited, which in some application scenarios helps to relieve the downstream system.
The above-described process flow can be collectively shown in fig. 2, and fig. 2 is a schematic diagram of one process flow of a method for executing tasks based on a thread pool according to an embodiment of the present invention. As shown in fig. 2, after the thread is started, the suspend status flag is determined (step S21), and if the suspend status flag indicates suspend, the process is delayed (step S22), and then the process returns to step S21; if the task is not suspended, acquiring the task from the task queue (step S23), judging whether the operation for acquiring the task is overtime (step S24), if so, directly judging whether the working state flag of the thread is end (step S27), and if so, deleting the thread (step S28); if not, indicating that the task is successfully acquired, the thread executes the task (step S25). After the task execution is completed, the sleep is performed for the sleep duration (step S26), and then the process proceeds to step S27. Here, it is taken as an example that the management apparatus needs to sleep, and if the management apparatus does not issue the sleep duration, the process directly proceeds to step S27 after the task is completed. If the judgment in the step S27 shows that the operation state flag is not over, the process returns to the step S21.
According to the embodiment of the invention, the suspended state identification is uniformly issued to the threads in the thread pool to control the threads to be in the suspended or non-suspended state, so that the thread pool can suspend the processing task and resume the processing task after suspension; the number of threads in the thread pool is set, and then the working state identifier is set to be the end of the designated thread, or the threads are added to the thread pool, so that the number of the threads in the thread pool can be reduced or increased; the dormancy duration is uniformly set for the threads, and the single thread control flow rate can be realized.
The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (10)

1. A method for performing tasks based on a thread pool, comprising:
the management device issues a pause state identifier to each thread in the thread pool according to external operation, wherein the value of the pause state identifier represents a pause state or a non-pause state;
the thread checks the pause state identifier in the thread in the state of not executing the task, suspends the thread and starts timing when the value of the pause state identifier indicates the pause state, and performs the check again after the timing reaches the preset time length until the value of the pause state identifier indicates the non-pause state;
in the case where the value of the suspended state flag is indicative of an un-suspended state, the thread acquires a task from the task queue and executes the task, and the check is performed again after the execution completes the task.
2. The method of claim 1, further comprising:
after receiving a thread quantity set value provided by external operation, the management device judges whether the quantity of the threads in the thread pool is greater than the thread quantity set value; if so, selecting the threads and then setting the working state identifier of the selected threads as end, wherein the number of the selected threads is a difference value obtained by subtracting the thread number setting value from the number of the threads in the thread pool; if not, creating a plurality of threads and then adding the threads into a thread pool, wherein the number of the threads is a difference value obtained by subtracting the number of the threads in the thread pool from the thread number setting value;
after executing the task, the thread judges whether the working state identification of the thread is finished, if so, the thread is deleted; otherwise, continuing to acquire the task from the task queue and executing the task.
3. The method of claim 2, wherein the step of selecting a thread comprises: and traversing the threads in the thread pool by the management device, and taking the threads which are not traversed as the selected threads when the number of the traversed threads reaches the thread number set value.
4. The method according to claim 2 or 3,
the method further comprises the following steps: the management device issues the sleep duration to the threads in the thread pool according to external operation;
after the executing completes the task and before the determining whether the working state identifier of the thread is finished, the method further includes: and the thread sleeps according to the sleeping time length.
5. A system for executing tasks based on thread pools, comprising a task queue, a management device, and a plurality of thread pools, wherein:
the task queue is used for storing a plurality of tasks;
the management device is used for issuing a pause state identifier to each thread in the thread pool according to external operation, and the value of the pause state identifier represents a pause state or a non-pause state;
the thread is used for checking the pause state identifier in the thread in the state of not executing the task, suspending the thread and starting timing under the condition that the value of the pause state identifier represents the pause state, and performing the check again after the timing reaches the preset time length until the value of the pause state identifier represents the non-pause state; in the case where the value of the suspended state flag is indicative of an un-suspended state, the thread acquires a task from the task queue and executes the task, and the check is performed again after the execution completes the task.
6. The system of claim 5,
the management device is further configured to: after receiving a thread quantity set value provided by external operation, judging whether the quantity of threads in the thread pool is greater than the thread quantity set value; if so, selecting the threads and then setting the working state identifier of the selected threads as end, wherein the number of the selected threads is a difference value obtained by subtracting the thread number setting value from the number of the threads in the thread pool; if not, creating a plurality of threads and then adding the threads into a thread pool, wherein the number of the threads is a difference value obtained by subtracting the number of the threads in the thread pool from the thread number setting value;
the thread is further to: after executing the completed task, judging whether the working state identification of the thread is finished, if so, deleting the thread; otherwise, continuing to acquire the task from the task queue and executing the task.
7. The system of claim 6, wherein the management means is further configured to traverse the threads in the thread pool, and when the number of traversed threads reaches the thread number setting, select the threads that are not traversed.
8. The system of claim 6 or 7,
the management device is also used for issuing the dormancy duration to the threads in the thread pool according to external operation;
and the thread is also used for sleeping according to the sleeping time length after the execution completes the task and before the judgment of whether the working state mark of the thread is finished is carried out.
9. An electronic device, comprising:
one or more processors;
a storage device for storing one or more programs,
when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-4.
10. A computer-readable medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-4.
CN201510342900.9A 2015-06-19 2015-06-19 Method and system for executing task based on thread pool Active CN106325977B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201510342900.9A CN106325977B (en) 2015-06-19 2015-06-19 Method and system for executing task based on thread pool

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201510342900.9A CN106325977B (en) 2015-06-19 2015-06-19 Method and system for executing task based on thread pool

Publications (2)

Publication Number Publication Date
CN106325977A CN106325977A (en) 2017-01-11
CN106325977B true CN106325977B (en) 2020-05-01

Family

ID=57732671

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201510342900.9A Active CN106325977B (en) 2015-06-19 2015-06-19 Method and system for executing task based on thread pool

Country Status (1)

Country Link
CN (1) CN106325977B (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1584842A (en) * 2004-06-09 2005-02-23 中兴通讯股份有限公司 Method for applied server of computer system
CN103024699A (en) * 2011-09-22 2013-04-03 北京神州泰岳软件股份有限公司 Short message sending method and information resource station entity
CN103268247A (en) * 2013-06-05 2013-08-28 中国电子科技集团公司第十五研究所 Method and device for executing task and adjusting number of remaining threads in thread pool
CN104111877A (en) * 2014-07-29 2014-10-22 广东能龙教育股份有限公司 Thread dynamic deployment system and method based on thread deployment engine

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1584842A (en) * 2004-06-09 2005-02-23 中兴通讯股份有限公司 Method for applied server of computer system
CN103024699A (en) * 2011-09-22 2013-04-03 北京神州泰岳软件股份有限公司 Short message sending method and information resource station entity
CN103268247A (en) * 2013-06-05 2013-08-28 中国电子科技集团公司第十五研究所 Method and device for executing task and adjusting number of remaining threads in thread pool
CN104111877A (en) * 2014-07-29 2014-10-22 广东能龙教育股份有限公司 Thread dynamic deployment system and method based on thread deployment engine

Also Published As

Publication number Publication date
CN106325977A (en) 2017-01-11

Similar Documents

Publication Publication Date Title
US10592276B2 (en) Reducing resource consumption by blocking a function associated with an asynchronous procedure call
US8584138B2 (en) Direct switching of software threads by selectively bypassing run queue based on selection criteria
US9513955B2 (en) Application management method and terminal
CN101681260B (en) Arithmetic unit
CN103345415B (en) The terminal power-economizing method of management intervalometer and device
CN103268247B (en) Method and device for executing task and adjusting number of remaining threads in thread pool
US9513953B2 (en) Reducing virtual machine suspension time in checkpoint system
JP2007157118A5 (en)
JP6180450B2 (en) Control device, control method and program for control device
DE102014101633A1 (en) Interruption of tasks for the management of chip components
JP2008505389A5 (en)
JP2009093383A (en) Method and device for controlling multicore processor
US9541980B2 (en) Operation management device, operation management method, and recording medium
CN105373453B (en) Data back up method and system
JP2006195945A (en) Electronic device and peak power control method therefor
CN102799212B (en) Global clock system for multi-core multi-processor parallel system, and application method thereof
CN109918145B (en) Application cold start acceleration method and device and electronic equipment
CN106330766A (en) Message transmitting method and apparatus
WO2017193964A1 (en) Component upgrade method, apparatus and system
CN106325977B (en) Method and system for executing task based on thread pool
US20180120926A1 (en) Controlling transitions of devices between normal state and quiescent state
CN105245372A (en) Optimization method for loading network resources based on cache and asynchronous mechanism
JP6175980B2 (en) CPU control method, control program, and information processing apparatus
WO2023151498A1 (en) Message execution processing method and apparatus, electronic device, and storage medium
CN103019849A (en) Method for managing virtual machine in cloud computing environment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant