Disclosure of Invention
The invention provides a method and a system for distributing a thread execution task, which aim to solve the problems that no execution result is fed back, and the application is not convenient enough.
The invention discloses a method for distributing a thread to execute tasks, which comprises the following steps:
step 1, creating a thread pool;
step 2, creating a task and adding the task into a task queue;
step 3, calling idle threads in the thread pool, and distributing tasks in the task queue to the idle threads;
and 4, after the thread executes the task, returning an execution result to the task.
Wherein the creating task further comprises:
step 21, adding result object parameters and multicast entrusting objects containing task operations into the task;
the step 4 further comprises the following steps:
and step 22, after the thread executes the task, giving an execution result to a result object parameter.
Wherein, after the task is created, the method further comprises:
step 31, setting priority for the created task;
the adding the task into the task queue further comprises:
step 32, adding the created tasks into a task queue according to the sequence of the priorities from high to low;
the step 3 further comprises:
and step 33, calling idle threads in the thread pool, sequentially extracting tasks by taking the head of the task queue as a starting point, and distributing the extracted tasks to the idle threads.
Wherein, the creating the thread pool further comprises:
step 41, executing a thread in a thread pool, where the thread is used for task allocation, and the thread is called an allocation thread;
after the task is added into the task queue, the method further comprises the following steps:
step 42, waking up the distribution thread to distribute tasks;
the step 3 further comprises:
and 43, the allocation thread calls an idle thread in the thread pool, the tasks in the task queue are allocated to the idle thread, and the allocation thread is dormant after the allocation is finished.
Wherein the step 1 further comprises:
step 51, creating threads according to a preset initial thread number, wherein the threads form a thread pool;
said step 43 further comprises:
step 52, allocating threads to search idle threads in the thread pool;
step 53, if an idle thread exists in the thread pool, calling the idle thread, extracting a task from the task queue, distributing the task to the idle thread, and distributing thread dormancy after distribution is finished; otherwise, step 52 is executed after allocating the thread sleep preset duration.
Wherein, the step 52 and the step 53 further comprise:
step 61, determining the number of created threads according to the search result and the number of tasks in the current task queue;
and 62, if the number is not 0, creating the threads according to the number, adding the created threads into the thread pool, and enabling the newly created threads to be idle.
Wherein the method further comprises periodically deleting redundant threads in the thread pool,
step 71, allocating threads to periodically determine the number of redundant threads in a thread pool;
and 72, deleting idle threads according to the number of the redundant threads, and releasing resources occupied by the deleted threads.
Wherein the step 71 further comprises:
step 81A, determining the number of idle threads needed in a thread pool according to the adding time of the last task sequenced in a task queue, the current time, the average number of processing tasks in unit time of threads, the number of tasks in the current task queue and a preset task waiting time threshold;
step 82A, adding the required idle thread number and the currently occupied thread number to obtain a sum value, taking the larger value of the initial thread number and the sum value, and taking the difference value of the current thread number and the larger value in a thread pool as the redundant thread number;
or,
the step 61 further comprises:
step 81B, determining the number of idle threads needed in the thread pool according to the adding time of the last task sequenced in the task queue, the current time, the average number of processing tasks in unit time of the thread, the number of tasks in the current task queue and a preset task waiting time threshold;
and step 82B, adding the required idle thread number and the thread number occupied currently to obtain a sum value, and taking the smaller value of the preset maximum thread number and the sum value, wherein the difference value of the smaller value and the thread number in the current thread pool is the number of the created threads.
Wherein, after the step 43, the method further comprises:
step 91, after the thread executes the task, waking up the distribution thread;
step 92, the distribution thread searches whether a task exists in the task queue, if so, step 43 is executed, otherwise, the distribution thread is dormant;
or,
step 43 is followed by:
step 93, after the thread executes the task, the thread searches whether a task exists in the task queue, if so, the first task in the task queue is extracted, and the task is executed;
or,
step 43 is followed by:
step 94, the allocation thread is awakened periodically, the allocation thread searches whether there is a task in the task queue, if yes, step 43 is executed, otherwise, the allocation thread sleeps.
The invention also discloses a system for distributing the thread to execute the task, which comprises:
the thread pool creating module is used for creating a thread pool;
the task adding module is used for creating a task and adding the task into a task queue;
the task allocation module is used for calling idle threads in the thread pool and allocating tasks in the task queue to the idle threads;
and the result returning module is used for returning the execution result to the task after the thread executes the task.
The embodiment of the invention has the beneficial effects that: the application is more convenient and faster by feeding back the execution result; by executing tasks according to priority, thread scheduling is more intelligent; through dynamic thread quantity adjustment, the threads are more flexibly used, and waste of system resources is reduced.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, embodiments of the present invention will be described in detail with reference to the accompanying drawings.
Referring to fig. 1, a flow chart of a method for allocating threads to execute tasks according to the present invention is shown.
A method of distributing threads to perform tasks, the method comprising:
step S100, a thread pool is created.
And step S200, creating a task and adding the task into a task queue.
And step S300, calling idle threads in the thread pool, and distributing the tasks in the task queue to the idle threads.
And step S400, after the thread executes the task, returning an execution result to the task.
Further, the creating task further comprises adding result object parameters and a multicast delegate object containing task operations to the task.
The step S400 further includes assigning the execution result to the result object parameter after the thread executes the task.
In a preferred embodiment, threads are invoked to perform tasks at a priority level.
After the task is created, the method further comprises the following steps:
step S210A, sets priorities for the created tasks.
The adding the task into the task queue further comprises:
step S220A, adding the created tasks into the task queue in order of priority from high to low.
The step S300 further includes:
step S310A, call an idle thread in the thread pool, sequentially extract tasks from the head of the task queue, and allocate the extracted tasks to the idle thread.
When the number of idle threads is more than or equal to the number of tasks in the task queue, extracting all tasks in the task queue; and when the number of the idle threads is less than that of the tasks in the task queue, sequentially extracting the tasks with the same number as the idle threads from the head of the task queue.
In a preferred embodiment, thread allocation is performed using an allocation thread.
After the thread pool is created, the method further comprises the following steps:
step S110B, a thread in the thread pool is executed, where the thread is used for task allocation, and the thread is called an allocation thread.
After the task is added into the task queue, the method further comprises the following steps:
in step S210B, the allocation thread is awakened to perform task allocation.
The step S300 further includes:
step S310B, the allocation thread calls an idle thread in the thread pool, allocates the task in the task queue to the idle thread, and allocates the thread to sleep after the allocation is finished.
When the thread is called by the allocated thread, the state is changed to occupied, and after the task is completed, the state is changed to idle.
Further, after a round of task execution is completed, whether tasks waiting to be executed still exist in the task queue is checked.
Viewing is achieved through a variety of implementations. For example, after a thread executes a task, the thread is waken up to be allocated; the allocation thread looks up whether there is a task in the task queue, if so, step S310B is executed, otherwise, the allocation thread sleeps.
Or after the thread executes the task, the thread searches whether the task exists in the task queue, if so, the first task in the task queue is extracted, and the task is executed.
Alternatively, the allocation thread is awakened periodically, the allocation thread searches whether there is a task in the task queue, if so, step S310B is executed, otherwise, the allocation thread sleeps.
By adopting the method, the distributed threads can be enabled to have dormancy, and the tasks in the task queue can be ensured to be extracted and executed in time.
In a preferred embodiment, the thread number in the thread pool adopts a fixed mode, and the fixed mode is suitable for the case that the task amount is relatively fixed.
The step S100 further includes:
step S110C, creating threads according to the preset initial number of threads, and the threads form a thread pool.
The allocation thread calls an idle thread in the thread pool, allocates the task in the task queue to the idle thread, and the allocation thread dormancy further comprises the following steps.
In step S310C, the allocated thread looks up an idle thread in the thread pool.
Step S320C, if an idle thread exists in the thread pool, calling the idle thread, extracting a task from the task queue, distributing the task to the idle thread, and distributing thread dormancy after distribution is finished; otherwise, step S310C is executed after the thread sleep is allocated for the preset duration.
In a preferred embodiment, the thread number in the thread pool adopts a dynamic mode, and the dynamic mode is suitable for the situation that the task amount changes relatively greatly.
The step S100 further includes:
step S110D, creating threads according to the preset initial number of threads, and the threads form a thread pool.
The initial number of threads is the minimum number of threads in the thread pool.
The allocation thread calls an idle thread in the thread pool, allocates the tasks in the task queue to the idle thread, and after allocation is finished, the allocation thread dormancy further comprises the following steps.
In step S311D, the allocated thread finds an idle thread in the thread pool.
Step S312D, determining the number of created threads according to the search result and the number of tasks in the current task queue.
The method I is to compare the number of the tasks in the task queue with the number of the found idle threads, if the number of the found idle threads is small, the difference value is obtained by subtracting the number of the tasks in the task queue from the number of the idle threads, and the difference value is the number of the created threads, otherwise, the number of the created threads is 0.
The configuration also comprises the maximum thread quantity, and the thread quantity in the thread pool is greater than or equal to the minimum quantity and less than or equal to the maximum thread quantity.
Firstly, judging whether the number of the current threads in the thread pool is the maximum number of the threads, if so, the number of the created threads is 0, and if not, judging as follows.
And comparing the number of the tasks in the task queue with the number of the found idle threads, if the number of the found idle threads is less, subtracting the number of the tasks in the task queue from the number of the idle threads to obtain a difference value, wherein the difference value is the number of the threads needing to be increased, otherwise, the number of the created threads is 0.
And judging whether the sum of the number of the existing threads and the number of the threads needing to be increased is larger than or equal to the maximum number of the threads, if so, determining that the difference value between the maximum number of the threads and the number of the existing threads is the number of the created threads, otherwise, determining that the number of the threads needing to be increased is the number of the created threads.
The configuration further includes the maximum thread number, and the thread number in the thread pool is greater than or equal to the minimum number and less than or equal to the maximum thread number.
And determining the number of idle threads required in the thread pool according to the adding time of the last task sequenced in the task queue, the current time, the average number of processing tasks in the thread unit time, the number of tasks in the current task queue and a preset task waiting time threshold.
And adding the required idle thread number and the thread number occupied currently to obtain a sum value, taking the smaller value of the preset maximum thread number and the sum value, and taking the difference value of the smaller value and the thread number in the current thread pool as the number of the created threads.
The specific operation is as follows.
Firstly, judging whether the number of the current threads in the thread pool is the maximum number of the threads, if so, the number of the created threads is 0, and if not, judging as follows.
And comparing the number of the tasks in the task queue with the number of the found idle threads, wherein if the number of the found idle threads is more than or equal to the number of the tasks in the task queue, the number of the created threads is 0, otherwise, the number of the created threads is determined according to the following method.
The number of idle threads required is determined as follows.
t=t0+T-t1,
n=N/(v×t)
Wherein T0 is the adding time of the last task in the task queue, T is the preset task waiting time threshold, T1 is the current time, N is the number of idle threads required, N is the number of tasks in the current task queue, and v is the average number of processing tasks per unit time of the threads.
And adding the number of the currently occupied threads and the number of the required idle threads to obtain a sum value.
And comparing the sum value with the maximum thread number, wherein if the sum value is larger, the number of the created threads is the difference value of the subtraction between the maximum thread number and the thread number in the current thread pool, otherwise, the number of the created threads is the difference value of the subtraction between the sum value and the thread number in the current thread pool.
In step S313D, if the number is not 0, creating a thread according to the number, and adding the created thread to the thread pool, the newly created thread is idle.
Step S314D, if an idle thread exists in the thread pool, extracting the task from the task queue, distributing the task to the idle thread, and after the distribution is finished, distributing thread dormancy; otherwise, step S312D is executed after the thread sleep is allocated for the preset duration.
In a preferred embodiment, the thread pool is periodically determined whether the thread number is excessive, a certain number of threads are deleted, and resources are released to reduce resource consumption.
The method also includes periodically deleting redundant threads in the thread pool as follows.
In step S510E, the allocation thread periodically determines the number of excess threads in the thread pool.
The number of excess threads is determined in a number of ways.
Comparing the number of the tasks in the task queue with the number of the idle threads, if the number of the idle threads is large, subtracting the number of the tasks in the task queue from the number of the idle threads to obtain a difference value, wherein the difference value is the number of the redundant threads, and otherwise, the number of the redundant threads is 0.
In the second mode, firstly, whether the number of the current threads in the thread pool is the initial thread number is judged, if yes, the number of the redundant threads is 0, and if not, the following judgment is carried out.
And comparing the number of the tasks in the task queue with the number of the idle threads, if the number of the tasks is less, subtracting the number of the idle threads from the number of the tasks to obtain a difference value, wherein the difference value is the number of the threads needing to be deleted, and otherwise, the number of the redundant threads is 0.
And judging whether the difference between the number of the existing threads and the number of the threads needing to be deleted is less than or equal to the initial number of the threads, if so, determining that the difference between the number of the existing threads and the initial number of the threads is the number of redundant threads, otherwise, determining that the number of the threads needing to be deleted is the number of the redundant threads.
And determining the number of idle threads required in the thread pool according to the adding time of the last task sequenced in the task queue, the current time, the average number of processing tasks in the thread unit time, the number of tasks in the current task queue and a preset task waiting time threshold. And adding the required idle thread number and the currently occupied thread number to obtain a sum value, taking the larger value of the initial thread number and the sum value, and taking the difference value of the current thread number and the larger value in the thread pool as the redundant thread number.
For example, whether the number of current threads in the thread pool is the initial thread number is judged, if yes, the number of redundant threads is 0, and if not, the following judgment is performed.
And comparing the number of the tasks in the task queue with the number of the idle threads, wherein if the number of the idle threads is less than or equal to the number of the tasks in the task queue, the number of the redundant threads is 0, otherwise, the number of the redundant threads is determined according to the following method.
The number of idle threads required is determined as follows.
t=t0+T-t1,
n=N/(v×t)
Wherein T0 is the time of last adding task in the task queue, T is the preset task waiting time threshold, T1 is the current time, N is the number of required idle threads, N is the number of tasks in the current task queue, and v is the average number of processing tasks in thread unit time.
And adding the number of the threads occupied currently and the number of the idle threads needed to obtain a sum value, comparing the sum value with the initial thread number, wherein if the initial thread number is larger, the number of the redundant threads is the difference value of the subtraction of the current thread number and the initial thread number, and otherwise, the number of the redundant threads is the difference value of the subtraction of the current thread number and the sum value.
Step S520E, deleting idle threads according to the number of the redundant threads, and releasing resources occupied by the deleted threads.
Referring to FIG. 2, a flow diagram of an embodiment method for allocating threads to perform tasks is provided. The dynamic mode is employed in this embodiment.
Step S201, creating threads according to a preset initial number of threads, where the threads form a thread pool, and executing one thread in the thread pool, where the thread is used for task allocation.
The threads are referred to as allocation threads.
And creating a thread object and adding the thread into the thread pool. The semaphore notification is in a blocked state when the thread has no task to execute, and the state of the thread is identified as idle. When the calling thread executes the task, the state of the thread is marked as occupied, a semaphore is sent to the thread to inform the thread, the thread starts executing, the thread transmits an execution result or an exception to a task result object parameter, the state of the thread is marked as idle, and the next calling is waited. The user can obtain a return result through the result object parameters, and the result is normal execution or abnormal interruption.
The thread pool has the following functions: determining the number of tasks in the current task queue; determining the number of idle threads in a current thread pool; checking whether a task still exists in the task queue; and releasing the resources of the deleted thread.
Step S202, creating tasks, setting priorities for the created tasks, inserting the created tasks into a task queue according to the sequence of the priorities from high to low, and waking up an allocation thread to perform task allocation.
When creating a task, the task receives a multicast entrusting object and result object parameters, and the multicast entrusting object is a method comprising task operation logic. The user gets the result of the execution of the operation from the result object.
Step S203, the allocation thread searches for an idle thread in the thread pool.
And step S204, the distribution thread determines the number of the created threads according to the search result and the number of the tasks in the current task queue.
Step S205, the threads are allocated for judgment, if the number is not 0, the threads are created according to the number, the created threads are added into the thread pool, and the newly created threads are idle.
Step S206, the thread is allocated for judgment, if an idle thread exists in the thread pool, step S208 is executed, otherwise, step S207 is executed.
In step S207, the thread sleep preset duration is allocated and then step S203 is executed.
And S208, taking the head of the task queue as a starting point by the distributing thread, sequentially extracting the tasks, distributing the extracted tasks to idle threads, and after the distribution is finished, enabling the distributing thread to be dormant.
Step S209, the thread executes the task, and after the execution is finished, the execution result is given to the result object parameter, and the distribution thread is waken up.
Step S210, the distribution thread searches whether there is a task in the task queue, if yes, step S203 is executed, otherwise, step S211 is executed.
In step S211, the assigned thread sleeps.
In this embodiment, the allocation thread also periodically determines the number of redundant threads in the thread pool; and deleting idle threads according to the number of the redundant threads, and releasing resources occupied by the deleted threads.
Referring to FIG. 3, a block diagram of a system for assigning threads to perform tasks in accordance with the present invention is shown.
The system comprises:
a threadpool creation module 100 for creating a thread pool.
And thetask adding module 200 is used for creating a task and adding the task into the task queue.
And thetask allocation module 300 is configured to invoke an idle thread in the thread pool, and allocate a task in the task queue to the idle thread.
And aresult returning module 400, configured to return an execution result to the task after the thread executes the task.
Further, the task adding module is further used for adding the result object parameters and the multicast entrusting object containing the task operation into the task when the task is created;
the result returning module is further used for endowing the execution result to the result object parameter after the thread executes the task.
Further, the task adding module is also used for setting priority for the created task after the task is created;
the task adding module is further used for adding the created tasks into the task queue according to the sequence of the priorities from high to low when the tasks are added into the task queue;
the task allocation module is further used for calling idle threads in the thread pool, sequentially extracting tasks by taking the head of the task queue as a starting point, and allocating the extracted tasks to the idle threads.
Further, the task allocation module is further configured to execute a thread in the thread pool after the thread pool creation module creates the thread pool, where the thread is used for task allocation, and the thread is called an allocation thread;
the task adding module is also used for awakening an allocation thread to perform task allocation after adding the task into the task queue;
the task allocation module is further used for calling idle threads in the thread pool through the allocation threads, allocating the tasks in the task queue to the idle threads, and enabling the allocation threads to be dormant after the allocation is finished.
Further, the thread pool creating module is further configured to create threads according to a preset initial thread number, where the threads form a thread pool;
the task allocation module is used for calling idle threads in the thread pool through the allocation threads, allocating the tasks in the task queue to the idle threads, and further searching the idle threads in the thread pool through the allocation threads when the allocation threads are dormant after the allocation is finished; if an idle thread exists in the thread pool, extracting a task from the task queue, distributing the task to the idle thread, and sleeping the distributed thread after the distribution is finished; otherwise, the thread is allocated to be dormant for a preset time length and then the search is executed.
Further, the task allocation module is further configured to determine the number of created threads according to the search result and the number of tasks in the current task queue after searching and before judging whether an idle thread exists in the thread pool; and if the number is not 0, creating the threads according to the number, adding the created threads into the thread pool, and enabling the newly created threads to be idle.
Further, the system also comprises a thread deleting module used for periodically determining the number of redundant threads in the thread pool by allocating the threads; and deleting idle threads according to the number of the redundant threads, and releasing resources occupied by the deleted threads.
The thread deleting module is used for determining the number of idle threads needed in the thread pool according to the adding time of the last task sequenced in the task queue, the current time, the average number of processing tasks in unit time of the thread, the number of tasks in the current task queue and a preset task waiting time threshold when the number of the redundant threads in the thread pool is determined by the distributed threads; and adding the required idle thread number and the currently occupied thread number to obtain a sum value, taking the larger value of the initial thread number and the sum value, and taking the difference value of the current thread number and the larger value in the thread pool as the redundant thread number.
Furthermore, the result returning module is also used for awakening the distribution thread after the thread executes the task;
the task allocation module is also used for searching whether a task exists in the task queue through the allocation thread after the allocation thread is awakened, if so, calling an idle thread in the thread pool, allocating the task in the task queue to the idle thread, and sleeping the allocation thread, otherwise, directly sleeping the allocation thread.
Further, the result returning module is further configured to, after the thread executes the task, find whether there is a task in the task queue through the thread, if there is a task in the task queue, extract a first task in the task queue, and execute the task through the thread.
Further, the task allocation module is further configured to wake up the allocation thread periodically, find whether there is a task in the task queue through the allocation thread, if there is a task in the task queue, call an idle thread in the thread pool, allocate the task in the task queue to the idle thread, and sleep the allocation thread, otherwise, directly sleep the allocation thread.
The above description is only for the preferred embodiment of the present invention, and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention shall fall within the protection scope of the present invention.