[go: up one dir, main page]

CN106528065B - A kind of thread acquisition methods and equipment - Google Patents

A kind of thread acquisition methods and equipment Download PDF

Info

Publication number
CN106528065B
CN106528065B CN201510583969.0A CN201510583969A CN106528065B CN 106528065 B CN106528065 B CN 106528065B CN 201510583969 A CN201510583969 A CN 201510583969A CN 106528065 B CN106528065 B CN 106528065B
Authority
CN
China
Prior art keywords
thread
threads
sets
user task
pending user
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201510583969.0A
Other languages
Chinese (zh)
Other versions
CN106528065A (en
Inventor
王世纪
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Advanced New Technologies Co Ltd
Advantageous New Technologies Co Ltd
Original Assignee
Alibaba Group Holding 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 Alibaba Group Holding Ltd filed Critical Alibaba Group Holding Ltd
Priority to CN201510583969.0A priority Critical patent/CN106528065B/en
Publication of CN106528065A publication Critical patent/CN106528065A/en
Application granted granted Critical
Publication of CN106528065B publication Critical patent/CN106528065B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

This application discloses a kind of thread acquisition methods and equipment, pass through the technical solution proposed using the embodiment of the present application, the prior art is solved when concurrently obtaining thread by thread pool, carry out locking judge whether thread is idle, be lined up obtain thread, so as to cause obtain thread inefficiency the problem of.The technical solution that the embodiment of the present application is proposed is when application program obtains the thread in thread pool and executes operation, it does not need to be locked, the contention for resources between task and task is reduced to the greatest extent, the efficiency for obtaining the thread of execution task is improved, and then enhances the retractility and robustness of application program.

Description

A kind of thread acquisition methods and equipment
Technical field
This application involves field of communication technology, in particular to a kind of thread acquisition methods and a kind of thread obtain equipment.
Background technique
In the application based on Object-Oriented Programming, creation and destruction object are time consuming, because creation one is right As memory source to be obtained or other more resources.
For example, in the application based on Java programming technique, JVM (JAVA Virtual Machine, JAVA virtual machine) It will attempt to track each object, so as to carry out garbage reclamation after object destruction.So improving service routine efficiency One means is exactly to reduce creation as far as possible and destroy the number of object, the more very Object Creation and pin in cost source It ruins.Exactly one critical issue for needing to solve how is serviced using existing object, then produces Thread Pool Technology.
Thread Pool Technology can effectively reduce the consumption of resource in multi-thread environment, and the processing capacity of system can be improved.But It is its defect is when concurrently obtaining thread by thread pool, to guarantee that same thread will not be by two pending work Task obtains simultaneously, is locked, and is lined up and obtains thread, thus will cause and obtains the reduction of thread efficiency.
Summary of the invention
The application provides a kind of thread acquisition methods and equipment, obtains the thread in thread pool in application program and executes operation When, it has reduced the contention for resources between task and task to the greatest extent, has improved the efficiency for obtaining the thread of execution task, and then enhance The retractility and robustness of application program.
The embodiment of the present application provides a kind of thread acquisition methods, the described method comprises the following steps:
In application server starting, thread pool is divided into multiple sets of threads;
Execution operation is carried out when the one or more pending user tasks of confirmation need to obtain thread from the thread pool When, the sets of threads that each pending user task is belonged to is determined respectively;
Whether there is free idle thread in each sets of threads determined by judging respectively;
When judging is free idle thread in current thread set, the idle thread is returned to and works as front described in belonging to The pending user task of Cheng Jihe.
Preferably, described in application server starting, thread pool is divided into after multiple sets of threads, further includes:
For each sets of threads, minimum number of threads and maximum thread amount are set;
The thread of minimum number of threads is created for each sets of threads;
Each sets of threads of thread of the initialization package containing the minimum number of threads.
Preferably, it is described judge respectively determined by whether be free idle thread in each sets of threads after, further includes:
When judging does not have idle thread in current thread set, judge to execute operation in the current thread set The maximum thread amount whether user task quantity is greater than for sets of threads setting;
If be not greater than, a thread is created in the current thread set, for belonging to the current thread The pending user task of set uses;
If it is greater, then being judged in other sets of threads in the thread pool according to preset thread Substitution Rules Whether thread can be obtained for the pending user task, if it is then the thread that will acquire return to it is described to User task is executed, if it is not possible, then returning to abnormal prompt message.
Preferably, described according to preset thread Substitution Rules, judge in other sets of threads in the thread pool Whether thread can be obtained for the pending user task, if it is then the thread that will acquire return to it is described to User task is executed, if it is not possible, abnormal prompt message is then returned, specifically:
A replacement sets of threads is selected in the thread pool according to preset sequence or at random;
If in the replacement sets of threads currently including idle thread, the idle thread is returned to described wait hold Row user task, and record sets of threads belonging to the idle thread used in the pending user task and its in institute State the position in thread pool;
If whether judging the replacement sets of threads currently without comprising idle thread in the replacement sets of threads New thread can be created, if it is then the thread that creation is new, and the new thread is returned into the pending user Task, and record sets of threads belonging to the new thread used in the pending user task and its in the thread Position in pond;
If the replacement sets of threads cannot create new thread, continue that next replacement sets of threads is selected to attempt It finds idle thread or creates new thread to the pending user task;
When all sets of threads in the thread pool all can not provide idle thread or creation for the pending task When new thread, abnormal prompt message is returned.
It is preferably, described to judge whether the replacement sets of threads create new thread, specifically:
According to the current thread quantity in the replacement sets of threads and for each sets of threads, maximum thread is set Quantity, judges whether the replacement sets of threads can create new thread
Maximum thread is set for each sets of threads when the current thread quantity in the replacement sets of threads is less than When amount, it is determined that the replacement sets of threads can create new thread;
When the current thread quantity in the replacement sets of threads is equal to or more than for each sets of threads setting maximum When number of threads, it is determined that the replacement sets of threads cannot create new thread.
Preferably, the sets of threads for determining each pending user task respectively and being belonged to, specifically includes:
The ID of currently received each pending user task is obtained respectively;
Respectively according to the ID of each pending user task to the sets of threads number divided in the thread pool into Row modulus;
The sets of threads that each pending user task is belonged to is determined respectively according to corresponding modulus result.
On the other hand, present invention also provides a kind of threads to obtain equipment, comprising:
Thread management module, in application server starting, thread pool to be divided into multiple sets of threads;
Locating module, for when confirm user task need from the thread pool obtain thread carry out execute operation when, In each sets of threads that the thread management module is divided, the thread collection that each pending user task is belonged to is determined respectively It closes;
Judgment module, for judging whether there is free idle thread in each sets of threads determined by the locating module respectively;
Processing module, for when the judgment module, which judges, is free idle thread in current thread set, by the free time Thread returns to the pending user task for belonging to the current thread set.
Preferably, the thread management module, is also used to:
After the thread pool is divided into multiple sets of threads, minimum number of threads is set for each sets of threads With maximum thread amount;
The thread of minimum number of threads is created for each sets of threads;
Each sets of threads of thread of the initialization package containing the minimum number of threads.
Preferably, the processing module, is specifically used for:
The judgment module is also used to judge when judging does not have idle thread in the current thread set described It is that the sets of threads is set that whether the user task quantity that operation is executed in current thread set, which is greater than the thread management module, The maximum thread amount set;
The processing module is also used to work as front described when the judging result of the judgment module is to be not greater than A thread is created in Cheng Jihe, the pending user task for belonging to the current thread set uses, or sentences described The judging result of disconnected module be greater than when, according to preset thread Substitution Rules, judge other threads in the thread pool Whether thread can be obtained for the pending user task in set, if it is then the thread that will acquire returns to The pending user task, if it is not possible, then returning to abnormal prompt message.
Preferably, the locating module, is specifically used for:
The ID of currently received each pending user task is obtained respectively;
The thread management module is drawn in the thread pool according to the ID of each pending user task respectively The sets of threads number divided carries out modulus;
The sets of threads that each pending user task is belonged to is determined respectively according to corresponding modulus result.
Compared with prior art, the technical solution that the embodiment of the present application is proposed has the advantage that
By the technical solution proposed using the embodiment of the present application, solves the prior art and concurrently obtained by thread pool When obtaining thread, carry out locking judge whether thread is idle, be lined up and obtain thread, so as to cause thread low efficiency is obtained Under problem.When thread of the technical solution that the embodiment of the present application is proposed in application program acquisition thread pool executes operation, It does not need to be locked, has reduced the contention for resources between task and task to the greatest extent, improve the thread for obtaining execution task Efficiency, and then enhance the retractility and robustness of application program.
Detailed description of the invention
It, below will be to institute in the embodiment of the present application description in order to clearly illustrate the technical solution of the embodiment of the present application Attached drawing to be used is needed to be briefly described, it should be apparent that, the accompanying drawings in the following description is only some implementations of the application Example, for those of ordinary skill in the art, without creative efforts, can also implement according to the application These attached drawings of example obtain other attached drawings.
A kind of flow diagram for thread acquisition methods that Fig. 1 is proposed by the embodiment of the present application;
The flow diagram of thread acquisition methods in a kind of specific embodiment that Fig. 2 is proposed by the embodiment of the present application;
A kind of thread that Fig. 3 is proposed by the embodiment of the present application obtains the structural schematic diagram of equipment.
Specific embodiment
As the application background technique is stated, in existing scheme, when concurrently obtaining thread by thread pool, need Carry out locking judge whether thread is idle, is lined up and obtains thread, thus, cause to obtain thread inefficiency.
To overcome defect in the prior art, the embodiment of the present application proposes a kind of thread acquisition methods and solves existing skill Art carries out locking judge whether thread is idle, is lined up and obtains thread when concurrently obtaining thread by thread pool, from And lead to the problem of obtaining thread inefficiency.
Embodiment one
As shown in Figure 1, a kind of flow diagram of the thread acquisition methods proposed by the embodiment of the present application, this method tool Body the following steps are included:
Step S101, when application server starts, thread pool is divided into multiple sets of threads.
In specific application scenarios, in order to realize subsequent thread process, sets of threads is carried out in this step and divides it Afterwards, further include following processing:
Firstly, minimum number of threads and maximum thread amount is arranged for each sets of threads divided.
According to setting as a result, creating the thread of minimum number of threads for each sets of threads, then, initialization package contains institute State each sets of threads of the thread of minimum number of threads.
It should be noted that in the process above, why only creating minimum number of threads in the embodiment of the present application Thread, reason is specific as follows:
Firstly, creation thread is the work for expending very much computer resource in Thread Pool Technology, so, it should to the greatest extent Amount reduces thread creation operation.But if in advance without the creation of thread, but just carrying out line when having task arriving It then will include the time that this step of thread creation is lost in the response time if journey creates, thus, the response time can be made It is slack-off.
Based on above-mentioned consideration, the technical solution that the embodiment of the present application is proposed just is pre-created one when servicing starting The thread of minimum number of threads meets daily demand with this, thus, it under normal circumstances, can when receiving user task With directly using the thread created, without creating thread again.
On the other hand, setting maximum thread amount be because are as follows: if task explodes suddenly, and minimum thread is not obviously at this time It is enough use, then server can newly created threads realize better response, still because computer resource is also limited, because This, can provide a maximum thread amount, once being more than this quantity, then will not continue to create new thread, because creating again If, the service ability of application server can be made to decline.
Therefore, the technical solution proposed for the embodiment of the present application can start under normal circumstances in application server When, the thread of minimum number of threads is created to per thread set, and if task quantity explodes, it can be created in thread pool New thread is used for new task, but not will create the thread more than maximum thread amount.
After the processing for completing this step, when the one or more pending user tasks of confirmation are needed from the thread pool When middle acquisition thread carries out executing operation, step S102 is executed.
Step S102, the sets of threads that each pending user task is belonged to is determined respectively.
Specifically, the processing of this step can be with are as follows:
The ID of currently received each pending user task is obtained respectively;
Respectively according to the ID of each pending user task to the sets of threads number divided in the thread pool into Row modulus;
The sets of threads that each pending user task is belonged to is determined respectively according to corresponding modulus result.
In the present embodiment due to when application server starts, be created that one by multiple sets of threads institute groups At thread pool;It, can be according to different user tasks when confirmation user task, which needs to obtain thread, to carry out executing operation ID positions different sets of threads, so that not needing lock to sentence when concurrently obtaining thread by thread pool Whether the journey that breaks is idle, improves efficiency for further acquisition thread.
Step S103, whether there is free idle thread in each sets of threads determined by judging respectively.
When judging is free idle thread in current thread set, step S104 is executed.
Step S104, the idle thread is returned to the pending user task for belonging to the current thread set.
It by above-mentioned processing, may be implemented there are idle thread, to belong in per thread set Pending user task distribute the processing of corresponding idle thread, thus, even if in the case where multi-thread concurrent, can also be with The thread distribution that each pending user task is realized by multiple sets of threads, is passing through line so as to solve the prior art When Cheng Chi concurrently obtains thread, carries out locking judge whether thread is idle, be lined up and obtain thread, so as to cause acquisition The problem of thread inefficiency.
In order to more completely illustrate technical solution that the embodiment of the present application is proposed, the present embodiment is further illustrated in step In rapid S103, there is no subsequent processes when idle thread in current thread set, is described as follows:
When judging does not have idle thread in current thread set, judge to execute operation in the current thread set User task quantity whether be greater than in step S101 be sets of threads setting maximum thread amount.
If be not greater than, illustrate that sets of threads at this time is also unsaturated, then creates one in the current thread set A thread, the pending user task for belonging to the current thread set use.
If it does, the current thread set of explanation at this time has been saturated, then according to preset thread Substitution Rules, judgement Whether thread can be obtained for the pending user task in other sets of threads in the thread pool, if can With the thread that then will acquire returns to the pending user task, if it is not possible, then returning to abnormal prompt message.
In order to make full use of the thread resources in thread pool, in the feelings that current thread set does not have resource that can create thread Under condition, the thread resources in other sets of threads can be called, and for the rational management of whole resource, the embodiment of the present application into One step introduces above-mentioned thread Substitution Rules, and in specific application scenarios, it is specific that above-mentioned application thread goes out treatment process It can be such that
Firstly, a replacement sets of threads is selected in the thread pool according to preset sequence or at random.
If in the replacement sets of threads currently including idle thread, the idle thread is returned to described wait hold Row user task, and record sets of threads belonging to the idle thread used in the pending user task and its in institute State the position in thread pool.
If whether judging the replacement sets of threads currently without comprising idle thread in the replacement sets of threads New thread can be created.
If the replacement sets of threads can create new thread, new thread is created, and by the new thread The pending user task is returned to, and records line belonging to the new thread used in the pending user task Cheng Jihe and its position in the thread pool.
If the replacement sets of threads cannot create new thread, continue that next replacement sets of threads is selected to attempt It finds idle thread or creates new thread to the pending user task.
When all sets of threads in the thread pool all can not provide idle thread or creation for the pending task When new thread, abnormal prompt message is returned.
In specific application scenarios, above-mentioned judges whether the replacement sets of threads can create the behaviour of new thread Make, can realize in the following way:
According to the current thread quantity in the replacement sets of threads and for each sets of threads, maximum thread is set Quantity illustrates that the replacement sets of threads at this time is also unsaturated, then judges whether the replacement sets of threads can create newly Thread.
Maximum thread is set for each sets of threads when the current thread quantity in the replacement sets of threads is less than When amount, it is determined that the replacement sets of threads can create new thread.
When the current thread quantity in the replacement sets of threads is equal to or more than for each sets of threads setting maximum When number of threads, it is determined that the replacement sets of threads cannot create new thread.
In specific application scenarios, judge whether other sets of threads in thread pool include idle thread, Yi Ji There is no the operation of new thread whether can be created in the case where idle thread, to make full use of the thread resources in thread pool to provide A set of substitution of resources scheme that layering evolution may be implemented, and corresponding replacement operation can be concentrated in certain time section It is carried out in a sets of threads, this will not interfere the normal operating of other sets of threads, without carrying out as the prior art The processing for locking and being lined up, under the scene for improving concurrent thread, the efficiency of scheduling of resource and distribution.
By the technical solution proposed using the embodiment of the present application, solves the prior art and concurrently obtained by thread pool When obtaining thread, carry out locking judge whether thread is idle, be lined up and obtain thread, so as to cause thread low efficiency is obtained Under problem.When thread of the technical solution that the embodiment of the present application is proposed in application program acquisition thread pool executes operation, It does not need to be locked, has reduced the contention for resources between task and task to the greatest extent, improve the thread for obtaining execution task Efficiency, and then enhance the retractility and robustness of application program.
Embodiment two
In order to which the technical solution of the application is further described, now in conjunction with specific application scenarios, to the technical side of the application Case is illustrated, as shown in Fig. 2, a kind of thread acquisition methods that the present embodiment proposes, are applied in application server, this method Specifically includes the following steps:
Step S201 creates a thread pool in application server starting, and the thread pool is by multiple sets of threads groups At, the thread of minimum number of threads is created for each sets of threads, then, line of the initialization package containing the minimum number of threads Each sets of threads of journey.
In the application preferred embodiment, in application service starting, what creation one was made of multiple sets of threads Thread pool is equivalent to an original big sets of threads, is divided into several small sets of threads, these small sets of threads A thread pool is formed, when determining that user task needs to obtain user task, can be positioned according to different user task ID Into different sets of threads, so that different user tasks does not need to be locked during concurrently obtaining thread, Improve the efficiency of thread acquisition.
In application service starting, the thread of minimum number of threads why is first created for each sets of threads, be because It is that the work for expending very much computer resource in creation thread, is then wrapped when there is task arriving in the response time for creation thread Containing this step of thread creation, the response time can be made slack-off, therefore in application service starting, first be created for each sets of threads The thread for building minimum number of threads, meets daily demand at this time, then can be directly sharp if there is application task needs to obtain thread With thread, do not need temporarily to create thread again.
In application service starting, why maximum thread amount is set, is to make application server in existing clothes It preferably works in business limit of power.For example, if being provided with 10 minimum lines in application server starting for sets of threads Number of passes amount when user task needs the number of threads that obtains to increase suddenly, such as needs 500 threads, at this time minimum Thread Count Amount is obvious not enough to be used, then application server will create new thread preferably to respond, but because computer resource is also It is limited, it is therefore desirable to set a maximum thread amount to sets of threads, for example setting maximum thread amount is 100, then Even more than 100, new thread will not be created in the sets of threads again, because creating new thread again, can to apply The service ability of server declines.Therefore, setting maximum thread amount is to make application server in existing service ability It preferably works in range.
Step S202, when confirmation user task, which needs to obtain thread, to carry out executing operation, according to the user task ID positions the sets of threads in the thread pool.
In the application preferred embodiment, the thread collection positioned according to the ID of the user task in thread pool It closes, specifically includes:
Modulus is carried out to the sets of threads number in the thread pool according to the ID of the user task, obtains modulus value, root Corresponding sets of threads is positioned according to the modulus value.So that different user tasks navigates to different corresponding sets of threads In.So that different user tasks does not need to be locked during concurrently obtaining thread, the efficiency of thread acquisition is improved.
Step S203 judges whether there is free idle thread in the sets of threads.
When being free idle thread in the sets of threads, step S204 is executed;
When there is no idle thread in the sets of threads, step S205 is executed.
The idle thread is returned to user task by step S204.
Step S205 judges whether that new thread task can be created in the sets of threads.
The judgment basis is the current user task number for executing operation less than the max line for being the sets of threads setting Number of passes amount.
If new thread task can be created in the sets of threads, S206 is thened follow the steps;
If new thread task cannot be created in the sets of threads, S207 is thened follow the steps.
Step S206 creates a thread and uses for user task.
Whether step S207 judges in other sets of threads in the thread pool comprising idle thread.
If including idle thread in other sets of threads in the thread pool, S208 is thened follow the steps;
If not including idle thread in other sets of threads in the thread pool, S209 is thened follow the steps.
The idle thread is returned to user task by step S208, and is recorded current user task and used belonging to thread Sets of threads and its position in the thread pool.
Step S209 judges whether that new thread can be created in other sets of threads in the thread pool;
It is that other described sets of threads are set that the judgment basis, which is whether the Thread Count in other described sets of threads be less than, The maximum thread amount set.
If new thread can be created in other sets of threads in the thread pool, S210 is thened follow the steps;
If new thread cannot be created in other sets of threads in the thread pool, S211 is thened follow the steps.
Step S210 creates new thread, the new thread is returned to user task, and record current user task Use sets of threads belonging to thread and its position in the thread pool.
Step S211 shows the declaration of exception currently without available thread.
By the technical solution proposed using the embodiment of the present application, solves the prior art and concurrently obtained by thread pool When obtaining thread, carry out locking judge whether thread is idle, be lined up and obtain thread, so as to cause thread low efficiency is obtained Under problem.When thread of the technical solution that the embodiment of the present application is proposed in application program acquisition thread pool executes operation, It does not need to be locked, has reduced the contention for resources between task and task to the greatest extent, improve the thread for obtaining execution task Efficiency, and then enhance the retractility and robustness of application program.
Embodiment three
Based on similarly applying conceiving with above-mentioned thread acquisition methods implementation process, one is additionally provided in the embodiment of the present application Kind thread obtains equipment, as shown in figure 3, the equipment includes:
Thread management module 31, in application server starting, thread pool to be divided into multiple sets of threads;
Locating module 32, for carrying out execution operation when confirmation user task needs to obtain thread from the thread pool When, in each sets of threads that the thread management module 31 is divided, determine what each pending user task was belonged to respectively Sets of threads;
Judgment module 33, for judging whether there is free disengaged line in each sets of threads determined by the locating module 32 respectively Journey;
Processing module 34 will be described for when the judgment module 33 judges to be free idle thread in current thread set Idle thread returns to the pending user task for belonging to the current thread set.
In specific application scenarios, the thread management module 31 is also used to:
After the thread pool is divided into multiple sets of threads, minimum number of threads is set for each sets of threads With maximum thread amount;
The thread of minimum number of threads is created for each sets of threads;
Each sets of threads of thread of the initialization package containing the minimum number of threads.
Further,
The judgment module 33 is also used to judge when judging does not have idle thread in the current thread set in institute State executed in current thread set operation user task quantity whether be greater than the thread management module 31 be the thread collection Close the maximum thread amount of setting;
The processing module 34 is also used to work as when the judging result of the judgment module 33 is to be not greater than described A thread is created in preceding sets of threads, the pending user task for belonging to the current thread set uses, or in institute State judgment module 33 judging result be greater than when, according to preset thread Substitution Rules, judge its in the thread pool Whether thread can be obtained for the pending user task in his sets of threads, if it is then the thread that will acquire The pending user task is returned to, if it is not possible, then returning to abnormal prompt message.
In specific application scenarios, the locating module 32 is specifically used for:
The ID of currently received each pending user task is obtained respectively;
Respectively according to the ID of each pending user task to the thread management module 31 institute in the thread pool The sets of threads number of division carries out modulus;
The sets of threads that each pending user task is belonged to is determined respectively according to corresponding modulus result.
By the technical solution proposed using the embodiment of the present application, solves the prior art and concurrently obtained by thread pool When obtaining thread, carry out locking judge whether thread is idle, be lined up and obtain thread, so as to cause thread low efficiency is obtained Under problem.When thread of the technical solution that the embodiment of the present application is proposed in application program acquisition thread pool executes operation, It does not need to be locked, has reduced the contention for resources between task and task to the greatest extent, improve the thread for obtaining execution task Efficiency, and then enhance the retractility and robustness of application program.
Wherein, the modules of the application device can integrate in one, can also be deployed separately.Above-mentioned module can close And be a module, multiple submodule can also be further split into.
Through the above description of the embodiments, those skilled in the art can be understood that the application can be by Software adds the mode of required general hardware platform to realize, naturally it is also possible to which by hardware, but in many cases, the former is more Good embodiment.Based on this understanding, the technical solution of the application substantially in other words contributes to the prior art Part can be embodied in the form of software products, which is stored in a storage medium, if including Dry instruction is used so that a computer equipment (can be personal computer, server or the network equipment etc.) executes this Shen It please method described in each embodiment.It will be appreciated by those skilled in the art that attached drawing is the schematic diagram of a preferred embodiment, Module or process in attached drawing are not necessarily implemented necessary to the application.It will be appreciated by those skilled in the art that in embodiment Device in module can according to embodiment describe be distributed in the device of embodiment, corresponding change position can also be carried out In the one or more devices for being different from the present embodiment.The module of above-described embodiment can be merged into a module, can also be with It is further split into multiple submodule.Above-mentioned the embodiment of the present application serial number is for illustration only, does not represent the advantages or disadvantages of the embodiments.With Several specific embodiments of upper disclosed only the application, still, the application is not limited to this, any those skilled in the art Member can think of variation should all fall into the protection scope of the application.

Claims (6)

1. a kind of thread acquisition methods, which is characterized in that the described method includes:
In application server starting, thread pool is divided into multiple sets of threads;
When the one or more pending user tasks of confirmation, which need to obtain thread from the thread pool, to carry out executing operation, point The sets of threads that each pending user task is belonged to is not determined;
Whether there is free idle thread in each sets of threads determined by judging respectively;
When judging is free idle thread in current thread set, the idle thread is returned to and belongs to the current thread collection The pending user task closed;
It is described application server starting when, thread pool is divided into after multiple sets of threads, further includes:
For each sets of threads, minimum number of threads and maximum thread amount are set;
The thread of minimum number of threads is created for each sets of threads;
Each sets of threads of thread of the initialization package containing the minimum number of threads;
It is described judge respectively determined by whether be free idle thread in each sets of threads after, further includes:
When judging does not have idle thread in current thread set, the user that operation is executed in the current thread set is judged The maximum thread amount whether task quantity is greater than for sets of threads setting;
If be not greater than, a thread is created in the current thread set, for belonging to the current thread set Pending user task use;
If it is greater, then according to preset thread Substitution Rules, judge in other sets of threads in the thread pool whether Thread can be obtained for the pending user task, if it is then the thread that will acquire return to it is described pending User task, if it is not possible, then returning to abnormal prompt message.
2. thread acquisition methods as described in claim 1, which is characterized in that it is described according to preset thread Substitution Rules, sentence Whether thread can be obtained for the pending user task in other sets of threads broken in the thread pool, if can With the thread that then will acquire returns to the pending user task, if it is not possible, then returning to abnormal prompt message, has Body are as follows:
A replacement sets of threads is selected in the thread pool according to preset sequence or at random;
If including currently idle thread in the replacement sets of threads, the idle thread is returned into the pending use Family task, and record sets of threads belonging to the idle thread used in the pending user task and its in the line The position of Cheng Chizhong;
If currently without comprising idle thread, judging whether the replacement sets of threads can be in the replacement sets of threads New thread is created, if it is then the thread that creation is new, and the new thread is returned into the pending user and is appointed Business, and record sets of threads belonging to the new thread used in the pending user task and its in the thread pool In position;
If the replacement sets of threads cannot create new thread, continue that next replacement sets of threads is selected to attempt to find Idle thread creates new thread to the pending user task;
When all sets of threads in the thread pool can not provide idle thread all for the pending task or create newly When thread, abnormal prompt message is returned.
3. thread acquisition methods as claimed in claim 2, which is characterized in that described to judge that the replacement sets of threads whether To create new thread, specifically:
According to the current thread quantity in the replacement sets of threads and for each sets of threads, maximum thread amount is set, Judge whether the replacement sets of threads can create new thread;
When the current thread quantity in the replacement sets of threads is less than, and maximum thread amount is arranged for each sets of threads, Then determine that the replacement sets of threads can create new thread;
When the current thread quantity in the replacement sets of threads is equal to or more than that maximum thread is arranged for each sets of threads When quantity, it is determined that the replacement sets of threads cannot create new thread.
4. thread acquisition methods as described in claim 1, which is characterized in that described to determine each pending user task institute respectively The sets of threads of ownership, specifically includes:
The ID of currently received each pending user task is obtained respectively;
The sets of threads number divided in the thread pool is taken according to the ID of each pending user task respectively Mould;
The sets of threads that each pending user task is belonged to is determined respectively according to corresponding modulus result.
5. a kind of thread obtains equipment characterized by comprising
Thread management module, in application server starting, thread pool to be divided into multiple sets of threads;
Locating module, for when confirm user task need from the thread pool obtain thread carry out execute operation when, in institute It states in each sets of threads that thread management module is divided, determines the sets of threads that each pending user task is belonged to respectively;
Judgment module, for judging whether there is free idle thread in each sets of threads determined by the locating module respectively;
Processing module, for when the judgment module, which judges, is free idle thread in current thread set, by the idle thread Return to the pending user task for belonging to the current thread set;
The thread management module, is also used to:
After the thread pool is divided into multiple sets of threads, for each sets of threads setting minimum number of threads and most Big number of threads;
The thread of minimum number of threads is created for each sets of threads;
Each sets of threads of thread of the initialization package containing the minimum number of threads;
The judgment module is also used to when judging does not have idle thread in the current thread set, is judged described current It is sets of threads setting that whether the user task quantity that operation is executed in sets of threads, which is greater than the thread management module, Maximum thread amount;
The processing module is also used to when the judging result of the judgment module is to be not greater than, in the current thread collection A thread is created in conjunction, the pending user task for belonging to the current thread set uses, or in the judgement mould The judging result of block be greater than when, according to preset thread Substitution Rules, judge other sets of threads in the thread pool In whether can obtain thread for the pending user task, if it is then the thread that will acquire return to it is described Pending user task, if it is not possible, then returning to abnormal prompt message.
6. thread as claimed in claim 5 obtains equipment, which is characterized in that the locating module is specifically used for:
The ID of currently received each pending user task is obtained respectively;
The thread management module is divided in the thread pool according to the ID of each pending user task respectively Sets of threads number carries out modulus;
The sets of threads that each pending user task is belonged to is determined respectively according to corresponding modulus result.
CN201510583969.0A 2015-09-14 2015-09-14 A kind of thread acquisition methods and equipment Active CN106528065B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201510583969.0A CN106528065B (en) 2015-09-14 2015-09-14 A kind of thread acquisition methods and equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201510583969.0A CN106528065B (en) 2015-09-14 2015-09-14 A kind of thread acquisition methods and equipment

Publications (2)

Publication Number Publication Date
CN106528065A CN106528065A (en) 2017-03-22
CN106528065B true CN106528065B (en) 2019-11-08

Family

ID=58348469

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201510583969.0A Active CN106528065B (en) 2015-09-14 2015-09-14 A kind of thread acquisition methods and equipment

Country Status (1)

Country Link
CN (1) CN106528065B (en)

Families Citing this family (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107766137A (en) * 2017-10-16 2018-03-06 北京京东尚科信息技术有限公司 A kind of task processing method and device
CN108846632A (en) * 2018-05-28 2018-11-20 浙江口碑网络技术有限公司 Thread processing method and device
CN109857547A (en) * 2019-01-04 2019-06-07 平安科技(深圳)有限公司 A kind of thread distribution method, device and terminal device
CN112395054B (en) * 2019-08-15 2024-07-02 杭州海康威视系统技术有限公司 Thread scheduling method, device and system
CN111782293A (en) * 2020-06-28 2020-10-16 珠海豹趣科技有限公司 Task processing method and device, electronic equipment and readable storage medium
CN112433837A (en) * 2020-11-24 2021-03-02 上海浦东发展银行股份有限公司 Asynchronous resource processing method and system based on enterprise service bus

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101364185A (en) * 2008-09-02 2009-02-11 中国科学院软件研究所 Thread pool capacity adaptive adjustment method and application server concurrency control method
CN101882089A (en) * 2009-05-07 2010-11-10 中兴通讯股份有限公司 Method for processing business conversational application with multi-thread and device thereof
CN102147746A (en) * 2010-03-05 2011-08-10 微软公司 Dynamic thread pool management
CN102360310A (en) * 2011-09-28 2012-02-22 中国电子科技集团公司第二十八研究所 Multitask process monitoring method and system in distributed system environment
CN102591721A (en) * 2011-12-30 2012-07-18 北京新媒传信科技有限公司 Method and system for distributing thread execution task

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7703101B2 (en) * 2004-02-13 2010-04-20 International Business Machines Corporation Autonomic workload classification using predictive assertion for wait queue and thread pool selection
US9250953B2 (en) * 2013-11-12 2016-02-02 Oxide Interactive Llc Organizing tasks by a hierarchical task scheduler for execution in a multi-threaded processing system

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101364185A (en) * 2008-09-02 2009-02-11 中国科学院软件研究所 Thread pool capacity adaptive adjustment method and application server concurrency control method
CN101882089A (en) * 2009-05-07 2010-11-10 中兴通讯股份有限公司 Method for processing business conversational application with multi-thread and device thereof
CN102147746A (en) * 2010-03-05 2011-08-10 微软公司 Dynamic thread pool management
CN102360310A (en) * 2011-09-28 2012-02-22 中国电子科技集团公司第二十八研究所 Multitask process monitoring method and system in distributed system environment
CN102591721A (en) * 2011-12-30 2012-07-18 北京新媒传信科技有限公司 Method and system for distributing thread execution task

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
MySQL threadpool(优先队列)介绍及性能测试;gpfeng;《http://www.gpfeng.com/?p=540》;20140313;第1页 *

Also Published As

Publication number Publication date
CN106528065A (en) 2017-03-22

Similar Documents

Publication Publication Date Title
CN106528065B (en) A kind of thread acquisition methods and equipment
CN106802826B (en) Service processing method and device based on thread pool
KR101600129B1 (en) Application efficiency engine
US20120317371A1 (en) Usage Aware NUMA Process Scheduling
CN107273331A (en) A kind of heterogeneous computing system and method based on CPU+GPU+FPGA frameworks
US20120271952A1 (en) Microprocessor with software control over allocation of shared resources among multiple virtual servers
CN103605568A (en) Multithread management method and device
CN105760234A (en) Thread pool management method and device
CN105808328A (en) Task scheduling method, device and system
CN106033373A (en) A virtual machine resource scheduling method and scheduling system in a cloud computing platform
CN109739627B (en) Task scheduling method, electronic device and medium
CN111638953B (en) Method, device and storage medium for realizing GPU virtualization
CN109324983A (en) A kind of method, storage medium, equipment and the system of automatic cleaning cache file
CN104932933A (en) Spin lock acquisition method and apparatus
Maruf et al. Extending resources for avoiding overloads of mixed‐criticality tasks in cyber‐physical systems
US8458719B2 (en) Storage management in a data processing system
CN107038482A (en) Applied to AI algorithm engineerings, the Distributed Architecture of systematization
CN108519987A (en) A kind of data persistence method and apparatus
CN111143063A (en) Task resource reservation method and device
CN113626151B (en) Container cloud log collection resource control method and system
CN103959276A (en) Resource allocation prioritization based on knowledge of user intent and process independence
CN106528551A (en) Memory application method and apparatus
CN116069480B (en) Processor and computing device
US9348667B2 (en) Apparatus for managing application program and method therefor
CN108287762B (en) Distributed computing interactive mode use resource optimization method and computer equipment

Legal Events

Date Code Title Description
C06 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
TR01 Transfer of patent right

Effective date of registration: 20201012

Address after: Cayman Enterprise Centre, 27 Hospital Road, George Town, Grand Cayman Islands

Patentee after: Innovative advanced technology Co.,Ltd.

Address before: Cayman Enterprise Centre, 27 Hospital Road, George Town, Grand Cayman Islands

Patentee before: Advanced innovation technology Co.,Ltd.

Effective date of registration: 20201012

Address after: Cayman Enterprise Centre, 27 Hospital Road, George Town, Grand Cayman Islands

Patentee after: Advanced innovation technology Co.,Ltd.

Address before: A four-storey 847 mailbox in Grand Cayman Capital Building, British Cayman Islands

Patentee before: Alibaba Group Holding Ltd.

TR01 Transfer of patent right