CN106325977A - Method and system for executing task on the basis of thread pool - Google Patents
Method and system for executing task on the basis of thread pool Download PDFInfo
- Publication number
- CN106325977A CN106325977A CN201510342900.9A CN201510342900A CN106325977A CN 106325977 A CN106325977 A CN 106325977A CN 201510342900 A CN201510342900 A CN 201510342900A CN 106325977 A CN106325977 A CN 106325977A
- Authority
- CN
- China
- Prior art keywords
- thread
- task
- halted state
- value
- pool
- 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.)
- Granted
Links
Landscapes
- Debugging And Monitoring (AREA)
- Management, Administration, Business Operations System, And Electronic Commerce (AREA)
Abstract
The invention aims to provide a method and a system for executing a task on the basis of a thread pool, and is favorable for causing the thread pool to suspend task processing and recover the task processing after suspension. The method comprises the following steps of: according to an external operation, a management device releases suspended state identification to each thread in the thread pool, the value of the suspended state identification expresses a suspended state or a non-suspended state; under a state that the thread does not execute the task, the suspended state identification in the thread is checked, wherein the value of the suspended state identification expresses that the thread is suspended and timing is started under the situation of the suspended state, and checking is carried out again when timing achieves preset duration until the value of the suspended state identification expresses the non-suspended state; and under the situation that the value of the suspended state identification expresses the non-suspended state, the thread obtains the task from a task queue and executes the task, and the checking is carried out again after the task finishes being executed.
Description
Technical field
The present invention relates to field of computer technology, particularly a kind of based on thread pool execution times
The method and system of business.
Background technology
Thread pool is a kind of multiple threads form, adds task to queue in processing procedure,
Then after creating thread, these tasks are automatically started.
For existing thread pool treatment mechanism, start and determined that thread pool when initializing
Core-wire number of passes or maximum thread, thread can only start and stop, and compulsory stopping thread
Pond easily causes task and loses, or processes imperfect, and the most existing thread pool is not destroying weight
Cannot the quantity of thread in manual adjustment thread pool in the case of newly created.The most do not support that current limliting limits
The function of speed, does not support the function suspended and recovery processes after time-out.
Summary of the invention
In view of this, the main object of the present invention is to provide a kind of based on thread pool execution task
Method and system, contributes to enabling thread pool to suspend process task and at recovery after time-out
Reason task.It is another object of the present invention to contribute to number of threads in flexible thread pool.This
A further object of invention contributes to reach single-threaded coutroi velocity.Other objects of the present invention can
To draw according to detailed description of the invention.
For achieving the above object, according to an aspect of the invention, it is provided one is based on thread
Pond performs the method for task.
The method based on thread pool execution task of the present invention includes: managing device is grasped according to outside
Make, each the thread issues halted state mark in thread pool, this halted state mark
Value represents halted state or non-halted state;Described thread checks when being not carried out task
Described halted state mark in this thread, the value identified in this halted state is to represent time-out shape
Hang up this thread in the case of state and start timing, again carrying out after timing reaches preset duration
Described inspection, until the value of this halted state mark is to represent non-halted state;At this time-out shape
State mark value be represent non-halted state in the case of, described thread from task queue obtain appoint
It is engaged in and performs this task, and after having performed this task, again carrying out described inspection.
Alternatively, also include: managing device is receiving the number of threads setting that peripheral operation provides
After value, it is judged that whether the quantity of described thread pool thread arranges value more than described number of threads;
The most then the duty of the thread that then selection thread arranges selection is designated end, selected
The quantity of the thread selected is that the quantity of described thread pool thread deducts described number of threads and arranges value
The difference obtained;If it is not, then create multiple thread to be subsequently adding thread pool, the plurality of thread
Quantity be that described number of threads arranges value and deducts the difference that the quantity of described thread pool thread obtains
Value;Described thread judges that after having performed task the duty of this thread identifies whether as knot
Bundle, the most then delete this thread;Otherwise continue obtain task from described task queue and perform
This task.
Alternatively, the step of described selection thread includes: the line in managing device traversal thread pool
Journey, the quantity of thread in traversal reaches described number of threads when arranging value, the line that will do not travel through
The thread that Cheng Zuowei selects.
Alternatively, the method also includes: managing device is according to peripheral operation, in thread pool
Thread issues sleep time;After described execution completes this task, and sentence described in carrying out
Before the duty of this thread disconnected identifies whether as terminating, the method also includes: thread according to
Described sleep time carries out dormancy.
According to a further aspect in the invention, it is provided that a kind of system performing task based on thread pool.
The present invention based on thread pool perform task system include task queue, managing device,
And comprising multiple thread pool, wherein: task queue is used for preserving multiple task;Management dress
Putting for according to peripheral operation, each the thread issues halted state in thread pool identifies,
The value of this halted state mark represents halted state or non-halted state;Thread is for being not carried out
Check the described halted state mark in this thread under the state of task, identify in this halted state
Value be represent halted state in the case of hang up this thread and start timing, reach pre-in timing
If again carrying out described inspection after duration, until the value of this halted state mark is to represent non-time-out
State;In the case of the value identified in this halted state is to represent non-halted state, described thread
Obtain task from task queue and perform this task, and again entering after having performed this task
The described inspection of row.
Alternatively, described managing device is additionally operable to: receiving the number of threads that peripheral operation provides
After value is set, it is judged that whether the quantity of described thread pool thread is arranged more than described number of threads
Value;The most then the duty of the thread that then selection thread arranges selection is designated end,
The quantity of selected thread is that the quantity of described thread pool thread deducts described number of threads and sets
Put the difference being worth to;If it is not, then create multiple thread to be subsequently adding thread pool, the plurality of
The quantity of thread is that described number of threads arranges value and deducts the quantity of described thread pool thread and obtain
Difference;Described thread is additionally operable to: judge the duty of this thread after having performed task
Identify whether for terminating, the most then to delete this thread;Otherwise continue to obtain from described task queue
Take task and perform this task.
Alternatively, described managing device is additionally operable to travel through the thread in thread pool, at the line of traversal
The quantity of journey reaches described number of threads when arranging value, using the thread that do not travels through as the line selected
Journey.
Alternatively, described managing device is additionally operable to according to peripheral operation, the thread in thread pool
Issue sleep time;Described thread is additionally operable to after described execution completes this task, and
Carry out the described duty judging this thread identify whether into terminate before, according to described dormancy
Duration carries out dormancy.
According to technical scheme, unified thread issues halted state mark in thread pool
Know to control thread for suspending or non-halted state, so that thread pool can suspend process task
And recovery processes task after time-out;Set number of threads in thread pool, then by appointment
Thread duty be set be designated end, or add thread to thread pool, it is possible to achieve
The number of threads being reduced or increased in thread pool;To thread is unified, sleep time, Ke Yishi are set
Existing single-threaded coutroi velocity.
Accompanying drawing explanation
Accompanying drawing is used for being more fully understood that the present invention, does not constitute inappropriate limitation of the present invention.Wherein:
Fig. 1 is the basic knot of the system of the task that performs based on thread pool according to embodiments of the present invention
The schematic diagram of structure;
Fig. 2 is the one of the method for the task that performs based on thread pool according to embodiment of the present invention
The schematic diagram of flow process.
Detailed description of the invention
Below in conjunction with accompanying drawing, the exemplary embodiment of the present invention is explained, including this
The various details of invention embodiment are to help understanding, it should they are thought the most exemplary
's.Therefore, those of ordinary skill in the art are it should be appreciated that can be to enforcement described herein
Mode makes various changes and modifications, without departing from scope and spirit of the present invention.Equally,
For clarity and conciseness, description below eliminates the description to known function and structure.
Fig. 1 is the basic knot of the system of the task that performs based on thread pool according to embodiments of the present invention
The schematic diagram of structure.As it is shown in figure 1, system 10 based on thread pool execution task mainly includes appointing
Business queue 11, thread pool 12 and managing device 13.Its thread pool 12 there is multiple line
Journey, i.e. thread 1 shown in figure, thread 2 ..., thread N.This system 1 is available for outside
Application system 20 uses.System 1, when initializing, is arranged thread pool by operator the biggest
The interval time that little and task performs in thread, set up the example of said system 1, wherein
Contain initialized task queue, thread pool and managing device.After initialization completes,
Outer application system 2 obtains a collection of task, is subsequently adding in the task queue 11 of system 1, line
Cheng Chi 12 obtains the task in queue to start to perform.
During thread pool runs, managing device 13 records all thread work states, note
Record the number of tasks that this batch performs, the number of tasks completed in the unit of account time, calculate TPS,
Grasp this batch or the execution speed of nearest a period of time task.These data are in managing device
Preserving in the state value of 13, outer application system 2 can directly get from managing device 13
Relevant data, in order to reference to the duty how adjusting system 1.
Two ways is supported in the acquisition of task and interpolation: a kind of mode is disposable to task queue
Add the task that this batch is to be performed in 11, treat tasks carrying complete interpolation next batch task;Separately
A kind of mode is lasting addition task in task queue 11, and task queue 11 has a team
The maximum restriction of row, if task of the not consuming quantity in queue reaches maximum upper limit, outside refusing
Portion continues to add new task in queue, if queue consumption task just allows less than maximum quantity
Continue to add new task in queue.The consumption of queue and the interpolation of task are all ready-made safe,
The thread of queue task is respectively consumed all in blocked state when queue is sky.
When disposable execution batch tasks performs, one can be used to synchronize assisted class, at other
When thread has not consumed current task, the outside thread performing task waits until always
All tasks have been carried out.So outside thread can with planned control task quantity, and
The execution time of assessment batch task, it is simple to outside is adjusted the speed of the execution of task.Line
In Cheng Chi 12, each thread attempts to obtain task from task queue 11, if task queue
The most each thread block of task is not had to wait until interpolation task in task queue 11, respectively in 11
Thread performs after taking task.
Below in embodiment of the present invention realize thread pool process thread time-out and
The scheme that after time-out, recovery processes is explained.A thread is had to perform in each thread
Halted state mark starts, with thread, the mark stopped, and this identifies by managing device according to personnel's
Peripheral operation is issued, and its value represents halted state or non-halted state.When managing device is issued temporarily
After stopping mark, each thread is the task that obtains from task queue of circulation upon actuation and holds
OK, the most first get a task, then perform, from task queue, take one the most again appoint
Business performs the most again, and the rest may be inferred.Before often taking a task, thread checks halted state
Mark, sees that its value indicates whether halted state, if then thread suspension, no longer from task queue
Middle acquisition tasks carrying, after suspension time length arrives setting value in advance, thread automatically wakens up
Then proceed to check whether suspend the value of mark changes and (because due to the control of managing device, have
May after time-out again recovery process, now suspend mark value will change), if changed; would open
Begin to obtain task from task queue, restart properly functioning.Halted state is by managing device
Unified management, the thread in all thread pools is shared and is used this halted state, once halted state
Changing, in thread pool, all threads run to need all to confirm certainly the when of verifying halted state
Oneself is to need to suspend or recover to continue down to perform.Managing device is according to the halted state issued
The change of the value of mark reaches to control all threads time-out, the function of recovery in thread pool.?
Before subtask execution start after tasks carrying is complete, under acquisition, need to verify halted state, and
Not being to be immediately performed during tasks carrying, there is not time-out and recovers shape in tasks carrying itself
State.Task once performs by the time to have performed, it is impossible to be interrupted, say, that task is one
The operating unit of individual atom.
Below for the technical side of the quantity of thread in the regulation thread pool in embodiment of the present invention
Case is illustrated.In the case of the quantity of the thread in needing regulation thread pool, applications
Calling the interface of managing device, the number of threads providing personnel to arrange to managing device arranges value,
After managing device receives this value of setting, according to this value of setting and the quantity of thread in current thread pond
Between magnitude relationship, it is possible to determine needs reduce also be to increase number of threads in thread pool.
If the number of threads in thread pool arranges value more than above-mentioned, then select some threads and arrange
The duty of these threads is designated end, and the quantity of these threads is thread in thread pool
Quantity deduct the difference that above-mentioned setting is worth to.Need the when that each thread operation task completing
The duty verifying this thread identifies whether as terminating, if terminating then thread fortune itself
Row terminates, and no longer takes task in task queue, and deletes this thread, specifically can adjust back line
Method in Cheng Chi, makes thread pool be deleted by this thread.Each thread after such process,
Number of threads in thread just drops to the quantity that personnel are arranged.
In above procedure, managing device, when selecting thread, can select to count,
Until the number of threads selected reaches above-mentioned difference.Managing device can also travel through in thread pool
Thread, the quantity of thread in traversal reaches above-mentioned number of threads when arranging value, will not travel through
Thread as the thread selected, now can continue to travel through these threads and arrange when traversal
The duty of these threads is designated end.
If the number of threads in thread pool arranges value less than number of threads, then managing device according to
Number of threads arranges the difference of value and the number of threads in thread pool, and creating number is this difference
Thread is subsequently adding thread pool, and the thread being newly added participates in normal work, i.e. by aforesaid stream
Cheng Zhihang.
In order to reach single-threaded coutroi velocity, managing device, can be to line according to the operation of personnel
Some or all thread issues sleep times in Cheng Chi.After thread completes task, first root
Carry out dormancy according to sleep time, judge that the duty of this thread identifies whether as terminating the most again.
In the case of carrying out dormancy, the processing speed of thread pool is limited, in application scenes
Contribute to alleviating the pressure of down-stream system.
Flow process described above can be unified shown in Figure 2, and Fig. 2 is according to embodiment of the present invention
The schematic diagram of a kind of flow process of method of the task that performs based on thread pool.As in figure 2 it is shown,
After thread starts, first judge that halted state identifies (step S21), if representing and suspending, then time delay
Step S21 is returned after (step S22);If representing non-time-out, then obtain from task queue and appoint
Business (step S23), can be the most overtime (step S24) for obtaining the operation judges of task,
The most directly judge the duty of thread identify whether as terminating (step S27) and if
It is then to delete this thread (step S28);If having not timed out, illustrate that Mission Success obtains, now
Thread execution task (step S25).After tasks carrying completes, carry out dormancy by sleep time
(step S26), subsequently into step S27.Here it is as a example by needs dormancy, such as management dress
Put and do not issue sleep time, be then directly entered step S27 after tasks carrying completes.If step
The judged result of S27 be duty mark be not terminate, then return step S21.
According to embodiment of the present invention, unified thread issues halted state mark in thread pool
With control thread for suspend or non-halted state so that thread pool can suspend process task with
And recovery processes task after time-out;Set number of threads in thread pool, then by specifying
Thread arranges duty and is designated end, or adds thread to thread pool, it is possible to achieve subtract
Little or increase the number of threads in thread pool;To thread is unified, sleep time is set, it is possible to achieve
Single-threaded coutroi velocity.
Above-mentioned detailed description of the invention, is not intended that limiting the scope of the invention.This area
Technical staff, it is to be understood that depend on that design requires and other factors, can occur various
The amendment of various kinds, combination, sub-portfolio and replacement.Any within the spirit and principles in the present invention
Amendment, equivalent and the improvement etc. made, within should be included in scope.
Claims (8)
1. the method performing task based on thread pool, it is characterised in that including:
Managing device is according to peripheral operation, each the thread issues halted state in thread pool
Mark, the value of this halted state mark represents halted state or non-halted state;
Described thread checks the described halted state mark in this thread when being not carried out task
Knowing, the value identified in this halted state is hung up this thread in the case of being to represent halted state and opens
Beginning timing, carries out described inspection after timing reaches preset duration again, until this halted state
The value of mark is to represent non-halted state;
In the case of the value identified in this halted state is to represent non-halted state, described thread from
Task queue obtains task and performs this task, and again carries out after having performed this task
Described inspection.
Method the most according to claim 1, it is characterised in that also include:
Managing device is after the number of threads receiving peripheral operation offer arranges value, it is judged that described line
Whether the quantity of Cheng Chi thread arranges value more than described number of threads;The most then select thread
The duty of the thread then arranging selection is designated end, and the quantity of selected thread is
The quantity of described thread pool thread deducts described number of threads and arranges the difference being worth to;If it is not,
Then creating multiple thread and be subsequently adding thread pool, the quantity of the plurality of thread is described Thread Count
Amount arranges value and deducts the difference that the quantity of described thread pool thread obtains;
Described thread judges that after having performed task the duty of this thread identifies whether as knot
Bundle, the most then delete this thread;Otherwise continue obtain task from described task queue and perform
This task.
Method the most according to claim 2, it is characterised in that the step of described selection thread
Suddenly include: the thread in managing device traversal thread pool, the quantity at the thread of traversal reaches institute
State number of threads time value is set, using the thread that do not travels through as the thread selected.
The most according to the method in claim 2 or 3, it is characterised in that
The method also includes: managing device is according to peripheral operation, the thread issues in thread pool
Sleep time;
After described execution completes this task, and carrying out the work of described this thread of judgement
Before whether status indicator is end, the method also includes: thread enters according to described sleep time
Row dormancy.
5. one kind based on thread pool perform task system, it is characterised in that include task queue,
Managing device and comprise multiple thread pool, wherein:
Task queue is used for preserving multiple task;
Managing device is for according to peripheral operation, and each thread issues in thread pool is suspended
Status indicator, the value of this halted state mark represents halted state or non-halted state;
The thread described halted state mark in check this thread when being not carried out task
Knowing, the value identified in this halted state is hung up this thread in the case of being to represent halted state and opens
Beginning timing, carries out described inspection after timing reaches preset duration again, until this halted state
The value of mark is to represent non-halted state;The value identified in this halted state is to represent non-time-out shape
In the case of state, described thread obtains task from task queue and performs this task, and is holding
Go and again carried out described inspection after this task.
System the most according to claim 5, it is characterised in that
Described managing device is additionally operable to: after the number of threads receiving peripheral operation offer arranges value,
Judge whether the quantity of described thread pool thread arranges value more than described number of threads;If so,
Then the duty of the thread that then selection thread arranges selection is designated end, selected line
The quantity of journey is that the quantity of described thread pool thread deducts described number of threads and arranges and be worth to
Difference;If it is not, then create multiple thread to be subsequently adding thread pool, the quantity of the plurality of thread
It is that described number of threads arranges value and deducts the difference that the quantity of described thread pool thread obtains;
Described thread is additionally operable to: judge the duty mark of this thread after having performed task
Whether it is end, the most then deletes this thread;Otherwise continue to obtain from described task queue to appoint
It is engaged in and performs this task.
System the most according to claim 6, it is characterised in that described managing device is also used
Thread in traversal thread pool, the quantity at the thread of traversal reaches described number of threads and arranges
During value, using the thread that do not travels through as the thread selected.
8. according to the system described in claim 6 or 7, it is characterised in that
Described managing device is additionally operable to according to peripheral operation, the thread issues dormancy in thread pool
Duration;
Described thread is additionally operable to after described execution completes this task, and sentences described in carrying out
Before the duty of this thread disconnected identifies whether as terminating, carry out not according to described sleep time
Sleep.
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 true CN106325977A (en) | 2017-01-11 |
CN106325977B 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)
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 |
-
2015
- 2015-06-19 CN CN201510342900.9A patent/CN106325977B/en active Active
Patent Citations (4)
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 |
---|---|
CN106325977B (en) | 2020-05-01 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
TWI742045B (en) | Task resource scheduling method and device | |
CN103677999B (en) | The management of resource in computing environment | |
CN104331327B (en) | The optimization method of task scheduling and optimization system in large scale virtualisation environment | |
CN106293919B (en) | A kind of the built-in tasks dispatching device and method of time trigger | |
CN103873321B (en) | Distributed file system-based simulation distributed parallel computing platform and method | |
CN102662763B (en) | Virtual machine resource scheduling method based on service quality | |
WO2017080273A1 (en) | Task management methods and system, and computer storage medium | |
CN109684060A (en) | A kind of mixed scheduling method of polymorphic type time-critical task | |
CN104111877A (en) | Thread dynamic deployment system and method based on thread deployment engine | |
CN103838621B (en) | Method and system for scheduling routine work and scheduling nodes | |
CN104915247B (en) | A kind of real time data calculation method and system | |
CN104820616B (en) | A kind of method and device of task scheduling | |
CN103617098B (en) | Intelligent backup method and intelligent backup system based on data variation | |
CN102681889A (en) | Scheduling method of cloud computing open platform | |
CN105262835B (en) | Date storage method and device in a kind of multimachine room | |
EP2940596A1 (en) | Data acquisition method and device | |
CN105320570B (en) | Method for managing resource and system | |
CN101452404A (en) | Task scheduling apparatus and method for embedded operating system | |
Qian et al. | An energy-saving task scheduler for mobile devices | |
CN107239331A (en) | A kind of synchronous triggering execution method of concurrent testing task | |
CN111142943A (en) | Automatic control concurrency method and device | |
CN106325977A (en) | Method and system for executing task on the basis of thread pool | |
CN106445651A (en) | Task executing method and system | |
CN104597832A (en) | PLC program scheduler IP core based on AMBA bus | |
CN104407917B (en) | A kind of transaction processing method and device based on JMS |
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 |