[go: up one dir, main page]

CN118519750A - Task processing method, device, computer equipment and storage medium - Google Patents

Task processing method, device, computer equipment and storage medium Download PDF

Info

Publication number
CN118519750A
CN118519750A CN202410728126.4A CN202410728126A CN118519750A CN 118519750 A CN118519750 A CN 118519750A CN 202410728126 A CN202410728126 A CN 202410728126A CN 118519750 A CN118519750 A CN 118519750A
Authority
CN
China
Prior art keywords
thread
task
threads
subtasks
target task
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202410728126.4A
Other languages
Chinese (zh)
Inventor
蒲兆楠
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Ping An Pay Electronic Payment Co.,Ltd.
Original Assignee
Pingan Payment Technology Service Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Pingan Payment Technology Service Co Ltd filed Critical Pingan Payment Technology Service Co Ltd
Priority to CN202410728126.4A priority Critical patent/CN118519750A/en
Publication of CN118519750A publication Critical patent/CN118519750A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • G06F9/4843Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
    • G06F9/4881Scheduling strategies for dispatcher, e.g. round robin, multi-level priority queues
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

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

Abstract

本申请涉及计算机技术领域,公开了一种任务处理方法、装置、计算机设备及存储介质,包括:若目标任务分配至线程池中第一线程的任务队列中,且目标任务满足分解条件,按照目标任务对应的分解策略对目标任务进行分解处理,得到多个子任务;调用线程池中的多个线程分别执行多个子任务,其中,多个线程包括第一线程;若分解策略包含有聚合标签,对多个子任务的执行结果进行聚合处理。本申请的方法,能够在计算密集型任务场景下,通过对目标任务的细化分解,实现多个线程并行执行目标任务,提升处理器使用效率,减少每个任务的执行时间,而且分解条件和分解策略支持自定义,满足不同场景执行任务的需求,扩大了应用范围。

The present application relates to the field of computer technology, and discloses a task processing method, apparatus, computer equipment and storage medium, including: if the target task is assigned to the task queue of the first thread in the thread pool, and the target task meets the decomposition condition, the target task is decomposed according to the decomposition strategy corresponding to the target task to obtain multiple subtasks; multiple threads in the thread pool are called to execute multiple subtasks respectively, wherein the multiple threads include the first thread; if the decomposition strategy includes an aggregation tag, the execution results of the multiple subtasks are aggregated. The method of the present application can realize the parallel execution of the target task by multiple threads in a computing-intensive task scenario by detailed decomposition of the target task, improve the processor utilization efficiency, and reduce the execution time of each task. Moreover, the decomposition conditions and decomposition strategies support customization, meet the needs of executing tasks in different scenarios, and expand the scope of application.

Description

任务处理方法、装置、计算机设备及存储介质Task processing method, device, computer equipment and storage medium

技术领域Technical Field

本申请涉及计算机技术领域,尤其是涉及到一种任务处理方法、装置、计算机设备及存储介质。The present application relates to the field of computer technology, and in particular to a task processing method, apparatus, computer equipment and storage medium.

背景技术Background Art

相关技术中,在使用线程执行任务时,通常对线程生命周期进行管理,这种线程池化管理方案,也即在收到新的任务时调用空闲的线程去处理,若当前没有闲置的线程则会将其放到等待队列中排队等待处理。但现有线程池化管理解决方案主要存在以下问题:In the related art, when using threads to execute tasks, the thread life cycle is usually managed. This thread pool management solution is to call an idle thread to process when a new task is received. If there is no idle thread at present, it will be placed in a waiting queue to wait for processing. However, the existing thread pool management solution has the following main problems:

1.单个任务由单个线程处理,若当前空闲线程多于任务数时,空闲线程无法缓解其他线程的处理压力,造成资源浪费;1. A single task is processed by a single thread. If there are more idle threads than tasks, the idle threads cannot relieve the processing pressure of other threads, resulting in a waste of resources.

2.不适用于计算密集型的任务,适用场景比较单一。2. It is not suitable for computing-intensive tasks and its applicable scenarios are relatively limited.

发明内容Summary of the invention

有鉴于此,本申请提供了一种任务处理方法、装置、计算机设备及存储介质,以解决线程执行任务时的资源浪费问题。In view of this, the present application provides a task processing method, apparatus, computer device and storage medium to solve the problem of resource waste when threads execute tasks.

第一方面,提供了一种任务处理方法,包括:In a first aspect, a task processing method is provided, comprising:

若目标任务分配至线程池中第一线程的任务队列中,且目标任务满足分解条件,按照目标任务对应的分解策略对目标任务进行分解处理,得到多个子任务;If the target task is assigned to the task queue of the first thread in the thread pool and the target task meets the decomposition condition, the target task is decomposed according to the decomposition strategy corresponding to the target task to obtain multiple subtasks;

调用线程池中的多个线程分别执行多个子任务,其中,多个线程包括第一线程;Calling multiple threads in the thread pool to respectively execute multiple subtasks, wherein the multiple threads include a first thread;

若分解策略包含有聚合标签,对多个子任务的执行结果进行聚合处理。If the decomposition strategy contains an aggregation tag, the execution results of multiple subtasks are aggregated.

进一步地,调用线程池中的多个线程分别执行多个子任务,包括:Furthermore, multiple threads in the calling thread pool respectively execute multiple subtasks, including:

调用第一线程执行第一子任务,并从第一线程的任务队列中删除第二子任务;Calling the first thread to execute the first subtask, and deleting the second subtask from the task queue of the first thread;

若处于空闲状态的第二线程的数量大于或等于第二子任务的数量,将第二子任务平均分配至处于空闲状态的第二线程的任务队列中,并调用第二线程执行第二子任务;If the number of the second threads in the idle state is greater than or equal to the number of the second subtasks, the second subtasks are evenly distributed to the task queues of the second threads in the idle state, and the second threads are called to execute the second subtasks;

其中,第二线程为线程池中除第一线程以外的线程,第一子任务为多个子任务中任一子任务,第二子任务为多个子任务中除第一子任务以外的子任务。The second thread is a thread in the thread pool other than the first thread, the first subtask is any subtask among the multiple subtasks, and the second subtask is a subtask among the multiple subtasks other than the first subtask.

进一步地,调用线程池中的多个线程分别执行多个子任务,包括:Furthermore, multiple threads in the calling thread pool respectively execute multiple subtasks, including:

若处于空闲状态的第二线程的数量小于第二子任务的数量,确定第二子任务的优先级;If the number of the second threads in the idle state is less than the number of the second subtasks, determining the priority of the second subtask;

按照优先级对第二子任务进行排序,得到第一顺序;Sort the second subtasks by priority to obtain the first order;

按照第一顺序将第二子任务分配至线程池中处于空闲状态的线程的任务队列中,并调用线程池中处于空闲状态的线程顺序执行第二子任务。The second subtask is assigned to the task queue of the thread in the idle state in the thread pool according to the first sequence, and the threads in the idle state in the thread pool are called to execute the second subtask sequentially.

进一步地,任务处理方法还包括:Furthermore, the task processing method also includes:

若线程池中线程的数量小于线程池的线程阈值,创建临时线程,其中,线程池中线程包括第一线程和第二线程;If the number of threads in the thread pool is less than the thread threshold of the thread pool, a temporary thread is created, wherein the threads in the thread pool include a first thread and a second thread;

调用临时线执行目标任务、第一子任务或第二子任务。The temporary line is called to execute the target task, the first subtask, or the second subtask.

进一步地,任务处理方法还包括:Furthermore, the task processing method also includes:

若临时线程处于空闲状态的时长大于或等于第一预设时长,删除临时线程。If the duration of the temporary thread being in the idle state is greater than or equal to the first preset duration, the temporary thread is deleted.

进一步地,任务处理方法还包括:Furthermore, the task processing method also includes:

若线程池中线程均处于工作状态,且线程池中线程的数量大于或等于线程池的线程阈值,确定线程池中线程的剩余工作时长;If all threads in the thread pool are in working state, and the number of threads in the thread pool is greater than or equal to the thread threshold of the thread pool, the remaining working time of the threads in the thread pool is determined;

若线程池中线程的剩余工作时长均大于第二预设时长,拒绝执行目标任务或将目标任务调度至其他线程池。If the remaining working time of the threads in the thread pool is greater than the second preset time, the target task is refused to be executed or the target task is scheduled to other thread pools.

进一步地,任务处理方法还包括:Furthermore, the task processing method also includes:

获取目标任务的业务场景;Obtain the business scenario of the target task;

按照业务场景匹配分解策略和分解条件;Match decomposition strategies and decomposition conditions according to business scenarios;

其中,分解策略包括以下至少一种:聚合标签、触发条件和分解规则;触发条件包括:执行目标任务之前对目标任务进行分解处理,或执行目标任务的过程中对目标任务进行分解处理。The decomposition strategy includes at least one of the following: aggregation tags, trigger conditions and decomposition rules; the trigger conditions include: decomposing the target task before executing the target task, or decomposing the target task during the execution of the target task.

第二方面,提供了一种任务处理装置,包括:In a second aspect, a task processing device is provided, comprising:

分解模块,用于若目标任务分配至线程池中第一线程的任务队列中,且目标任务满足分解条件,按照目标任务对应的分解策略对目标任务进行分解处理,得到多个子任务;A decomposition module is used for decomposing the target task according to the decomposition strategy corresponding to the target task to obtain multiple subtasks if the target task is assigned to the task queue of the first thread in the thread pool and the target task meets the decomposition condition;

执行模块,用于调用线程池中的多个线程分别执行多个子任务,其中,多个线程包括第一线程;An execution module, used for calling multiple threads in the thread pool to respectively execute multiple subtasks, wherein the multiple threads include a first thread;

聚合模块,用于若分解策略包含有聚合标签,对多个子任务的执行结果进行聚合处理。The aggregation module is used to aggregate the execution results of multiple subtasks if the decomposition strategy contains an aggregation tag.

进一步地,执行模块,具体用于调用第一线程执行第一子任务,并从第一线程的任务队列中删除第二子任务;若处于空闲状态的第二线程的数量大于或等于第二子任务的数量,将第二子任务平均分配至处于空闲状态的第二线程的任务队列中,并调用第二线程执行第二子任务;Further, the execution module is specifically used to call the first thread to execute the first subtask, and delete the second subtask from the task queue of the first thread; if the number of second threads in an idle state is greater than or equal to the number of second subtasks, evenly distribute the second subtasks to the task queues of the second threads in an idle state, and call the second thread to execute the second subtask;

其中,第二线程为线程池中除第一线程以外的线程,第一子任务为多个子任务中任一子任务,第二子任务为多个子任务中除第一子任务以外的子任务。The second thread is a thread in the thread pool other than the first thread, the first subtask is any subtask among the multiple subtasks, and the second subtask is a subtask among the multiple subtasks other than the first subtask.

进一步地,任务处理装置还包括:Furthermore, the task processing device also includes:

第一确定模块,用于若处于空闲状态的第二线程的数量小于第二子任务的数量,确定第二子任务的优先级;A first determining module, configured to determine the priority of the second subtask if the number of the second threads in an idle state is less than the number of the second subtasks;

排序模块,用于按照优先级对第二子任务进行排序,得到第一顺序;A sorting module, used for sorting the second subtasks according to the priority to obtain a first order;

执行模块,具体用于按照第一顺序将第二子任务分配至线程池中处于空闲状态的线程的任务队列中,并调用线程池中处于空闲状态的线程顺序执行第二子任务。The execution module is specifically used to distribute the second subtask to the task queue of the thread in the idle state in the thread pool according to the first order, and call the threads in the idle state in the thread pool to execute the second subtask sequentially.

进一步地,任务处理装置还包括:Furthermore, the task processing device also includes:

线程管理模块,用于若线程池中线程的数量小于线程池的线程阈值,创建临时线程,其中,线程池中线程包括第一线程和第二线程;A thread management module, used for creating a temporary thread if the number of threads in the thread pool is less than a thread threshold of the thread pool, wherein the threads in the thread pool include a first thread and a second thread;

执行模块,具体用于调用临时线执行目标任务、第一子任务或第二子任务。The execution module is specifically used to call the temporary line to execute the target task, the first subtask or the second subtask.

进一步地,线程管理模块,还用于若临时线程处于空闲状态的时长大于或等于第一预设时长,删除临时线程。Furthermore, the thread management module is further configured to delete the temporary thread if the duration for which the temporary thread is in an idle state is greater than or equal to a first preset duration.

进一步地,任务处理装置还包括:Furthermore, the task processing device also includes:

第二确定模块,用于若线程池中线程均处于工作状态,且线程池中线程的数量大于或等于线程池的线程阈值,确定线程池中线程的剩余工作时长;The second determination module is used to determine the remaining working time of the threads in the thread pool if all the threads in the thread pool are in a working state and the number of threads in the thread pool is greater than or equal to the thread threshold of the thread pool;

执行模块,还用于若线程池中线程的剩余工作时长均大于第二预设时长,拒绝执行目标任务;The execution module is further configured to refuse to execute the target task if the remaining working time of the threads in the thread pool is greater than a second preset time;

或者,or,

调度模块,用于若线程池中线程的剩余工作时长均大于第二预设时长,将目标任务调度至其他线程池。The scheduling module is used to schedule the target task to other thread pools if the remaining working time of the threads in the thread pool is greater than the second preset time.

进一步地,任务处理装置还包括:Furthermore, the task processing device also includes:

获取模块,用于获取目标任务的业务场景;The acquisition module is used to obtain the business scenario of the target task;

匹配模块,用于按照业务场景匹配分解策略和分解条件;A matching module is used to match decomposition strategies and decomposition conditions according to business scenarios;

其中,分解策略包括以下至少一种:聚合标签、触发条件和分解规则;;触发条件包括:执行目标任务之前对目标任务进行分解处理,或执行目标任务的过程中对目标任务进行分解处理。Among them, the decomposition strategy includes at least one of the following: aggregation tags, trigger conditions and decomposition rules; the trigger conditions include: decomposing the target task before executing the target task, or decomposing the target task during the execution of the target task.

第三方面,提供了一种计算机设备,包括存储器、处理器以及存储在存储器中并可在处理器上运行的计算机程序,处理器执行计算机程序时实现上述任务处理方法的步骤。In a third aspect, a computer device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above-mentioned task processing method when executing the computer program.

第四方面,提供了一种计算机可读存储介质,计算机可读存储介质存储有计算机程序,计算机程序被处理器执行时实现上述任务处理方法的步骤。In a fourth aspect, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the above-mentioned task processing method are implemented.

上述任务处理方法、装置、计算机设备及存储介质所实现的方案中,若检测到目标任务被分配至线程池中第一线程的任务队列中,也即处理器即将调用第一线程执行该目标任务。此时,若目标任务满足分解条件,在执行目标任务之前或执行目标任务过程中将较大的目标任务分解成较小的子任务,使得目标任务不在只由一个线程执行,可以由多个线程进行分解执行。在当目标任务对应的分解策略中存在聚合标签时,聚合多个子任务的执行结果,从而得到目标任务的执行结果。通过上述技术方案,一方面,能够在计算密集型任务场景下,通过对目标任务的细化分解,尽可能的将任务拆分,实现多个线程并行执行目标任务,大大提升处理器使用效率,减少每个任务的执行时间;再一方面,分解条件和分解策略支持用户自定义,支持多种不同场景执行任务的需求,扩大了任务处理方案的应用范围。In the scheme implemented by the above-mentioned task processing method, device, computer equipment and storage medium, if it is detected that the target task is assigned to the task queue of the first thread in the thread pool, that is, the processor is about to call the first thread to execute the target task. At this time, if the target task meets the decomposition condition, the larger target task is decomposed into smaller subtasks before or during the execution of the target task, so that the target task is no longer executed by only one thread, but can be decomposed and executed by multiple threads. When there is an aggregation tag in the decomposition strategy corresponding to the target task, the execution results of multiple subtasks are aggregated to obtain the execution result of the target task. Through the above-mentioned technical scheme, on the one hand, in a computing-intensive task scenario, by refining the decomposition of the target task, the task can be split as much as possible, and multiple threads can be realized to execute the target task in parallel, which greatly improves the processor utilization efficiency and reduces the execution time of each task; on the other hand, the decomposition conditions and decomposition strategies support user customization, support the needs of executing tasks in a variety of different scenarios, and expand the application scope of the task processing scheme.

上述说明仅是本申请技术方案的概述,为了能够更清楚了解本申请的技术手段,而可依照说明书的内容予以实施,并且为了让本申请的上述和其它目的、特征和优点能够更明显易懂,以下特举本申请的具体实施方式。The above description is only an overview of the technical solution of the present application. In order to more clearly understand the technical means of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below.

附图说明BRIEF DESCRIPTION OF THE DRAWINGS

为了更清楚地说明本申请实施例的技术方案,下面将对本申请实施例的描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本申请的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动性的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the description of the embodiments of the present application will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

图1是本申请中任务处理方法的流程示意图之一;FIG1 is a flowchart of a task processing method in the present application;

图2是本申请中任务处理方法的流程示意图之二;FIG2 is a second flow chart of the task processing method in the present application;

图3是本申请中任务处理方法的流程示意图之三;FIG3 is a third flowchart of the task processing method in the present application;

图4是本申请中任务处理方法的流程示意图之四;FIG4 is a fourth flowchart of the task processing method in the present application;

图5是本申请中任务处理装置的结构示意图;FIG5 is a schematic diagram of the structure of a task processing device in the present application;

图6是本申请中计算机设备的结构示意图。FIG. 6 is a schematic diagram of the structure of a computer device in the present application.

具体实施方式DETAILED DESCRIPTION

下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本申请一部分实施例,而不是全部的实施例。基于本申请中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本申请保护的范围。The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

请参阅图1所示,图1为本申请实施例提供的任务处理方法的一个流程示意图,包括如下步骤:Please refer to FIG. 1 , which is a flowchart of a task processing method provided in an embodiment of the present application, comprising the following steps:

步骤101,若目标任务分配至线程池中第一线程的任务队列中,且目标任务满足分解条件,按照目标任务对应的分解策略对目标任务进行分解处理,得到多个子任务;Step 101: if the target task is assigned to the task queue of the first thread in the thread pool and the target task meets the decomposition condition, the target task is decomposed according to the decomposition strategy corresponding to the target task to obtain multiple subtasks;

在该实施例中,当目标任务提交至线程池后,线程池判断当前是否存在空闲状态的线程。若不存在则将目标任务分配至等待任务队列中,以等待空闲状态的线程来获取并执行,若存在则将该空闲状态的线程作为可执行目标任务的第一线程。将目标任务分配至线程池中第一线程的任务队列后,判断目标任务是否满足分解条件,在目标任务满足分解条件,也即可分解该目标任务的情况下,按照对应的分解策略将目标任务分解成多个子任务,以便于使用不同的线程分别执行,从而实现多个子任务的并行处理,减少目标任务的执行时间,实现更高效的线程池管理。In this embodiment, after the target task is submitted to the thread pool, the thread pool determines whether there is a thread in an idle state. If not, the target task is assigned to the waiting task queue to be acquired and executed by the thread waiting for the idle state. If it exists, the thread in the idle state is used as the first thread that can execute the target task. After the target task is assigned to the task queue of the first thread in the thread pool, it is determined whether the target task meets the decomposition condition. If the target task meets the decomposition condition, that is, the target task can be decomposed, the target task is decomposed into multiple subtasks according to the corresponding decomposition strategy, so that different threads can be used to execute them respectively, thereby realizing parallel processing of multiple subtasks, reducing the execution time of the target task, and realizing more efficient thread pool management.

可以理解的是,步骤101,也即若目标任务满足分解条件,按照目标任务对应的分解策略对目标任务进行分解处理,得到多个子任务之前,该任务处理方法还包括:It can be understood that, before step 101, that is, if the target task meets the decomposition condition, the target task is decomposed according to the decomposition strategy corresponding to the target task to obtain multiple subtasks, the task processing method further includes:

步骤201,获取目标任务的业务场景;Step 201, obtaining the business scenario of the target task;

步骤202,按照业务场景匹配分解策略和分解条件。Step 202: Match the decomposition strategy and decomposition conditions according to the business scenario.

其中,分解条件可以是目标任务的资源占用量是否大于预设占用量,也可以是目标任务所属业务场景是否为指定场景或是否关联有分解策略等,本申请实施例不再一一例举。分解策略包括:聚合标签、触发条件和/或分解规则。聚合标签用于标记分解后的子任务是否需要进行结果聚合。触发条件包括执行目标任务之前对目标任务进行分解处理,或执行目标任务的过程中对目标任务进行分解处理。分解规则可以是按照目标任务的资源占用量比例划分、按照线程池空闲状态的线程数量划分等,本申请实施例不再一一例举。Among them, the decomposition condition can be whether the resource usage of the target task is greater than the preset usage, or whether the business scenario to which the target task belongs is a specified scenario or whether there is an associated decomposition strategy, etc., which will not be listed one by one in the embodiments of the present application. The decomposition strategy includes: aggregation tags, trigger conditions and/or decomposition rules. The aggregation tag is used to mark whether the decomposed subtasks need to be aggregated. The trigger condition includes decomposing the target task before executing the target task, or decomposing the target task during the execution of the target task. The decomposition rule can be divided according to the proportion of resource usage of the target task, divided according to the number of threads in the idle state of the thread pool, etc., which will not be listed one by one in the embodiments of the present application.

在该实施例中,分解条件和分解策略可由用户根据实际业务场景合理设置,也可以利用预先设置的对应关系或模型,利用目标任务的业务场景匹配所需的分解策略和分解条件。从而实现了分解条件和分解策略的个性化化配置,使得目标任务的分解方式更佳灵活,提升目标任务的细化分解效果,增加线程并行执行的几率,而且能够支持多种不同场景执行任务的需求,有利于扩大了任务处理方案的应用范围。In this embodiment, the decomposition conditions and decomposition strategies can be reasonably set by the user according to the actual business scenario, or the pre-set corresponding relationship or model can be used to match the required decomposition strategy and decomposition conditions with the business scenario of the target task. This realizes the personalized configuration of the decomposition conditions and decomposition strategies, makes the decomposition method of the target task more flexible, improves the detailed decomposition effect of the target task, increases the probability of parallel execution of threads, and can support the needs of executing tasks in a variety of different scenarios, which is conducive to expanding the application scope of the task processing solution.

步骤102,调用线程池中的多个线程分别执行多个子任务;Step 102, calling multiple threads in the thread pool to respectively execute multiple subtasks;

其中,线程池中的多个线程包括第一线程。The multiple threads in the thread pool include a first thread.

在该实施例中,目标任务被分解成多个子任务之后,利用线程池中的多个线程分别执行多个子任务,使得目标任务不在只由一个线程执行,可以由多个线程进行分解执行,从而实现多个线程并行执行目标任务,大大提升处理器使用效率,减少每个任务的执行时间。In this embodiment, after the target task is decomposed into multiple subtasks, multiple threads in the thread pool are used to execute the multiple subtasks respectively, so that the target task is no longer executed by only one thread, but can be decomposed and executed by multiple threads, thereby realizing multiple threads executing the target task in parallel, greatly improving the processor utilization efficiency and reducing the execution time of each task.

可以理解的是,若目标任务不满足分解条件,则不进行分解处理,直接调用第一线程执行目标任务。本实施例剔除的技术方案不仅支持可分解任务执行,也支持不可分解任务执行,并可以保证不可分解任务的高效执行,用户只需要关注任务逻辑,无需关注线程池内部原理,有利于提升开发效率,减少开发成本。It is understandable that if the target task does not meet the decomposition condition, no decomposition process is performed, and the first thread is directly called to execute the target task. The technical solution eliminated in this embodiment not only supports the execution of decomposable tasks, but also supports the execution of indecomposable tasks, and can ensure the efficient execution of indecomposable tasks. Users only need to pay attention to the task logic, without paying attention to the internal principles of the thread pool, which is conducive to improving development efficiency and reducing development costs.

在具体的应用场景中,如图2所示,步骤102,也即调用线程池中的多个线程分别执行多个子任务,具体包括如下步骤:In a specific application scenario, as shown in FIG2 , step 102, that is, calling multiple threads in the thread pool to respectively execute multiple subtasks, specifically includes the following steps:

步骤102-1,调用第一线程执行第一子任务,并从第一线程的任务队列中删除第二子任务;Step 102-1, calling the first thread to execute the first subtask, and deleting the second subtask from the task queue of the first thread;

步骤102-2,处于空闲状态的第二线程的数量大于或等于第二子任务的数量,若是,进入步骤102-3,若否,进入步骤102-4;Step 102-2: If the number of the second threads in the idle state is greater than or equal to the number of the second subtasks, then the process goes to step 102-3; if not, then the process goes to step 102-4;

其中,第二线程为线程池中除第一线程以外的线程,第一子任务为多个子任务中任一子任务,第二子任务为多个子任务中除第一子任务以外的子任务。The second thread is a thread in the thread pool other than the first thread, the first subtask is any subtask among the multiple subtasks, and the second subtask is a subtask among the multiple subtasks other than the first subtask.

步骤102-3,将第二子任务平均分配至第二线程的任务队列中,并调用第二线程执行第二子任务;Step 102-3, evenly distribute the second subtask to the task queue of the second thread, and call the second thread to execute the second subtask;

在该实施例中,使用目标任务已分配的第一线程执行多个子任务中任一子任务(第一子任务),并将多个子任务中除第一子任务以外的子任务(第二子任务)从第一线程中删除,以便于再次分配新的线程。若线程池中处于空闲状态的第二线程的数量大于或等于第二子任务的数量,说明线程池拥有足够多的空闲线程执行第二子任务。此时,将第二子任务平均添加至第二线程的任务队列中,调用至少一个处于空闲状态的第二线程分别执行至少一个第二子任务。由此,处理器仅保留一个子任务通过第一线程来执行,将其他子任务由线程池中其他第二线程执行,使得目标任务不在只由一个线程执行,可以由多个线程进行分解执行。从而实现多个线程并行执行目标任务,大大提升处理器使用效率,减少每个任务的执行时间,有效提高任务的执行效率,特别是在计算密集型任务的情况下,保证了目标任务的及时性。而且由于第二子任务被平均分配,能够避免多线程获取同一子任务时的线程同步开销,有利于减少无谓的资源消耗。In this embodiment, the first thread to which the target task has been assigned is used to execute any subtask (first subtask) among the multiple subtasks, and the subtasks (second subtasks) other than the first subtask among the multiple subtasks are deleted from the first thread, so as to facilitate the allocation of new threads again. If the number of second threads in the thread pool that are in an idle state is greater than or equal to the number of second subtasks, it means that the thread pool has enough idle threads to execute the second subtask. At this time, the second subtask is evenly added to the task queue of the second thread, and at least one second thread in an idle state is called to execute at least one second subtask respectively. Thus, the processor only retains one subtask to be executed by the first thread, and the other subtasks are executed by other second threads in the thread pool, so that the target task is no longer executed by only one thread, but can be decomposed and executed by multiple threads. Thereby, multiple threads are implemented to execute the target task in parallel, which greatly improves the processor utilization efficiency, reduces the execution time of each task, effectively improves the execution efficiency of the task, and ensures the timeliness of the target task, especially in the case of computationally intensive tasks. Moreover, since the second subtask is evenly distributed, the thread synchronization overhead when multiple threads obtain the same subtask can be avoided, which is conducive to reducing unnecessary resource consumption.

步骤102-4,确定第二子任务的优先级;Step 102-4, determining the priority of the second subtask;

具体地,可根据第二子任务的属性信息确定第二子任务的优先级。其中,属性信息包括以下至少一种:预计耗时、优先级标签、任务类别。例如,预计耗时越大优先级越高,或者属于第一类别的第二子任务优先级高于属于第二类别的第二子任务优先级等。Specifically, the priority of the second subtask can be determined according to the attribute information of the second subtask. The attribute information includes at least one of the following: estimated time, priority label, and task category. For example, the greater the estimated time, the higher the priority, or the priority of the second subtask belonging to the first category is higher than the priority of the second subtask belonging to the second category, etc.

步骤102-5,按照优先级对第二子任务进行排序,得到第一顺序;Step 102-5, sorting the second subtasks according to the priority to obtain a first order;

步骤102-6,按照第一顺序将第二子任务分配至线程池中处于空闲状态的线程的任务队列中,并调用线程池中处于空闲状态的线程顺序执行第二子任务。Step 102-6, assigning the second subtask to the task queue of the thread in the idle state in the thread pool according to the first order, and calling the threads in the idle state in the thread pool to execute the second subtask sequentially.

在该实施例中,若线程池中处于空闲状态的第二线程的数量大于或等于第二子任务的数量,说明处于空闲状态的第二线程无法支撑第二子任务并行执行。此时,确定每个第二子任务的优先级,并按照优先级对第二子任务进行排序。在按照第二子任务的第一顺序逐个将第二子任务分配至线程池中处于空闲状态的线程的任务队列中,并调用线程池中处于空闲状态的线程顺序执行第二子任务。不仅避免了多线程获取同一子任务时的线程同步开销的情况,而且先执行完子任务的线可以会继续执行其他子任务,从而使用尽可能更多的线程执行拆分后的子任务,在降低了线程池中线程的闲置率的同时,减少每个子任务的执行时间。In this embodiment, if the number of second threads in the thread pool that are in an idle state is greater than or equal to the number of second subtasks, it means that the second threads in the idle state cannot support the parallel execution of the second subtasks. At this time, the priority of each second subtask is determined, and the second subtasks are sorted according to the priority. The second subtasks are assigned to the task queues of the threads in the thread pool that are in an idle state one by one according to the first order of the second subtasks, and the threads in the thread pool that are in an idle state are called to execute the second subtasks in sequence. Not only is the thread synchronization overhead when multiple threads obtain the same subtask avoided, but the thread that first executes the subtask can continue to execute other subtasks, thereby using as many threads as possible to execute the split subtasks, while reducing the idle rate of threads in the thread pool, reducing the execution time of each subtask.

可以理解的是,线程池中处于空闲状态的线程可以是第一线程,也可以是第二线程,若仍然存在未执行的第二子任务,且第一线程已经结束对第一子任务的执行并重新恢复空闲状态,则可采用空闲的第一线程执行剩余未执行的第二子任务。同理,第二线程在已经结束对第二子任务的执行并重新恢复空闲状态后,可继续获取并执行剩余未执行的第二子任务。It is understandable that the thread in the thread pool in an idle state can be the first thread or the second thread. If there is still an unexecuted second subtask, and the first thread has finished executing the first subtask and resumed the idle state, the idle first thread can be used to execute the remaining unexecuted second subtask. Similarly, after the second thread has finished executing the second subtask and resumed the idle state, it can continue to obtain and execute the remaining unexecuted second subtask.

步骤103,若分解策略包含有聚合标签,对多个子任务的执行结果进行聚合处理。Step 103: If the decomposition strategy includes an aggregation tag, aggregation processing is performed on the execution results of the multiple subtasks.

在该实施例中,当目标任务对应的分解策略中存在聚合标签时,说明需要统筹多个子任务的执行结果,则在多个子任务均完成执行后,对多个子任务的执行结果进行聚合处理,以确定目标任务的执行结果,并输出该目标任务的执行结果,从而在完成目标任务的执行的基础上,保证执行结果的完整性,省去用户分别查看不同子任务执行结果的时间,使用户能够直观的了解整个目标任务的结果,保证了执行结果的输出便捷性。In this embodiment, when an aggregation tag exists in the decomposition strategy corresponding to the target task, it indicates that the execution results of multiple subtasks need to be coordinated. Then, after the execution of multiple subtasks is completed, the execution results of the multiple subtasks are aggregated to determine the execution result of the target task, and the execution result of the target task is output. Thus, on the basis of completing the execution of the target task, the integrity of the execution result is guaranteed, saving the user the time of viewing the execution results of different subtasks separately, allowing the user to intuitively understand the result of the entire target task, and ensuring the convenience of outputting the execution result.

在本申请的一些实施例中,如图3所示,提供了一种具体地实体对齐方案,任务处理方法还包括如下步骤:In some embodiments of the present application, as shown in FIG3 , a specific entity alignment solution is provided, and the task processing method further includes the following steps:

步骤301,若线程池中线程的数量小于线程池的线程阈值,创建临时线程;Step 301, if the number of threads in the thread pool is less than the thread threshold of the thread pool, create a temporary thread;

其中,线程池中线程包括第一线程和第二线程。临时线程是指线程池中可被回收的非自然核心线程。The threads in the thread pool include a first thread and a second thread. A temporary thread refers to a non-natural core thread in the thread pool that can be recycled.

步骤302,调用临时线执行目标任务、第一子任务或第二子任务;Step 302, calling a temporary line to execute the target task, the first subtask or the second subtask;

在该实施例中,若线程池中线程的数量小于线程池的线程阈值,也即当前线程池拥有的线程数量还未达到线程池允许的最大线程数量(线程阈值)。此时,可创建一些临时线程,以将临时线程作为第一线程或第二线程去执行目标任务、第一子任务或第二子任务。从而使线程池能够提供更多的空闲线程,并及时处理目标任务,优化线程池的线程管理,保证了多任务的独立运行,实现快速、高效地目标任务处理。In this embodiment, if the number of threads in the thread pool is less than the thread threshold of the thread pool, that is, the number of threads owned by the current thread pool has not reached the maximum number of threads allowed by the thread pool (thread threshold). At this time, some temporary threads can be created to use the temporary threads as the first thread or the second thread to execute the target task, the first subtask or the second subtask. This enables the thread pool to provide more idle threads, process the target task in a timely manner, optimize the thread management of the thread pool, ensure the independent operation of multiple tasks, and achieve fast and efficient target task processing.

值得一提的是,创建临时线程的触发条件还包括:当目标任务数量或多个子任务数量大于线程池的核心线程数量时,创建临时线程,或者目标任务数量或多个子任务数量大于核心线程数量与指定任务队列数量之和时,线程池认为现有的核心线程能够及时处理任务,此是创建新的临时线程。It is worth mentioning that the triggering conditions for creating temporary threads also include: when the number of target tasks or multiple subtasks is greater than the number of core threads of the thread pool, a temporary thread is created; or when the number of target tasks or multiple subtasks is greater than the sum of the number of core threads and the number of specified task queues, the thread pool believes that the existing core threads can process the tasks in time, and this is to create a new temporary thread.

步骤303,若临时线程处于空闲状态的时长大于或等于第一预设时长,删除临时线程。Step 303: If the duration of the temporary thread being in the idle state is greater than or equal to the first preset duration, the temporary thread is deleted.

在该实施例中,若临时线程处于空闲状态的时长大于或等于第一预设时长,说明临时线程已长时间未被使用,则及时回收该临时线程,释放其占用的资源,从而防止线程资源浪费。In this embodiment, if the duration of the temporary thread being in the idle state is greater than or equal to the first preset duration, indicating that the temporary thread has not been used for a long time, the temporary thread is promptly recycled to release the resources occupied by it, thereby preventing thread resource waste.

在本申请的一些实施例中,如图4所示,提供了一种具体地实体对齐方案,任务处理方法还包括如下步骤:In some embodiments of the present application, as shown in FIG4 , a specific entity alignment solution is provided, and the task processing method further includes the following steps:

步骤401,若线程池中线程均处于工作状态,且线程池中线程的数量大于或等于线程池的线程阈值,确定线程池中线程的剩余工作时长;Step 401, if all threads in the thread pool are in working state, and the number of threads in the thread pool is greater than or equal to the thread threshold of the thread pool, determine the remaining working time of the threads in the thread pool;

步骤402,若线程池中线程的剩余工作时长均大于第二预设时长,拒绝执行目标任务或将目标任务调度至其他线程池。Step 402: If the remaining working time of the threads in the thread pool is greater than the second preset time, the target task is refused to be executed or the target task is scheduled to other thread pools.

在该实施例中,当线程池中所有的线程均处于工作状态,也即没有空闲线程去执行当前的目标任务,且线程池中线程的数量大于或等于线程池的线程阈值,也即线程池已没有空间创建新的线程,此时,确定线程池中每个线程的剩余工作时长。若线程池中每个线程的剩余工作时长均大于第二预设时长,说明短时间内不会释放出空闲线程执行当前的目标任务,则拒绝执行目标任务,并提示用户目标任务执行失败,以便于用户及时采取响应补救措施;或者将目标任务调度至其他线程池,以供其他线程池对目标任务进行处理,保证目标任务能够顺利执行。In this embodiment, when all threads in the thread pool are in working state, that is, there is no idle thread to execute the current target task, and the number of threads in the thread pool is greater than or equal to the thread threshold of the thread pool, that is, the thread pool has no space to create new threads, at this time, the remaining working time of each thread in the thread pool is determined. If the remaining working time of each thread in the thread pool is greater than the second preset time, it means that no idle thread will be released in a short time to execute the current target task, then the target task is refused to be executed, and the user is prompted that the target task execution fails, so that the user can take timely response remedial measures; or the target task is scheduled to other thread pools for other thread pools to process the target task, so as to ensure that the target task can be executed smoothly.

在一个具体实施例中,提供一种任务处理方法,具体包括:In a specific embodiment, a task processing method is provided, which specifically includes:

1.创建线程池或使用已存在的线程池,并设置相关参数,参数包括:初始核心线程数,最大线程数(线程阈值),等待队列,拒绝策略,空闲线程存活时间(第一预设时长),线程工厂等。1. Create a thread pool or use an existing thread pool and set relevant parameters, including: initial number of core threads, maximum number of threads (thread threshold), waiting queue, rejection policy, idle thread survival time (first preset duration), thread factory, etc.

2.根据用户需要创建可分解任务或者普通任务(不可分解任务),可分解任务可实现分解逻辑和任务执行逻辑,普通任务只需要实现执行逻辑。2. Create decomposable tasks or ordinary tasks (non-decomposable tasks) according to user needs. Decomposable tasks can implement decomposition logic and task execution logic, while ordinary tasks only need to implement execution logic.

3.将目标任务提交至线程池。3. Submit the target task to the thread pool.

4.线程池判断当前是否存在空闲线程,若存在则直接使用空闲线程执行目标任务。4. The thread pool determines whether there is an idle thread at present. If so, the idle thread is used directly to execute the target task.

具体地,空闲线程获取目标任务开始执行,判断目标任务是否是可分解任务,也即目标任是否存在执行分解任务前分解策略且满足分解任务条件。Specifically, the idle thread obtains the target task and starts to execute it, and determines whether the target task is a decomposable task, that is, whether the target task has a decomposition strategy before executing the decomposition task and meets the decomposition task conditions.

如果是可分解任务,则执行分解任务流程,将分解后的N个任务封装成新的N个子任务,留下一个子任务由当前线程执行,并将其他分解的子任务加入分解任务队列;分解队列中的任务由其他线程执行;判断分解后的子任务执行结果是否需要聚合,若是则等待其他线程执行完分解后的子任务后聚合结果;若否则该线程可继续从分解任务队列中获取其他分解后的子任务并执行。If it is a decomposable task, the decomposition task process is executed, the N decomposed tasks are encapsulated into N new subtasks, one subtask is left to be executed by the current thread, and the other decomposed subtasks are added to the decomposition task queue; the tasks in the decomposition queue are executed by other threads; it is determined whether the execution results of the decomposed subtasks need to be aggregated. If so, wait for other threads to complete the execution of the decomposed subtasks and then aggregate the results; if not, the thread can continue to obtain other decomposed subtasks from the decomposition task queue and execute them.

如果是普通任务,则直接利用空闲线程执行该普通任务。If it is a common task, the idle thread is directly used to execute the common task.

执行完成后判断等待任务队列中是否还有任务,如果有则从中获取一个任务,并执行该任务。After the execution is completed, determine whether there are any tasks in the waiting task queue. If so, get a task from it and execute it.

5.若不存在空闲线程则将目标任务放到线程池的等待任务队列中。5. If there is no idle thread, put the target task into the waiting task queue of the thread pool.

6.判断等待任务队列是否已满,如果没有满,则将目标任务入列,并等待其他空闲线程来获取并执行。6. Determine whether the waiting task queue is full. If not, queue the target task and wait for other idle threads to obtain and execute it.

7.如果等待任务队列已满,则判断当前线程数是否小于最大线程数,如果小于则通过线程创建工厂创建新的线程执行任务,任务执行完毕后,新创建的线程变为空闲线程并开始等待下发新任务,若超过空闲线程存活时间,则将其销毁。7. If the waiting task queue is full, determine whether the current number of threads is less than the maximum number of threads. If it is less, create a new thread through the thread creation factory to execute the task. After the task is completed, the newly created thread becomes an idle thread and starts waiting for new tasks to be issued. If the idle thread survival time is exceeded, it will be destroyed.

8.若判断当前线程数已经到达最大线程数,则将目标任务派发给拒绝任务模块,拒绝任务模块执行对应拒绝策略拒绝该目标任务。8. If it is determined that the current number of threads has reached the maximum number of threads, the target task is dispatched to the rejection task module, and the rejection task module executes the corresponding rejection strategy to reject the target task.

在该实施例中,一方面,在计算密集型任务场景下,支持任务按一定策略进行分解,通过对任务的细化分解,尽可能的将任务拆分,将大任务分解成小任务,任务不在只由一个线程执行,可以由多个线程进行分解执行,让更多的线程执行拆分后的小任务,提升CPU使用效率,减少每个任务的执行时间。再一方面,分解任务的策略支持使用者自定义,让使用者根据实际场景定制化分解策略,并支持在任务执行过程中对任务进行分解,具备更灵活的分解任务策略,最大程度的将任务细化分解,增加其他线程帮助执行的几率,支持更多的使用场景。又一方面,同时具备执行可分解任务的功能和单一任务的功能,可以支持不同场景执行任务的需求,具有泛用性,用户不需要关注其内部逻辑和线程池内部原理,提升开发效率,减少开发成本。In this embodiment, on the one hand, in the scenario of computationally intensive tasks, tasks are supported to be decomposed according to a certain strategy. By decomposing the tasks in detail, tasks are split as much as possible, and large tasks are decomposed into small tasks. Tasks are no longer executed by only one thread, but can be decomposed and executed by multiple threads, allowing more threads to execute the split small tasks, improving CPU usage efficiency and reducing the execution time of each task. On the other hand, the strategy for decomposing tasks supports user customization, allowing users to customize the decomposition strategy according to actual scenarios, and supports decomposing tasks during task execution. It has a more flexible decomposition task strategy, which can decompose tasks to the greatest extent, increase the probability of other threads helping to execute, and support more usage scenarios. On the other hand, it has the function of executing decomposable tasks and the function of single tasks at the same time, can support the needs of executing tasks in different scenarios, has versatility, and users do not need to pay attention to its internal logic and the internal principles of the thread pool, improves development efficiency, and reduces development costs.

应理解,上述实施例中各步骤的序号的大小并不意味着执行顺序的先后,各过程的执行顺序应以其功能和内在逻辑确定,而不应对本申请实施例的实施过程构成任何限定。It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.

在一实施例中,提供一种任务处理装置,该任务处理装置与上述实施例中任务处理方法一一对应。如图5所示,该任务处理装置包括分解模块501、执行模块502、聚合模块503。各功能模块详细说明如下:In one embodiment, a task processing device is provided, which corresponds to the task processing method in the above embodiment. As shown in FIG5 , the task processing device includes a decomposition module 501, an execution module 502, and an aggregation module 503. Each functional module is described in detail as follows:

分解模块501,用于若目标任务分配至线程池中第一线程的任务队列中,且目标任务满足分解条件,按照目标任务对应的分解策略对目标任务进行分解处理,得到多个子任务;A decomposition module 501 is used for decomposing the target task according to the decomposition strategy corresponding to the target task to obtain multiple subtasks if the target task is assigned to the task queue of the first thread in the thread pool and the target task meets the decomposition condition;

执行模块502,用于调用线程池中的多个线程分别执行多个子任务,其中,多个线程包括第一线程;An execution module 502 is used to call multiple threads in a thread pool to respectively execute multiple subtasks, wherein the multiple threads include a first thread;

聚合模块503,用于若分解策略包含有聚合标签,对多个子任务的执行结果进行聚合处理。The aggregation module 503 is used to aggregate the execution results of multiple subtasks if the decomposition strategy includes an aggregation tag.

本申请提供了一种任务处理装置,若检测到目标任务被分配至线程池中第一线程的任务队列中,也即处理器即将调用第一线程执行该目标任务。此时,若目标任务满足分解条件,在执行目标任务之前或执行目标任务过程中将较大的目标任务分解成较小的子任务,使得目标任务不在只由一个线程执行,可以由多个线程进行分解执行。在当目标任务对应的分解策略中存在聚合标签时,聚合多个子任务的执行结果,从而得到目标任务的执行结果。通过上述技术方案,一方面,能够在计算密集型任务场景下,通过对目标任务的细化分解,尽可能的将任务拆分,实现多个线程并行执行目标任务,大大提升处理器使用效率,减少每个任务的执行时间;再一方面,分解条件和分解策略支持用户自定义,支持多种不同场景执行任务的需求,扩大了任务处理方案的应用范围。The present application provides a task processing device. If it is detected that the target task is assigned to the task queue of the first thread in the thread pool, that is, the processor is about to call the first thread to execute the target task. At this time, if the target task meets the decomposition condition, the larger target task is decomposed into smaller subtasks before or during the execution of the target task, so that the target task is no longer executed by only one thread, but can be decomposed and executed by multiple threads. When there is an aggregation tag in the decomposition strategy corresponding to the target task, the execution results of multiple subtasks are aggregated to obtain the execution result of the target task. Through the above technical solution, on the one hand, in a computationally intensive task scenario, by detailed decomposition of the target task, the task can be split as much as possible, and multiple threads can be used to execute the target task in parallel, which greatly improves the processor utilization efficiency and reduces the execution time of each task; on the other hand, the decomposition conditions and decomposition strategies support user customization, support the needs of executing tasks in a variety of different scenarios, and expand the application scope of the task processing solution.

在一实施例中,执行模块502,具体用于调用第一线程执行第一子任务,并从第一线程的任务队列中删除第二子任务;若处于空闲状态的第二线程的数量大于或等于第二子任务的数量,将第二子任务平均分配至处于空闲状态的第二线程的任务队列中,并调用第二线程执行第二子任务;In one embodiment, the execution module 502 is specifically configured to call the first thread to execute the first subtask, and delete the second subtask from the task queue of the first thread; if the number of the second threads in the idle state is greater than or equal to the number of the second subtasks, evenly distribute the second subtasks to the task queues of the second threads in the idle state, and call the second thread to execute the second subtask;

其中,第二线程为线程池中除第一线程以外的线程,第一子任务为多个子任务中任一子任务,第二子任务为多个子任务中除第一子任务以外的子任务。The second thread is a thread in the thread pool other than the first thread, the first subtask is any subtask among the multiple subtasks, and the second subtask is a subtask among the multiple subtasks other than the first subtask.

在一实施例中,任务处理装置还包括:第一确定模块(图中未示出)和排序模块(图中未示出)。In one embodiment, the task processing device further includes: a first determination module (not shown in the figure) and a sorting module (not shown in the figure).

具体地,第一确定模块,用于若处于空闲状态的第二线程的数量小于第二子任务的数量,确定第二子任务的优先级;Specifically, the first determination module is used to determine the priority of the second subtask if the number of the second threads in the idle state is less than the number of the second subtasks;

排序模块,用于按照优先级对第二子任务进行排序,得到第一顺序;A sorting module, used for sorting the second subtasks according to the priority to obtain a first order;

执行模块502,具体用于按照第一顺序将第二子任务分配至线程池中处于空闲状态的线程的任务队列中,并调用线程池中处于空闲状态的线程顺序执行第二子任务。The execution module 502 is specifically configured to assign the second subtask to the task queue of the thread in the idle state in the thread pool according to the first sequence, and call the threads in the idle state in the thread pool to execute the second subtask sequentially.

在一实施例中,任务处理装置还包括:线程管理模块(图中未示出)。In one embodiment, the task processing device further includes: a thread management module (not shown in the figure).

具体地,线程管理模块,用于若线程池中线程的数量小于线程池的线程阈值,创建临时线程,其中,线程池中线程包括第一线程和第二线程;Specifically, the thread management module is used to create a temporary thread if the number of threads in the thread pool is less than the thread threshold of the thread pool, wherein the threads in the thread pool include a first thread and a second thread;

执行模块502,具体用于调用临时线执行目标任务、第一子任务或第二子任务。The execution module 502 is specifically used to call the temporary line to execute the target task, the first subtask or the second subtask.

在一实施例中,线程管理模块,还用于若临时线程处于空闲状态的时长大于或等于第一预设时长,删除临时线程。In one embodiment, the thread management module is further configured to delete the temporary thread if the duration for which the temporary thread is in an idle state is greater than or equal to a first preset duration.

在一实施例中,任务处理装置还包括:第一确定模块(图中未示出)和调度模块(图中未示出)。In one embodiment, the task processing device further includes: a first determination module (not shown in the figure) and a scheduling module (not shown in the figure).

具体地,第二确定模块,用于若线程池中线程均处于工作状态,且线程池中线程的数量大于或等于线程池的线程阈值,确定线程池中线程的剩余工作时长;Specifically, the second determination module is used to determine the remaining working time of the threads in the thread pool if all threads in the thread pool are in a working state and the number of threads in the thread pool is greater than or equal to the thread threshold of the thread pool;

执行模块502,还用于若线程池中线程的剩余工作时长均大于第二预设时长,拒绝执行目标任务;或者,调度模块,用于若线程池中线程的剩余工作时长均大于第二预设时长,将目标任务调度至其他线程池。The execution module 502 is also used to refuse to execute the target task if the remaining working time of the threads in the thread pool is greater than the second preset time; or, the scheduling module is used to schedule the target task to other thread pools if the remaining working time of the threads in the thread pool is greater than the second preset time.

在一实施例中,任务处理装置还包括:获取模块(图中未示出)和匹配模块(图中未示出)。In one embodiment, the task processing device further includes: an acquisition module (not shown in the figure) and a matching module (not shown in the figure).

具体地,获取模块,用于获取目标任务的业务场景;Specifically, the acquisition module is used to obtain the business scenario of the target task;

匹配模块,用于按照业务场景匹配分解策略和分解条件;A matching module is used to match decomposition strategies and decomposition conditions according to business scenarios;

其中,分解策略包括以下至少一种:聚合标签、触发条件和分解规则;;触发条件包括:执行目标任务之前对目标任务进行分解处理,或执行目标任务的过程中对目标任务进行分解处理。Among them, the decomposition strategy includes at least one of the following: aggregation tags, trigger conditions and decomposition rules; the trigger conditions include: decomposing the target task before executing the target task, or decomposing the target task during the execution of the target task.

关于任务处理装置的具体限定可以参见上文中对于任务处理方法的限定,在此不再赘述。上述任务处理装置中的各个模块可全部或部分通过软件、硬件及其组合来实现。上述各模块可以硬件形式内嵌于或独立于计算机设备中的处理器中,也可以以软件形式存储于计算机设备中的存储器中,以便于处理器调用执行以上各个模块对应的操作。For the specific definition of the task processing device, please refer to the definition of the task processing method above, which will not be repeated here. Each module in the above-mentioned task processing device can be implemented in whole or in part by software, hardware and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.

在一个实施例中,提供了一种计算机设备,包括存储器、处理器及存储在存储器上并可在处理器上运行的计算机程序,该计算机程序被处理器执行时以实现一种任务处理方法的功能或步骤。In one embodiment, a computer device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program implements functions or steps of a task processing method when executed by the processor.

在一个实施例中,提供了一种计算机设备,该计算机设备可以是客户端,其内部结构图如图6所示。该计算机设备包括通过系统总线连接的处理器、存储器、网络接口、显示屏和输入装置。其中,该计算机设备的处理器用于提供计算和控制能力。该计算机设备的存储器包括非易失性存储介质、内存储器。该非易失性存储介质存储有操作系统和计算机程序。该内存储器为非易失性存储介质中的操作系统和计算机程序的运行提供环境。该计算机设备的网络接口用于与外部服务器通过网络连接通信。处理器执行计算机程序时实现以下步骤:若目标任务分配至线程池中第一线程的任务队列中,且目标任务满足分解条件,按照目标任务对应的分解策略对目标任务进行分解处理,得到多个子任务;调用线程池中的多个线程分别执行多个子任务,其中,多个线程包括第一线程;若分解策略包含有聚合标签,对多个子任务的执行结果进行聚合处理。In one embodiment, a computer device is provided, which can be a client, and its internal structure diagram is shown in Figure 6. The computer device includes a processor, a memory, a network interface, a display screen and an input device connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external server through a network connection. When the processor executes the computer program, the following steps are implemented: if the target task is assigned to the task queue of the first thread in the thread pool, and the target task meets the decomposition condition, the target task is decomposed according to the decomposition strategy corresponding to the target task to obtain multiple subtasks; multiple threads in the thread pool are called to execute multiple subtasks respectively, wherein the multiple threads include the first thread; if the decomposition strategy contains an aggregation tag, the execution results of the multiple subtasks are aggregated.

在一个实施例中,提供了一种计算机可读存储介质,其上存储有计算机程序,计算机程序被处理器执行时实现以下步骤:若目标任务分配至线程池中第一线程的任务队列中,且目标任务满足分解条件,按照目标任务对应的分解策略对目标任务进行分解处理,得到多个子任务;调用线程池中的多个线程分别执行多个子任务,其中,多个线程包括第一线程;若分解策略包含有聚合标签,对多个子任务的执行结果进行聚合处理。In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the following steps are implemented: if a target task is assigned to a task queue of a first thread in a thread pool, and the target task meets a decomposition condition, the target task is decomposed according to a decomposition strategy corresponding to the target task to obtain a plurality of subtasks; a plurality of threads in the thread pool are called to respectively execute the plurality of subtasks, wherein the plurality of threads include the first thread; if the decomposition strategy includes an aggregation tag, the execution results of the plurality of subtasks are aggregated.

需要说明的是,上述关于计算机可读存储介质或计算机设备所能实现的功能或步骤,可对应参阅前述方法实施例中,任务处理方法的相关描述,为避免重复,这里不再一一描述。It should be noted that the above functions or steps that can be implemented by the computer-readable storage medium or computer device can refer to the relevant description of the task processing method in the aforementioned method embodiment. In order to avoid repetition, they will not be described one by one here.

本领域普通技术人员可以理解实现上述实施例方法中的全部或部分流程,是可以通过计算机程序来指令相关的硬件来完成,所述的计算机程序可存储于一非易失性计算机可读取存储介质中,该计算机程序在执行时,可包括如上述各方法的实施例的流程。其中,本申请所提供的各实施例中所使用的对存储器、存储、数据库或其它介质的任何引用,均可包括非易失性和/或易失性存储器。非易失性存储器可包括只读存储器(ROM)、可编程ROM(PROM)、电可编程ROM(EPROM)、电可擦除可编程ROM(EEPROM)或闪存。易失性存储器可包括随机存取存储器(RAM)或者外部高速缓冲存储器。作为说明而非局限,RAM以多种形式可得,诸如静态RAM(SRAM)、动态RAM(DRAM)、同步DRAM(SDRAM)、双数据率SDRAM(DDRSDRAM)、增强型SDRAM(ESDRAM)、同步链路(Synchlink)DRAM(SLDRAM)、存储器总线(Rambus)直接RAM(RDRAM)、直接存储器总线动态RAM(DRDRAM)、以及存储器总线动态RAM(RDRAM)等。Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and/or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).

所属领域的技术人员可以清楚地了解到,为了描述的方便和简洁,仅以上述各功能单元、模块的划分进行举例说明,实际应用中,可以根据需要而将上述功能分配由不同的功能单元、模块完成,即将所述装置的内部结构划分成不同的功能单元或模块,以完成以上描述的全部或者部分功能。Those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example. In actual applications, the above-mentioned functions can be distributed and completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.

以上所述实施例仅用以说明本发明的技术方案,而非对其限制,尽管参照前述实施例对本发明进行了详细的说明,本领域的普通技术人员应当理解,其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本发明各实施例技术方案的精神和范围,均应包含在本发明的保护范围之内。The embodiments described above are only used to illustrate the technical solutions of the present invention rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein; and these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be included in the protection scope of the present invention.

Claims (10)

1. A method of task processing, comprising:
If the target task is distributed to a task queue of a first thread in a thread pool and the target task meets a decomposition condition, decomposing the target task according to a decomposition strategy corresponding to the target task to obtain a plurality of subtasks;
invoking a plurality of threads in the thread pool to respectively execute the plurality of subtasks, wherein the plurality of threads comprise the first thread;
And if the decomposition strategy comprises an aggregation label, performing aggregation processing on the execution results of the plurality of subtasks.
2. The task processing method according to claim 1, wherein the invoking the plurality of threads in the thread pool to execute the plurality of subtasks, respectively, comprises:
Invoking the first thread Cheng Zhihang first subtask and deleting a second subtask from the first thread's task queue;
If the number of the second threads in the idle state is greater than or equal to the number of the second subtasks, the second subtasks are evenly distributed to a task queue of the second threads in the idle state, and the second threads are called to execute the second subtasks;
The second thread is a thread in the thread pool except for the first thread, the first subtask is any one of the plurality of subtasks, and the second subtask is a subtask in the plurality of subtasks except for the first subtask.
3. The task processing method according to claim 2, wherein the invoking the plurality of threads in the thread pool to execute the plurality of subtasks, respectively, includes:
If the number of the second threads in the idle state is smaller than the number of the second subtasks, determining the priority of the second subtasks;
sequencing the second subtasks according to the priority to obtain a first sequence;
and distributing the second subtasks to task queues of the threads in the idle state in the thread pool according to the first sequence, and calling the threads in the idle state in the thread pool to execute the second subtasks in sequence.
4. The task processing method according to claim 2, characterized in that the method further comprises:
if the number of threads in the thread pool is smaller than the thread threshold of the thread pool, creating temporary threads, wherein the threads in the thread pool comprise the first thread and the second thread;
and calling the temporary line to execute the target task, the first subtask or the second subtask.
5. The task processing method according to claim 4, characterized in that the method further comprises:
And deleting the temporary thread if the time length of the temporary thread in the idle state is greater than or equal to a first preset time length.
6. A task processing method according to any one of claims 1 to 5, characterized in that the method further comprises:
if the threads in the thread pool are in a working state and the number of threads in the thread pool is larger than or equal to the thread threshold of the thread pool, determining the residual working time length of the threads in the thread pool;
and if the residual working time length of the threads in the thread pool is greater than the second preset time length, refusing to execute the target task or scheduling the target task to other thread pools.
7. A task processing method according to any one of claims 1 to 5, characterized in that the method further comprises:
acquiring a service scene of the target task;
Matching the decomposition strategy and the decomposition conditions according to the service scene;
Wherein the decomposition policy comprises at least one of: the aggregation tag, the triggering condition and the decomposition rule, wherein the triggering condition comprises: and decomposing the target task before executing the target task, or decomposing the target task in the process of executing the target task.
8. A task processing device, comprising:
The decomposing module is used for decomposing the target task according to a decomposing strategy corresponding to the target task to obtain a plurality of subtasks if the target task is distributed to a task queue of a first thread in the thread pool and the target task meets the decomposing condition;
an execution module, configured to invoke a plurality of threads in the thread pool to execute the plurality of subtasks respectively, where the plurality of threads includes the first thread;
and the aggregation module is used for carrying out aggregation processing on the execution results of the plurality of subtasks if the decomposition strategy comprises an aggregation label.
9. Computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that the processor implements the steps of the task processing method according to any of claims 1 to 7 when the computer program is executed.
10. A computer-readable storage medium storing a computer program, characterized in that the computer program when executed by a processor implements the steps of the task processing method according to any one of claims 1 to 7.
CN202410728126.4A 2024-06-05 2024-06-05 Task processing method, device, computer equipment and storage medium Pending CN118519750A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202410728126.4A CN118519750A (en) 2024-06-05 2024-06-05 Task processing method, device, computer equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202410728126.4A CN118519750A (en) 2024-06-05 2024-06-05 Task processing method, device, computer equipment and storage medium

Publications (1)

Publication Number Publication Date
CN118519750A true CN118519750A (en) 2024-08-20

Family

ID=92281092

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202410728126.4A Pending CN118519750A (en) 2024-06-05 2024-06-05 Task processing method, device, computer equipment and storage medium

Country Status (1)

Country Link
CN (1) CN118519750A (en)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN119356830A (en) * 2024-12-25 2025-01-24 苏州元脑智能科技有限公司 Method and device for executing computing tasks, storage medium and electronic device
CN119668699A (en) * 2024-11-28 2025-03-21 杭州迈冲科技有限公司 A boot acceleration method and device based on Android system
CN120560866A (en) * 2025-07-31 2025-08-29 北京科杰科技有限公司 Parallel computing performance optimization method, device, equipment, and medium

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN119668699A (en) * 2024-11-28 2025-03-21 杭州迈冲科技有限公司 A boot acceleration method and device based on Android system
CN119356830A (en) * 2024-12-25 2025-01-24 苏州元脑智能科技有限公司 Method and device for executing computing tasks, storage medium and electronic device
CN120560866A (en) * 2025-07-31 2025-08-29 北京科杰科技有限公司 Parallel computing performance optimization method, device, equipment, and medium

Similar Documents

Publication Publication Date Title
CN118519750A (en) Task processing method, device, computer equipment and storage medium
CN110351384A (en) Big data platform method for managing resource, device, equipment and readable storage medium storing program for executing
CN110597858A (en) Task data processing method, device, computer equipment and storage medium
US8239872B2 (en) Method and system for controlling distribution of work items to threads in a server
CN108363623A (en) GPU resource dispatching method, device, equipment and computer readable storage medium
CN114610474B (en) Multi-strategy job scheduling method and system under heterogeneous supercomputing environment
CN112286671A (en) Containerization batch processing job scheduling method and device and computer equipment
CN115048216B (en) Resource management scheduling method, device and equipment of artificial intelligent cluster
CN116010064A (en) Method, system and device for DAG job scheduling and cluster management
CN104281489A (en) Multithreading request method and system under SOA (service oriented architecture)
CN115480897A (en) Task processing method, device, equipment, storage medium and program product
CN113225269B (en) Container-based workflow scheduling method, device and system and storage medium
CN113900825A (en) App time-consuming task optimization method and device based on artificial intelligence and related equipment
CN112395062A (en) Task processing method, device, equipment and computer readable storage medium
CN114816694A (en) A multi-process collaborative RPA task scheduling method and device
CN116737331B (en) Intelligent task flow arrangement method and platform
CN115632993B (en) Flow control method and device
CN109189580B (en) Multi-task development model based on multi-core platform and method thereof
CN118567856A (en) Asynchronous management method and device for database query task and computer equipment
CN114237907B (en) Task scheduling processing method and device
CN100525211C (en) Method and system for implementing integral dispatching in network element management system
CN115543568A (en) Timed task processing method and device, computer equipment and storage medium
CN117827428A (en) Cloud service initialization method and system based on rule engine and token bucket algorithm
CN117251269A (en) Jenkins resource management method, device, equipment and storage medium
CN117193967A (en) Job task scheduling method, device, equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
TA01 Transfer of patent application right
TA01 Transfer of patent application right

Effective date of registration: 20250421

Address after: Unit 3509, 35th Floor, No. 1333 Lujiazui Ring Road, China (Shanghai) Pilot Free Trade Zone, Pudong New Area, Shanghai, 200135

Applicant after: Ping An Pay Electronic Payment Co.,Ltd.

Country or region after: China

Address before: Unit 2605, 26th Floor, Zhaobangji Financial Building, No. 319, Fuhua Road, Futian Street, Futian District, Shenzhen, Guangdong 518031

Applicant before: Pingan Payment Technology Service Co.,Ltd.

Country or region before: China