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.