CN107463439A - A kind of thread pool implementation method and device - Google Patents

A kind of thread pool implementation method and device Download PDF

Info

Publication number
CN107463439A
CN107463439A CN201710718751.0A CN201710718751A CN107463439A CN 107463439 A CN107463439 A CN 107463439A CN 201710718751 A CN201710718751 A CN 201710718751A CN 107463439 A CN107463439 A CN 107463439A
Authority
CN
China
Prior art keywords
thread
subject
pool
pending task
currently
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.)
Pending
Application number
CN201710718751.0A
Other languages
Chinese (zh)
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.)
Shandong Inspur Genersoft Information Technology Co Ltd
Original Assignee
Shandong Inspur Genersoft 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 Shandong Inspur Genersoft Information Technology Co Ltd filed Critical Shandong Inspur Genersoft Information Technology Co Ltd
Priority to CN201710718751.0A priority Critical patent/CN107463439A/en
Publication of CN107463439A publication Critical patent/CN107463439A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The invention provides a kind of thread pool implementation method and device, this method can include:It is pre-created thread pool;According to default thread configuration parameter, the thread of the first quantity is created, and by the threads store of first quantity into the thread pool;When receiving the pending task of the second quantity, for pending task each described, a subject thread for being currently at idle condition is determined from the thread pool;Transfer the subject thread and perform current pending task;When the subject thread meets default recovery condition, the resource that the subject thread takes is discharged, the subject thread is recovered in the thread pool.This programme can improve the efficiency of processing task.

Description

A kind of thread pool implementation method and device
Technical field
The present invention relates to field of computer technology, more particularly to a kind of thread pool implementation method and device.
Background technology
With the continuous development of Internet technology, requirement more and more higher of the user for server handling ability.For example, A certain moment, server are likely to be received multiple tasks, then how to handle these tasks and compare concern as technical staff Problem.
At present, can be that each task creates a thread, Ran Houli successively when server receives task each time With the thread execution task of establishment, final destroying threads after execution task.If however, each time received task compared with It is more, then the processing mode of a thread is respectively created for each task each time to be devoted a tremendous amount of time, so as to make Into the less efficient of processing task.
The content of the invention
The embodiments of the invention provide a kind of thread pool implementation method and device, it is possible to increase handles the efficiency of task.
In a first aspect, the embodiments of the invention provide a kind of thread pool implementation method, thread pool is pre-created;
According to default thread configuration parameter, the thread of the first quantity is created, and by the threads store of first quantity Into the thread pool;Also include:
When receiving the pending task of the second quantity, for pending task each described, from the thread pool One subject thread for being currently at idle condition of middle determination;
Transfer the subject thread and perform current pending task;
When the subject thread meets default recovery condition, the resource that the subject thread takes is discharged, by described in Subject thread is recovered in the thread pool.
Preferably,
The thread configuration parameter, including:Maximum thread and incremental number;
One subject thread for being currently at idle condition of determination from the thread pool, including:
Judge to whether there is in the thread pool and have the thread for being currently at idle condition, if so, will then be currently at sky Any one thread of not busy state is defined as subject thread;If it is not, then perform,
S1:Judge whether the quantity of the current thread created reaches the maximum thread, if so, then performing S2;If It is no, then perform S3;
S2:The current pending task is stored into default task queue;
S3:Create quantity and be the thread of the incremental number, and store into the thread pool;
S4:Quantity is defined as subject thread for any one in the thread of the incremental number.
Preferably,
After the S2, further comprise:
Detect whether to be recovered in the thread pool and be currently at the thread of idle condition in real time;
When detecting the presence of the thread for being recovered in the thread pool and being currently at idle condition, by the thread It is defined as subject thread.
Preferably,
The thread configuration parameter, including:Time threshold;
It is described to transfer the subject thread and perform current pending task, including:
Transfer the subject thread and perform the current pending task, and start timing;
It is described to discharge the resource that the subject thread takes when the subject thread meets default recovery condition, will The subject thread is recovered in the thread pool, including:
Determine whether timing reaches the time threshold;
If so, the resource that then subject thread described in forcible aborting takes, forces the subject thread being recovered to the line Cheng Chizhong;
The current pending task is completed if it is not, then detecting the subject thread in real time and whether performing, when the target When the current pending task is completed in thread execution, the resource that the subject thread takes is discharged, the subject thread is returned Receive in the thread pool.
Preferably,
Further comprise:
Record and cache the execution information that each described subject thread performs each pending task;
It is described to be directed to each described pending task, from the thread pool determine one be currently at idle condition Subject thread, including:
In each execution information of caching, it is determined whether the mesh corresponding with the current pending task be present Mark execution information;When it is determined that the target execution information be present, according to the target execution information, corresponding first is determined Subject thread;Judge whether the first object thread is currently in idle condition, when judge the first object thread work as It is preceding when being in idle condition, the first object thread is defined as subject thread;
Preferably,
It is described be pre-created thread pool before, further comprise:
One common model for closing the thread pool of encapsulation;
Further comprise:When detecting system finishing, created using the common model destruction of encapsulation each The thread and the thread pool.
Second aspect, the embodiments of the invention provide a kind of thread pool realization device, including:
Creating unit, for being pre-created thread pool;According to default thread configuration parameter, the line of the first quantity of establishment Journey, and by the threads store of first quantity into the thread pool;
Determining unit, for when receiving the pending task of the second quantity, for pending task each described, A subject thread for being currently at idle condition is determined from the thread pool;
Unit is transferred, current pending task is performed for transferring the subject thread;
Recovery unit, taken for when the subject thread meets default recovery condition, discharging the subject thread Resource, the subject thread is recovered in the thread pool.
Preferably,
The thread configuration parameter, including:Maximum thread and incremental number;
The determining unit, there is the thread for being currently at idle condition for judging to whether there is in the thread pool, if It is that any one thread for being currently at idle condition is then defined as subject thread;If it is not, then perform,
S1:Judge whether the quantity of the current thread created reaches the maximum thread, if so, then performing S2;If It is no, then perform S3;
S2:The current pending task is stored into default task queue;
S3:Create quantity and be the thread of the incremental number, and store into the thread pool;
S4:Quantity is defined as subject thread for any one in the thread of the incremental number.
Preferably,
The determining unit, it is further used for after the current pending task is stored into the task queue, Detect whether to be recovered in the thread pool and be currently at the thread of idle condition in real time;It is recovered to when having detected the presence of In the thread pool and when being currently at the thread of idle condition, the thread is defined as subject thread.
Preferably,
The thread configuration parameter, including:Time threshold;
It is described to transfer unit, the current pending task is performed for transferring the subject thread, and start timing;
The recovery unit, for determining whether timing reaches the time threshold;If so, then target described in forcible aborting The resource that thread takes, forces the subject thread being recovered in the thread pool;If it is not, the score is then detected in real time Whether journey, which performs, is completed the current pending task, when the subject thread, which performs, completes the current pending task, The resource that the subject thread takes is discharged, the subject thread is recovered in the thread pool.
Preferably,
Further comprise:Buffer unit;
The buffer unit, each described pending task is performed for recording and caching each described subject thread Execution information;
The determining unit, in each execution information for being cached in the buffer unit, it is determined whether exist The target execution information corresponding with the current pending task;When it is determined that the target execution information be present, according to institute Target execution information is stated, determines corresponding first object thread;Judge the first object thread currently whether in the free time State, when judging that the first object thread is currently at idle condition, the first object thread is defined as target Thread;
Preferably,
Further comprise:Encapsulation unit and processing unit;
The encapsulation unit, for before thread pool is created, encapsulating a common model for closing the thread pool;
The processing unit, for when detecting system finishing, being destroyed and having been created using the common model of encapsulation Each thread and the thread pool.
The embodiments of the invention provide a kind of thread pool implementation method and device, by creating line when also not receiving task Cheng Chi, and the threads store of the first quantity is created into the thread pool, then, can be direct afterwards when receiving pending task A subject thread in idle condition is selected in the thread pool to perform current pending task, without again for connecing Each the pending task received, create a thread successively again, and finally when subject thread meets default recovery During condition, subject thread can be also recovered in thread pool, during to receive pending task again, carry out execution processing, from And avoid when receiving task each time, be required to each time for each task creation thread the drawbacks of, therefore this programme The time of task processing is shortened, improves the efficiency of task processing.
Brief description of the drawings
In order to illustrate more clearly about the embodiment of the present invention or technical scheme of the prior art, below will be to embodiment or existing There is the required accompanying drawing used in technology description to be briefly described, it should be apparent that, drawings in the following description are the present invention Some embodiments, for those of ordinary skill in the art, on the premise of not paying creative work, can also basis These accompanying drawings obtain other accompanying drawings.
Fig. 1 is a kind of flow chart for thread pool implementation method that one embodiment of the invention provides;
Fig. 2 is a kind of flow chart for thread pool implementation method that another embodiment of the present invention provides;
Fig. 3 is a kind of structural representation for thread pool realization device that one embodiment of the invention provides;
Fig. 4 is a kind of structural representation for thread pool realization device that another embodiment of the present invention provides;
Fig. 5 is a kind of structural representation for thread pool realization device that another embodiment of the invention provides.
Embodiment
To make the purpose, technical scheme and advantage of the embodiment of the present invention clearer, below in conjunction with the embodiment of the present invention In accompanying drawing, the technical scheme in the embodiment of the present invention is clearly and completely described, it is clear that described embodiment is Part of the embodiment of the present invention, rather than whole embodiments, based on the embodiment in the present invention, those of ordinary skill in the art The every other embodiment obtained on the premise of creative work is not made, belongs to the scope of protection of the invention.
As shown in figure 1, the embodiments of the invention provide a kind of thread pool implementation method, this method can include following step Suddenly:
Step 101:It is pre-created thread pool.
Step 102:According to default thread configuration parameter, the thread of the first quantity is created, and by first quantity Threads store is into the thread pool.
Step 103:When receiving the pending task of the second quantity, for pending task each described, from institute State one subject thread for being currently at idle condition of determination in thread pool.
Step 104:Transfer the subject thread and perform current pending task.
Step 105:When the subject thread meets default recovery condition, the money that the subject thread takes is discharged Source, the subject thread is recovered in the thread pool.
In the embodiment shown in fig. 1, by creating thread pool when also not receiving task, and the line of the first quantity is created Journey is stored into the thread pool, then afterwards when receiving pending task, can be selected directly in the thread pool at one Current pending task is performed in the subject thread of idle condition, without being directed to each pending received again Business, create a thread successively again, and finally when subject thread meets default recovery condition, can also be by subject thread It is recovered in thread pool, during to receive pending task again, carries out execution processing, takes office so as to avoid to receive each time During business, be required to each time for each task creation thread the drawbacks of, therefore this programme shorten task processing time, carry The high efficiency of task processing.
It is in an embodiment of the invention, described in order to determine a subject thread for being currently at idle condition Thread configuration parameter, including:Maximum thread and incremental number;
The embodiment of the step 103, it may include:Judge to whether there is in the thread pool and be currently at sky The thread of not busy state, if so, any one thread for being currently at idle condition then is defined as into subject thread;If it is not, then hold OK,
S1:Judge whether the quantity of the current thread created reaches the maximum thread, if so, then performing S2;If It is no, then perform S3;
S2:The current pending task is stored into default task queue;
S3:Create quantity and be the thread of the incremental number, and store into the thread pool;
S4:Quantity is defined as subject thread for any one in the thread of the incremental number.
Wherein, thread activity number has been may also include in default thread configuration parameter, e.g., thread activity number is 200, then After thread pool is created, according to default thread activity number, 200 (being the first quantity) individual threads are created.
For example, maximum thread is 500, incremental number is 20, then for current pending task, it is assumed that Neng Gou A thread for being currently at idle condition is found in thread pool, then the thread is directly defined as subject thread, if but It is not present, it is determined that can also create new thread, it is assumed that the current number of threads created is 500, due to having reached Maximum thread, then new thread can not be now created again, by the way that current pending task is stored in into default task queue In, until when have be recovered in thread pool and in idle shape body thread when, reselection subject thread, wait to be performed;If If the current number of threads created is 200,20 new threads can be now created in batches, it is then new at this 20 A subject thread is determined in thread.
Pending task in order to make to be stored in task queue is performed, in an embodiment of the invention, After the S2, further comprise:Detect whether to be recovered in the thread pool and be currently at the line of idle condition in real time Journey;When detecting the presence of the thread for being recovered in the thread pool and being currently at idle condition, the thread is determined For subject thread.
For example, currently 500 threads are created altogether, wherein, default maximum thread is also 500, then for storage Pending task in task queue, a period of time is may wait for, when there are the thread being recycled in thread pool, then Secondary determination subject thread.
In an embodiment of the invention, the thread configuration parameter, including:Time threshold;The step 104 it is specific Embodiment, it may include:Transfer the subject thread and perform the current pending task, and start timing;
The embodiment of the step 105, it may include:Determine whether timing reaches the time threshold;
If so, the resource that then subject thread described in forcible aborting takes, forces the subject thread being recovered to the line Cheng Chizhong;
The current pending task is completed if it is not, then detecting the subject thread in real time and whether performing, when the target When the current pending task is completed in thread execution, the resource that the subject thread takes is discharged, the subject thread is returned Receive in the thread pool.
Wherein, default time threshold can be set according to the actual demand of user.
For example, default time threshold is 3 seconds, then when timing reaches 3 seconds, even if subject thread is not carried out completing Current pending task, can also force the resource of subject thread release busy, and force subject thread being recovered in thread pool; And when timing is not reaching to time threshold 3 seconds, but when subject thread has performed completion current pending task, then discharge target The resource that thread takes, and subject thread is recovered in thread pool, to perform other pending tasks.
In an embodiment of the invention, further comprise:Record and cache each described subject thread perform it is each The execution information of the individual pending task;
The embodiment of the step 103, it may include:
In each execution information of caching, it is determined whether the mesh corresponding with the current pending task be present Mark execution information;When it is determined that the target execution information be present, according to the target execution information, corresponding first is determined Subject thread;Judge whether the first object thread is currently in idle condition, when judge the first object thread work as It is preceding when being in idle condition, the first object thread is defined as subject thread.
For example, current pending task is to load certain webpage, then after thread 3 is completed to load certain webpage, can record it Corresponding execution information, as thread 3 is completed to load certain webpage, so as to when receiving corresponding pending task again, such as Create 100 threads, then it can not first have to the state for judging each thread of blindness, but by the execution information of caching, Directly judge whether thread 3 is currently in idle condition, if so, then directly using thread 3 as subject thread, it is more so as to eliminate The process of secondary judgement, so as to further improve the execution efficiency of task.
In an embodiment of the invention, before the step 101, further comprise:The line is closed in encapsulation one Cheng Chi common model;It can further comprise:When detecting system finishing, destroyed and created using the common model of encapsulation The each thread built and the thread pool.
In the embodiment of the present invention, all threads can be disposably destroyed in system finishing, but in the Thread Count of establishment When soon reaching maximum thread, if larger amount of concurrent tasks are now not present, then in order to reduce the resource of committed memory, Performance is improved, then can destroy the thread that a part is constantly in idle condition.
A kind of thread pool implementation method provided in an embodiment of the present invention is described more detail below, as shown in Fig. 2 this method can To comprise the following steps:
Step 201:It is pre-created a thread pool.
In the embodiment of the present invention, thread pool can be created when system starts., it is necessary to initialize such as when so creating thread pool Lower key code:
ExecutorService=Executors.newCachedThreadPool ();
Step 202:According to default thread configuration parameter, n is created1Individual thread, and by n1Individual threads store is to thread pool In.
In the embodiment of the present invention, it may include have in thread configuration parameter:Thread activity number n1, maximum thread n, incremental number A and time threshold t, wherein, the specific value of parameters can be set according to the user's request of reality.For example, n1= 100th, n=1000, a=50 and t=5 second.When so creating thread first, it can be created according to thread activity number, so, There are 100 threads to be in idle condition in thread pool, can be used to perform the pending task received afterwards.
Wherein, the key code that this part may use is as follows:
Step 203:When receiving n2During individual pending task, for each pending task, judge be in thread pool It is no to there are the thread for being currently at idle condition, if so, step 204 is then performed, if it is not, then performing step 205.
Illustrate 1, n2=50,100 threads for being in idle condition are there is currently in thread pool, then this hour hand , can be from the thread of 100 idle conditions to each pending task in 50 pending tasks, such as pending task 1 Determine any one subject thread as pending task 1.
Illustrate 2, n2=150,100 threads for being in idle condition are there is currently in thread pool, then now It can directly ensure that 100 in 150 pending tasks are performed, and each in remaining 50 pending tasks, it can hold Row step 205.
Step 204:Any one thread for being currently at idle condition is defined as subject thread, and performs step 210.
Step 205:Judge whether the quantity of the current thread created reaches default maximum thread n, if so, then holding Row step 206, if it is not, then performing step 208.
Wherein, in order to ensure system can normal operation, avoid due to committed memory resource excessively causes and system occur The situation of paralysis, the then number of threads created must not exceed maximum thread.
Illustration 2 in above-mentioned steps 203, for each in remaining 50 pending tasks, it is assumed that 1:When Preceding 100 threads created in above-mentioned steps 202, the then quantity 100 of the thread created are much smaller than maximum thread 1000, then for present case, then perform step 208.Assuming that 2:Currently 100 in above-mentioned steps 202 are created After thread, and multiple threads are created according to incremental number, the quantity of the thread such as created is 1000, then for working as cause Condition, then perform step 206.
Step 206:Current pending task is stored into default task queue.
In the embodiment of the present invention, the task queue of specified size can be pre-set, for depositing having not been performed for task.
For the hypothesis 2 in above-mentioned steps 205, because the number of threads created reaches maximum thread, even if line The thread for being currently at idle condition is not present in Cheng Chizhong, then also no longer creates new thread, but by this 50 pending Business, is deposited into task queue, and perform step 207.
Step 207:Detect whether to be recovered in thread pool and be currently at the thread of idle condition in real time, when detecting When there are the thread for being recovered in thread pool and being currently at idle condition, thread is defined as subject thread, and perform step Rapid 210.
According to the analysis in above-mentioned steps 206, it may be determined that appeared in 1000 threads be not in idle condition, But it is regular according to default recovery, thread can finally be reclaimed, therefore, can be after thread recovery, after recovery Thread execution task queue in the having not been performed of the task.Example 1, current time detects has reclaimed 100 threads altogether, then Be stored in each in 50 in task queue tasks, can be determined in the thread of 100 recovery one it is not selected Thread;Example 2, current time detects has reclaimed 20 threads altogether, then can be according to the principle of task queue first in first out First deposit the also non-selected thread of task choosing one being had not been performed into 20 in task queue, and remaining storage 30 pending tasks, when detecting the presence of the thread being recovered in thread pool again, then it is performed.
Step 208:A (a is default incremental number) individual thread is created, and is stored into thread pool.
For the hypothesis 1 in above-mentioned steps 205, because the quantity 100 of the thread created is far smaller than maximum thread, Therefore when the thread in idle condition is not present in thread pool, new thread can be created again according to default incremental number, According to above-mentioned steps 202, then 50 new threads can be created in the embodiment of the present invention again.
Step 209:Any one in a thread is defined as subject thread.
Each in above-mentioned steps 208 in remaining 50 pending tasks is so directed to, can be by the new thread of establishment In also it is non-selected any one be defined as subject thread.
Step 210:Transfer subject thread and perform current pending task, and start timing.
In the embodiment of the present invention, a method can be constructed, so that subject thread is by calling the party before step 201 Method performs the current pending task, and the key code in this method can be as follows:
Thread pool calls the partial code of service logic as follows:
Step 211:Determine whether timing reaches default time threshold, if so, step 212 is then performed, if it is not, then performing Step 213.
According to above-mentioned steps 202, time threshold t=5 seconds, that is to say, that when timing reaches 5 seconds, even if subject thread It is not carried out completing current pending task, the resource of subject thread release busy can be forced, and forces to reclaim subject thread Into thread pool.
Step 212:The resource that forcible aborting subject thread takes, and force subject thread being recovered in thread pool, and Terminate current process.
Step 213:Whether detection subject thread performs the current pending task of completion in real time, if so, then performing step 214。
In the embodiment of the present invention, when timing is not reaching to time threshold 5 seconds, but subject thread has performed completion and currently treated During execution task, then the resource that subject thread takes is discharged, and subject thread is recovered in thread pool, other wait to hold to perform Row task.
Step 214:The resource that subject thread takes is discharged, subject thread is recovered in thread pool.
Step 215:Record the execution information that simultaneously caching of target thread performs current pending task.
For example, current pending task is to open website A, if thread 1 opens website A, records and perform letter corresponding to it Breath, complete to open website A as thread 1 performs, then when receiving corresponding pending task again, can not have to first for every One thread judges one time, but the execution information by being recorded in caching, directly judges that thread 1 is current whether in the free time State, if so, then directly using thread 1 as subject thread.
Step 216:When detecting system finishing, each thread and thread pool created is destroyed.
For example, a STOP model (namely STOP methods) can be encapsulated in advance, then, should by calling in system finishing Method is completed to the destruction of each thread and thread pool that have created.Wherein, the key code of STOP methods can be as follows:
As shown in figure 3, the embodiments of the invention provide a kind of thread pool realization device, including:
Creating unit 301, for being pre-created thread pool;According to default thread configuration parameter, the first quantity is created Thread, and by the threads store of first quantity into the thread pool;
Determining unit 302, for when receiving the pending task of the second quantity, for pending each described Business, a subject thread for being currently at idle condition is determined from the thread pool;
Unit 303 is transferred, current pending task is performed for transferring the subject thread;
Recovery unit 304, for when the subject thread meets default recovery condition, discharging the subject thread and accounting for Resource, the subject thread is recovered in the thread pool.
In an embodiment of the invention, the thread configuration parameter, including:Maximum thread and incremental number;It is described true Order member 302, for judging with the presence or absence of there is the thread for being currently at idle condition in the thread pool, if so, then will be current Any one thread in idle condition is defined as subject thread;If it is not, then perform,
S1:Judge whether the quantity of the current thread created reaches the maximum thread, if so, then performing S2;If It is no, then perform S3;
S2:The current pending task is stored into default task queue;
S3:Create quantity and be the thread of the incremental number, and store into the thread pool;
S4:Quantity is defined as subject thread for any one in the thread of the incremental number.
In an embodiment of the invention, the determining unit, it is further used for depositing by the current pending task After storage is into the task queue, detect whether to be recovered in the thread pool and be currently at the line of idle condition in real time Journey;When detecting the presence of the thread for being recovered in the thread pool and being currently at idle condition, the thread is determined For subject thread.
In an embodiment of the invention, the thread configuration parameter, including:Time threshold;
It is described to transfer unit 303, the current pending task is performed for transferring the subject thread, and start to count When;
The recovery unit 304, for determining whether timing reaches the time threshold;If so, then described in forcible aborting The resource that subject thread takes, forces the subject thread being recovered in the thread pool;If it is not, the mesh is then detected in real time Whether graticule journey, which performs, is completed the current pending task, and the current pending task is completed when the subject thread performs When, the resource that the subject thread takes is discharged, the subject thread is recovered in the thread pool.
As shown in figure 4, further comprise:Buffer unit 401;
The buffer unit 401, for recording and caching each described subject thread, to perform each described pending The execution information of task;
The determining unit 304, in each execution information for being cached in the buffer unit, it is determined whether deposit In the target execution information corresponding with the current pending task;When it is determined that the target execution information be present, according to The target execution information, determine corresponding first object thread;Judge the first object thread currently whether in sky Not busy state, when judging that the first object thread is currently at idle condition, the first object thread is defined as mesh Graticule journey;
As shown in figure 5, in one embodiment of the invention, further comprise:Encapsulation unit 501 and processing unit 502;
The encapsulation unit 501, for before thread pool is created, encapsulating a public mould for closing the thread pool Type;
The processing unit 502, for when detecting system finishing, being destroyed and having been created using the common model of encapsulation The each thread built and the thread pool.
The contents such as the information exchange between each unit, implementation procedure in said apparatus, due to implementing with the inventive method Example is based on same design, and particular content can be found in the narration in the inventive method embodiment, and here is omitted.
The embodiments of the invention provide a kind of computer-readable medium, including computer executed instructions, work as storage control Computing device described in computer executed instructions when, the thread pool that the storage control is performed in any of the above-described embodiment is real Existing method.
The embodiments of the invention provide a kind of storage control, including:Processor, memory and bus;The memory For storing computer executed instructions, the processor is connected with the memory by the bus, when the storage control When device is run, the computer executed instructions of memory storage described in the computing device, so that the storage control Perform the thread pool implementation method in any of the above-described embodiment.
To sum up, each embodiment of the present invention at least has the advantages that:
1st, in embodiments of the present invention, by creating thread pool when also not receiving task, and the line of the first quantity is created Journey is stored into the thread pool, then afterwards when receiving pending task, can be selected directly in the thread pool at one Current pending task is performed in the subject thread of idle condition, without being directed to each pending received again Business, create a thread successively again, and finally when subject thread meets default recovery condition, can also be by subject thread It is recovered in thread pool, during to receive pending task again, carries out execution processing, takes office so as to avoid to receive each time During business, be required to each time for each task creation thread the drawbacks of, therefore this programme shorten task processing time, carry The high efficiency of task processing.
2nd, in embodiments of the present invention, when the thread for being currently at idle condition is not present in thread pool, if now Number of threads through establishment is not up to default maximum thread, then can be created again according to default incremental number a number of Thread, to handle the pending task for being currently needed for processing, so as to further improve the efficiency of task processing, and exist afterwards When task is less, to reduce the occupancy of memory source, thread of the part often in idle condition can also be destroyed.
3rd, in embodiments of the present invention, a certain wait to hold in order to avoid due to some other failures, causing to distribute to thread During row task, the problem of can not performing always, so as to passage time threshold value, so, start timing after thread is transferred, if timing Reach time threshold, then even if thread is not carried out completing, then thread can be subjected to pressure withdrawal, it is other pending not influence The execution of task, so as to further improve the efficiency of task processing.
4th, in embodiments of the present invention, each pending task is performed by recording and caching each subject thread Execution information, so as to after when receiving same task, corresponding first object thread can be determined by execution information, If now judging, first object thread is in idle condition, the first object thread directly can be defined as into subject thread, And the process for judging a next state for each thread created again is avoided, so as to also further increase at task The efficiency of reason.
It should be noted that herein, such as first and second etc relational terms are used merely to an entity Or operation makes a distinction with another entity or operation, and not necessarily require or imply and exist between these entities or operation Any this actual relation or order.Moreover, term " comprising ", "comprising" or its any other variant be intended to it is non- It is exclusive to include, so that process, method, article or equipment including a series of elements not only include those key elements, But also the other element including being not expressly set out, or also include solid by this process, method, article or equipment Some key elements.In the absence of more restrictions, the key element limited by sentence " including one ", is not arranged Except other identical factor in the process including the key element, method, article or equipment being also present.
One of ordinary skill in the art will appreciate that:Realizing all or part of step of above method embodiment can pass through Programmed instruction related hardware is completed, and foregoing program can be stored in computer-readable storage medium, the program Upon execution, the step of execution includes above method embodiment;And foregoing storage medium includes:ROM, RAM, magnetic disc or light Disk etc. is various can be with the medium of store program codes.
It is last it should be noted that:Presently preferred embodiments of the present invention is the foregoing is only, is merely to illustrate the skill of the present invention Art scheme, is not intended to limit the scope of the present invention.Any modification for being made within the spirit and principles of the invention, Equivalent substitution, improvement etc., are all contained in protection scope of the present invention.

Claims (10)

  1. A kind of 1. thread pool implementation method, it is characterised in that
    It is pre-created thread pool;
    According to default thread configuration parameter, the thread of the first quantity is created, and by the threads store of first quantity to institute State in thread pool;Also include:
    It is true from the thread pool for pending task each described when receiving the pending task of the second quantity A fixed subject thread for being currently at idle condition;
    Transfer the subject thread and perform current pending task;
    When the subject thread meets default recovery condition, the resource that the subject thread takes is discharged, by the target Thread is recovered in the thread pool.
  2. 2. according to the method for claim 1, it is characterised in that
    The thread configuration parameter, including:Maximum thread and incremental number;
    One subject thread for being currently at idle condition of determination from the thread pool, including:
    Judge to whether there is in the thread pool and have the thread for being currently at idle condition, if so, will then be currently at idle shape Any one thread of state is defined as subject thread;If it is not, then perform,
    S1:Judge whether the quantity of the current thread created reaches the maximum thread, if so, then performing S2;If it is not, then Perform S3;
    S2:The current pending task is stored into default task queue;
    S3:Create quantity and be the thread of the incremental number, and store into the thread pool;
    S4:Quantity is defined as subject thread for any one in the thread of the incremental number.
  3. 3. according to the method for claim 2, it is characterised in that
    After the S2, further comprise:
    Detect whether to be recovered in the thread pool and be currently at the thread of idle condition in real time;
    When detecting the presence of the thread for being recovered in the thread pool and being currently at idle condition, the thread is determined For subject thread.
  4. 4. according to the method for claim 1, it is characterised in that
    The thread configuration parameter, including:Time threshold;
    It is described to transfer the subject thread and perform current pending task, including:
    Transfer the subject thread and perform the current pending task, and start timing;
    It is described to discharge the resource that the subject thread takes when the subject thread meets default recovery condition, will described in Subject thread is recovered in the thread pool, including:
    Determine whether timing reaches the time threshold;
    If so, the resource that then subject thread described in forcible aborting takes, forces the subject thread being recovered to the thread pool In;
    The current pending task is completed if it is not, then detecting the subject thread in real time and whether performing, when the subject thread When the current pending task is completed in execution, the resource that the subject thread takes is discharged, the subject thread is recovered to In the thread pool.
  5. 5. according to any described methods of claim 1-4, it is characterised in that
    Further comprise:
    Record and cache the execution information that each described subject thread performs each pending task;
    It is described to be directed to each described pending task, a target for being currently at idle condition is determined from the thread pool Thread, including:
    In each execution information of caching, it is determined whether the target corresponding with the current pending task be present and hold Row information;When it is determined that the target execution information be present, according to the target execution information, corresponding first object is determined Thread;Judge whether the first object thread is currently in idle condition, when judging that the first object thread currently locates When idle condition, the first object thread is defined as subject thread;
    And/or
    It is described be pre-created thread pool before, further comprise:
    One common model for closing the thread pool of encapsulation;
    Further comprise:When detecting system finishing, created using the common model destruction of encapsulation each described Thread and the thread pool.
  6. A kind of 6. thread pool realization device, it is characterised in that including:
    Creating unit, for being pre-created thread pool;According to default thread configuration parameter, the thread of the first quantity is created, and By the threads store of first quantity into the thread pool;
    Determining unit, for when receiving the pending task of the second quantity, for pending task each described, from institute State one subject thread for being currently at idle condition of determination in thread pool;
    Unit is transferred, current pending task is performed for transferring the subject thread;
    Recovery unit, the money taken for when the subject thread meets default recovery condition, discharging the subject thread Source, the subject thread is recovered in the thread pool.
  7. 7. device according to claim 6, it is characterised in that
    The thread configuration parameter, including:Maximum thread and incremental number;
    The determining unit, for judging with the presence or absence of there is the thread for being currently at idle condition in the thread pool, if so, then Any one thread for being currently at idle condition is defined as subject thread;If it is not, then perform,
    S1:Judge whether the quantity of the current thread created reaches the maximum thread, if so, then performing S2;If it is not, then Perform S3;
    S2:The current pending task is stored into default task queue;
    S3:Create quantity and be the thread of the incremental number, and store into the thread pool;
    S4:Quantity is defined as subject thread for any one in the thread of the incremental number.
  8. 8. device according to claim 7, it is characterised in that
    The determining unit, it is further used for after the current pending task is stored into the task queue, in real time Detect whether to be recovered in the thread pool and be currently at the thread of idle condition;When detected the presence of be recovered to it is described In thread pool and when being currently at the thread of idle condition, the thread is defined as subject thread.
  9. 9. device according to claim 6, it is characterised in that
    The thread configuration parameter, including:Time threshold;
    It is described to transfer unit, the current pending task is performed for transferring the subject thread, and start timing;
    The recovery unit, for determining whether timing reaches the time threshold;If so, then subject thread described in forcible aborting The resource of occupancy, force the subject thread being recovered in the thread pool;If it is not, then detecting the subject thread in real time is No perform completes the current pending task, when the subject thread, which performs, completes the current pending task, release The resource that the subject thread takes, the subject thread is recovered in the thread pool.
  10. 10. according to any described devices of claim 6-9, it is characterised in that
    Further comprise:Buffer unit;
    The buffer unit, holding for each pending task is performed for recording and caching each described subject thread Row information;
    The determining unit, in each execution information for being cached in the buffer unit, it is determined whether exist and institute State the corresponding target execution information of current pending task;When it is determined that the target execution information be present, according to the mesh Execution information is marked, determines corresponding first object thread;Judge whether the first object thread is currently in idle condition, When judging that the first object thread is currently at idle condition, the first object thread is defined as subject thread;
    And/or
    Further comprise:Encapsulation unit and processing unit;
    The encapsulation unit, for before thread pool is created, encapsulating a common model for closing the thread pool;
    The processing unit, for when detecting system finishing, having been created using the common model destruction of encapsulation each The individual thread and the thread pool.
CN201710718751.0A 2017-08-21 2017-08-21 A kind of thread pool implementation method and device Pending CN107463439A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201710718751.0A CN107463439A (en) 2017-08-21 2017-08-21 A kind of thread pool implementation method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201710718751.0A CN107463439A (en) 2017-08-21 2017-08-21 A kind of thread pool implementation method and device

Publications (1)

Publication Number Publication Date
CN107463439A true CN107463439A (en) 2017-12-12

Family

ID=60549137

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201710718751.0A Pending CN107463439A (en) 2017-08-21 2017-08-21 A kind of thread pool implementation method and device

Country Status (1)

Country Link
CN (1) CN107463439A (en)

Cited By (25)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108345499A (en) * 2018-02-01 2018-07-31 平安科技(深圳)有限公司 Unified thread pool processing method, application server and computer readable storage medium
CN108829521A (en) * 2018-06-13 2018-11-16 平安科技(深圳)有限公司 Task processing method and device, computer equipment and storage medium
CN108920275A (en) * 2018-06-25 2018-11-30 郑州云海信息技术有限公司 A kind of method, apparatus, equipment and computer storage medium grabbing black box log
CN109189506A (en) * 2018-08-06 2019-01-11 北京奇虎科技有限公司 A kind of method and device based on PHP asynchronous process task
CN109189483A (en) * 2018-08-02 2019-01-11 优视科技新加坡有限公司 Regulation method, apparatus, equipment/terminal/server and the storage medium of task execution
CN109271252A (en) * 2018-08-29 2019-01-25 华为技术有限公司 A kind of method and device adjusting Thread Count
CN109388485A (en) * 2018-09-26 2019-02-26 广州虎牙信息科技有限公司 A kind of processing method, device, equipment and the storage medium of task execution thread
CN109542640A (en) * 2018-11-13 2019-03-29 平安科技(深圳)有限公司 Picture upload method, device, computer equipment and storage medium
CN110032434A (en) * 2019-03-12 2019-07-19 平安普惠企业管理有限公司 A kind of method of adjustment and device of semaphore license
CN110413390A (en) * 2019-07-24 2019-11-05 深圳市盟天科技有限公司 Thread task processing method, device, server and storage medium
CN110633133A (en) * 2018-06-21 2019-12-31 中兴通讯股份有限公司 Task processing method and device and computer readable storage medium
CN110908784A (en) * 2019-11-12 2020-03-24 苏州智加科技有限公司 Image labeling method, device, equipment and storage medium
CN111338787A (en) * 2020-02-04 2020-06-26 浙江大华技术股份有限公司 Data processing method and device, storage medium and electronic device
CN111432243A (en) * 2020-04-01 2020-07-17 山东汇贸电子口岸有限公司 Video transcoding method and device
CN111552567A (en) * 2020-04-27 2020-08-18 北京奇艺世纪科技有限公司 Thread management method and device, electronic equipment and storage medium
CN111625336A (en) * 2020-05-22 2020-09-04 金蝶软件(中国)有限公司 Thread data processing method and device
CN111651328A (en) * 2020-06-08 2020-09-11 北京字节跳动网络技术有限公司 Thread monitoring method and device
CN111782295A (en) * 2020-06-29 2020-10-16 珠海豹趣科技有限公司 Application program running method and device, electronic equipment and storage medium
CN111782293A (en) * 2020-06-28 2020-10-16 珠海豹趣科技有限公司 Task processing method and device, electronic equipment and readable storage medium
CN112035255A (en) * 2020-08-28 2020-12-04 北京浪潮数据技术有限公司 Thread pool resource management task processing method, device, equipment and storage medium
CN112416584A (en) * 2020-11-18 2021-02-26 捷开通讯(深圳)有限公司 Process communication method, device, storage medium and mobile terminal
CN112667385A (en) * 2021-01-15 2021-04-16 北京金和网络股份有限公司 Cloud service system, task execution method and device thereof, and server
CN113727110A (en) * 2021-08-27 2021-11-30 猪八戒股份有限公司 Method, apparatus and medium for converting h264 format into mp4 format
CN113742355A (en) * 2020-06-29 2021-12-03 北京沃东天骏信息技术有限公司 Method, device, equipment and computer readable medium for updating inventory
CN114201726A (en) * 2020-09-18 2022-03-18 深圳先进技术研究院 Convolution operation optimization method, system, terminal and storage medium

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101135981A (en) * 2007-08-29 2008-03-05 中兴通讯股份有限公司 Method and device for realizing batch report generation
CN101290619A (en) * 2007-04-20 2008-10-22 西北民族大学 Content based Tibetan website tangka image search engine intelligent robot search method
US20090172674A1 (en) * 2007-12-28 2009-07-02 International Business Machines Corporation Managing the computer collection of information in an information technology environment
US20120297393A1 (en) * 2009-12-30 2012-11-22 Zte Corporation Data Collecting Method, Data Collecting Apparatus and Network Management Device
CN103366471A (en) * 2013-06-26 2013-10-23 福建联迪商用设备有限公司 Online service concurrent processing method, system and server
US20150135183A1 (en) * 2013-11-12 2015-05-14 Oxide Interactive, LLC Method and system of a hierarchical task scheduler for a multi-thread system
CN106681811A (en) * 2016-12-08 2017-05-17 腾讯科技(深圳)有限公司 Multi-thread scheduling method and device based on thread pool

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101290619A (en) * 2007-04-20 2008-10-22 西北民族大学 Content based Tibetan website tangka image search engine intelligent robot search method
CN101135981A (en) * 2007-08-29 2008-03-05 中兴通讯股份有限公司 Method and device for realizing batch report generation
US20090172674A1 (en) * 2007-12-28 2009-07-02 International Business Machines Corporation Managing the computer collection of information in an information technology environment
US20120297393A1 (en) * 2009-12-30 2012-11-22 Zte Corporation Data Collecting Method, Data Collecting Apparatus and Network Management Device
CN103366471A (en) * 2013-06-26 2013-10-23 福建联迪商用设备有限公司 Online service concurrent processing method, system and server
US20150135183A1 (en) * 2013-11-12 2015-05-14 Oxide Interactive, LLC Method and system of a hierarchical task scheduler for a multi-thread system
CN106681811A (en) * 2016-12-08 2017-05-17 腾讯科技(深圳)有限公司 Multi-thread scheduling method and device based on thread pool

Cited By (34)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108345499A (en) * 2018-02-01 2018-07-31 平安科技(深圳)有限公司 Unified thread pool processing method, application server and computer readable storage medium
CN108829521A (en) * 2018-06-13 2018-11-16 平安科技(深圳)有限公司 Task processing method and device, computer equipment and storage medium
CN110633133A (en) * 2018-06-21 2019-12-31 中兴通讯股份有限公司 Task processing method and device and computer readable storage medium
CN108920275A (en) * 2018-06-25 2018-11-30 郑州云海信息技术有限公司 A kind of method, apparatus, equipment and computer storage medium grabbing black box log
CN109189483A (en) * 2018-08-02 2019-01-11 优视科技新加坡有限公司 Regulation method, apparatus, equipment/terminal/server and the storage medium of task execution
WO2020026010A3 (en) * 2018-08-02 2020-03-05 优视科技新加坡有限公司 Task execution control method, device, equipment/terminal/server and storage medium
CN109189506A (en) * 2018-08-06 2019-01-11 北京奇虎科技有限公司 A kind of method and device based on PHP asynchronous process task
CN109271252A (en) * 2018-08-29 2019-01-25 华为技术有限公司 A kind of method and device adjusting Thread Count
CN109388485A (en) * 2018-09-26 2019-02-26 广州虎牙信息科技有限公司 A kind of processing method, device, equipment and the storage medium of task execution thread
CN109388485B (en) * 2018-09-26 2021-01-22 广州虎牙信息科技有限公司 Processing method, device and equipment of task execution thread and storage medium
CN109542640A (en) * 2018-11-13 2019-03-29 平安科技(深圳)有限公司 Picture upload method, device, computer equipment and storage medium
CN109542640B (en) * 2018-11-13 2023-08-11 平安科技(深圳)有限公司 Picture uploading method and device, computer equipment and storage medium
CN110032434A (en) * 2019-03-12 2019-07-19 平安普惠企业管理有限公司 A kind of method of adjustment and device of semaphore license
CN110413390A (en) * 2019-07-24 2019-11-05 深圳市盟天科技有限公司 Thread task processing method, device, server and storage medium
CN110908784A (en) * 2019-11-12 2020-03-24 苏州智加科技有限公司 Image labeling method, device, equipment and storage medium
CN110908784B (en) * 2019-11-12 2022-09-20 苏州智加科技有限公司 Image labeling method, device, equipment and storage medium
CN111338787A (en) * 2020-02-04 2020-06-26 浙江大华技术股份有限公司 Data processing method and device, storage medium and electronic device
CN111338787B (en) * 2020-02-04 2023-09-01 浙江大华技术股份有限公司 Data processing method and device, storage medium and electronic device
CN111432243A (en) * 2020-04-01 2020-07-17 山东汇贸电子口岸有限公司 Video transcoding method and device
CN111552567A (en) * 2020-04-27 2020-08-18 北京奇艺世纪科技有限公司 Thread management method and device, electronic equipment and storage medium
CN111625336A (en) * 2020-05-22 2020-09-04 金蝶软件(中国)有限公司 Thread data processing method and device
CN111625336B (en) * 2020-05-22 2024-03-19 金蝶软件(中国)有限公司 Thread data processing method and device
CN111651328A (en) * 2020-06-08 2020-09-11 北京字节跳动网络技术有限公司 Thread monitoring method and device
CN111782293A (en) * 2020-06-28 2020-10-16 珠海豹趣科技有限公司 Task processing method and device, electronic equipment and readable storage medium
CN111782295A (en) * 2020-06-29 2020-10-16 珠海豹趣科技有限公司 Application program running method and device, electronic equipment and storage medium
CN111782295B (en) * 2020-06-29 2023-08-29 珠海豹趣科技有限公司 Application program running method and device, electronic equipment and storage medium
CN113742355A (en) * 2020-06-29 2021-12-03 北京沃东天骏信息技术有限公司 Method, device, equipment and computer readable medium for updating inventory
CN112035255A (en) * 2020-08-28 2020-12-04 北京浪潮数据技术有限公司 Thread pool resource management task processing method, device, equipment and storage medium
CN114201726A (en) * 2020-09-18 2022-03-18 深圳先进技术研究院 Convolution operation optimization method, system, terminal and storage medium
WO2022104924A1 (en) * 2020-11-18 2022-05-27 捷开通讯(深圳)有限公司 Process communication method and apparatus, storage medium, and mobile terminal
CN112416584B (en) * 2020-11-18 2023-12-19 捷开通讯(深圳)有限公司 Process communication method and device, storage medium and mobile terminal
CN112416584A (en) * 2020-11-18 2021-02-26 捷开通讯(深圳)有限公司 Process communication method, device, storage medium and mobile terminal
CN112667385A (en) * 2021-01-15 2021-04-16 北京金和网络股份有限公司 Cloud service system, task execution method and device thereof, and server
CN113727110A (en) * 2021-08-27 2021-11-30 猪八戒股份有限公司 Method, apparatus and medium for converting h264 format into mp4 format

Similar Documents

Publication Publication Date Title
CN107463439A (en) A kind of thread pool implementation method and device
CN100549982C (en) Memory management apparatus, method and system based on the dynamic property monitoring
CN101432702B (en) System and method for allocating and deallocating memory within transactional code
CN104699625B (en) For the system and method that synchronous transient resource uses between the virtual machine in virtual machine monitor environment
CN107391268A (en) service request processing method and device
CN104063293B (en) A kind of data back up method and stream calculation system
CN106952158A (en) Solve the problems, such as the bookkeeping methods and equipment of focus account
CN103810048A (en) Automatic adjusting method and device for thread number aiming to realizing optimization of resource utilization
CN106547612A (en) A kind of multi-task processing method and device
CN104461758B (en) A kind of quick abnormality eliminating method and its processing structure for emptying streamline of tolerance cache missings
CN103927305B (en) It is a kind of that the method and apparatus being controlled is overflowed to internal memory
CN104221005B (en) For sending a request to the mechanism of accelerator from multithreading
CN1728087A (en) Method and apparatus for implementing memory order models with order vectors
CN107278296A (en) For performing the apparatus and method for generating tracking data in response to affairs
CN107368450A (en) Realize the operation for the multiple-slice processor for safeguarding the load/store unit for being refused instruction
CN105373434B (en) resource management system and method
CN113435998B (en) Loan overdue prediction method and device, electronic equipment and storage medium
CN1950775B (en) Intrusion detection during program execution in a computer
CN109614344A (en) A kind of spatial cache recovery method, device, equipment and storage system
CN107065616A (en) Inter-linked controlling method and device, computer-readable recording medium
CN109144428A (en) It is a kind of applied to the rubbish recovering method of solid state hard disk, equipment and medium
CN106201918B (en) A kind of method and system based on big data quantity and extensive caching quick release
CN103140847B (en) Regulate atomic memory operation with the attack preventing denial of service
CN105573721A (en) Programmable load replay precluding mechanism
CN107526622A (en) Rapid exception handling method and device for Linux

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
RJ01 Rejection of invention patent application after publication
RJ01 Rejection of invention patent application after publication

Application publication date: 20171212