Task execution method and device, storage medium and electronic device
By managing resource access status in a resource identifier queue, threads are inserted into the tail of the queue when they detect that a resource is being accessed, thus solving the problem of low resource access efficiency in multithreaded programs and achieving efficient resource access and task execution.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-08-31
- Publication Date
- 2026-03-17
AI Technical Summary
In multithreaded programs, resource access is inefficient, leading to reduced program execution efficiency, a problem that current technologies have not been able to effectively solve.
By managing resource access status in a resource identifier queue, a thread inserts a resource at the tail of the queue when it detects that a resource has been accessed, and continues to loop in the resource identifier queue until an unaccessed resource is found and operated on, thus avoiding waiting.
It improves resource access efficiency, avoids thread waiting, enhances task execution efficiency, and ensures the efficiency and accuracy of resource access.
Smart Images

Figure CN115408130B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and more specifically, to a task execution method, apparatus, storage medium, and electronic device. Background Technology
[0002] When developing multithreaded programs, we often encounter scenarios where multiple threads need to access the same resources, such as multiple threads accessing the same memory data, the same database table, or multiple threads using the same network connection to send messages. In these scenarios, protecting resources is a necessary operation; otherwise, it may lead to data anomalies and the program not performing as expected, or even cause the program to crash.
[0003] Creating a dedicated thread for each resource to perform read and write operations could solve this problem, but this only applies to scenarios with few resources. Otherwise, it would lead to an excessive number of threads, consuming too much computer resources. To limit the number of threads, how to use a limited number of threads to safely and efficiently access all necessary resources is a problem that needs to be solved in multithreaded program development. In related technologies, when multiple threads simultaneously access the same resource, there are blocking and waiting stages, resulting in low resource access efficiency. In other words, related technologies suffer from low resource access efficiency.
[0004] There is currently no effective solution to the problem of low resource access efficiency in related technologies. Summary of the Invention
[0005] This invention provides a task execution method, apparatus, storage medium, and electronic device to at least solve the problem of low resource access efficiency in related technologies.
[0006] According to an embodiment of the present invention, a task execution method is provided, comprising: a first thread reading and removing a first resource identifier located at the head of a resource identifier queue, and obtaining a resource access status corresponding to the first resource identifier, wherein the first resource identifier is an identifier of a first resource; if the resource access status corresponding to the first resource identifier indicates that the first resource is being accessed, the first thread inserts the first resource identifier into the tail of the resource identifier queue; the first thread reading and removing a second resource identifier located at the head of the resource identifier queue, and obtaining a resource access status corresponding to the second resource identifier, wherein the second resource identifier is an identifier of a second resource; if the resource access status corresponding to the second resource identifier indicates that the second resource is not being accessed, the first thread accesses the second resource, sets the resource access status corresponding to the second resource identifier to indicate that the second resource is being accessed, and executes a task to be executed corresponding to the second resource.
[0007] In an exemplary embodiment, the step of executing the task to be executed corresponding to the second resource according to the second resource includes: reading a first task identifier located at the head of a task identifier queue corresponding to the second resource, wherein the task identifier queue corresponding to the second resource is used to record task identifiers of tasks requesting access to the second resource; and executing the first task represented by the first task identifier according to the second resource.
[0008] In an exemplary embodiment, the method further includes: after the first task is completed, the first thread sets the resource access status corresponding to the second resource identifier to indicate that the second resource is not accessed, and removes the first task identifier from the task identifier queue corresponding to the second resource.
[0009] In an exemplary embodiment, the method further includes: during the execution of a task to be executed corresponding to the second resource, if there is an idle second thread in the thread pool, the second thread reads and removes a third resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the third resource identifier, wherein the third resource identifier is an identifier of the third resource; if the resource access status corresponding to the third resource identifier indicates that the third resource is not accessed, the second thread accesses the third resource, sets the resource access status corresponding to the third resource identifier to indicate that the third resource is being accessed, and executes the task to be executed corresponding to the third resource based on the third resource.
[0010] In one exemplary embodiment, the method further includes: upon obtaining a new task to be executed, determining a fourth resource requested by the new task; creating a fourth resource identifier for identifying the fourth resource; inserting the fourth resource identifier into the tail of the resource identifier queue, and inserting the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource, wherein the task identifier queue corresponding to the fourth resource is used to record the task identifiers of tasks requesting access to the fourth resource.
[0011] In an exemplary embodiment, inserting the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource includes: if it is determined that the task identifier queue corresponding to the fourth resource is not present in memory, creating the task identifier queue corresponding to the fourth resource, and inserting the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource.
[0012] According to another embodiment of the present invention, a task execution apparatus is also provided, comprising: a first processing module, configured to read and remove a first resource identifier located at the head of a resource identifier queue via a first thread, and obtain a resource access status corresponding to the first resource identifier, wherein the first resource identifier is an identifier of a first resource; an insertion module, configured to insert the first resource identifier into the tail of the resource identifier queue via the first thread when the resource access status corresponding to the first resource identifier indicates that the first resource is being accessed; a second processing module, configured to read and remove a second resource identifier located at the head of the resource identifier queue via the first thread, and obtain a resource access status corresponding to the second resource identifier, wherein the second resource identifier is an identifier of a second resource; and a third processing module, configured to access the second resource via the first thread when the resource access status corresponding to the second resource identifier indicates that the second resource is not being accessed, set the resource access status corresponding to the second resource identifier to indicate that the second resource is being accessed, and execute a task to be executed corresponding to the second resource.
[0013] In one exemplary embodiment, the third processing module includes: a first reading unit, configured to read a first task identifier located at the head of a task identifier queue corresponding to the second resource, wherein the task identifier queue corresponding to the second resource is used to record task identifiers of tasks requesting access to the second resource; and a first execution unit, configured to execute the first task represented by the first task identifier according to the second resource.
[0014] According to yet another embodiment of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored therein, wherein the computer program is configured to perform the steps in any of the above method embodiments when it is run.
[0015] According to yet another embodiment of the present invention, an electronic device is also provided, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0016] Through this invention, the first thread reads and removes the first resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the first resource identifier. When the resource access status corresponding to the first resource identifier indicates that the first resource is being accessed, the first thread inserts the first resource identifier into the tail of the resource identifier queue. At this time, the first thread continues to read and remove the second resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the second resource identifier. When the resource access status corresponding to the second resource identifier indicates that the second resource is not being accessed, the first thread accesses the second resource and executes the task to be executed corresponding to the second resource. At the same time, the resource access status corresponding to the second resource identifier is set to the second resource being accessed. In other words, when a thread obtains the first resource identifier and determines that the first resource is being accessed, it inserts the first resource identifier into the tail of the resource identifier queue. It then obtains the next resource identifier, the second resource identifier. When it is determined that the second resource corresponding to the second resource identifier is not being accessed, it accesses the second resource and executes the task corresponding to it. This avoids the problem of low resource access efficiency and low task execution efficiency caused by the thread continuing to wait when the resource identifier is being accessed, as well as the problem of low resource access efficiency in related technologies. Therefore, it solves the problem of low resource access efficiency in related technologies and achieves the effect of improving resource access efficiency. Attached Figure Description
[0017] Figure 1 This is a block diagram of the mobile terminal hardware structure of the task execution method according to an embodiment of the present invention;
[0018] Figure 2 This is a flowchart of a task execution method according to an embodiment of the present invention;
[0019] Figure 3 This is an example of a resource identifier queue according to an embodiment of the present invention. Figure 1 ;
[0020] Figure 4 This is an example of a resource identifier queue according to an embodiment of the present invention. Figure 2 ;
[0021] Figure 5 This is an example of a resource identifier queue according to an embodiment of the present invention. Figure 3 ;
[0022] Figure 6 This is a schematic diagram of a task queue according to an embodiment of the present invention. Figure 1 ;
[0023] Figure 7 This is a schematic diagram of a task queue according to an embodiment of the present invention. Figure 2 ;
[0024] Figure 8 This is a schematic diagram of a task queue according to an embodiment of the present invention. Figure 3 ;
[0025] Figure 9 This is a flowchart of a resource access method according to a specific embodiment of the present invention;
[0026] Figure 10 This is a structural block diagram of a task execution device according to an embodiment of the present invention. Detailed Implementation
[0027] The embodiments of the present invention will be described in detail below with reference to the accompanying drawings and examples.
[0028] It should be noted that the terms "first," "second," etc., in the specification, claims, and drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0029] The methods and embodiments provided in this application can be executed on a mobile terminal, computer terminal, or similar computing device. Taking running on a mobile terminal as an example, Figure 1 This is a block diagram of the mobile terminal hardware structure of the task execution method according to an embodiment of the present invention. Figure 1 As shown, a mobile terminal may include one or more ( Figure 1 Only one is shown in the diagram. A processor 102 (which may include, but is not limited to, a microprocessor MCU or a programmable logic device FPGA, etc.) and a memory 104 for storing data are also shown. The mobile terminal may further include a transmission device 106 for communication functions and an input / output device 108. Those skilled in the art will understand that... Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the mobile terminal described above. For example, the mobile terminal may also include components that are more... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown.
[0030] The memory 104 can be used to store computer programs, such as application software programs and modules, like the computer program corresponding to the task execution method in this embodiment of the invention. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, thus implementing the above-described method. The memory 104 may include high-speed random access memory and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to the mobile terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0031] The transmission device 106 is used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by the mobile terminal's communication provider. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 may be a Radio Frequency (RF) module used for wireless communication with the Internet.
[0032] This embodiment provides a task execution method. Figure 2 This is a flowchart of a task execution method according to an embodiment of the present invention, such as... Figure 2 As shown, the process includes the following steps:
[0033] Step S202: The first thread reads and removes the first resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the first resource identifier, wherein the first resource identifier is the identifier of the first resource.
[0034] Step S204: When the resource access status corresponding to the first resource identifier indicates that the first resource is being accessed, the first thread inserts the first resource identifier into the tail of the resource identifier queue.
[0035] Step S206: The first thread reads and removes the second resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the second resource identifier, wherein the second resource identifier is the identifier of the second resource;
[0036] In step S208, when the resource access status corresponding to the second resource identifier indicates that the second resource is not accessed, the first thread accesses the second resource, sets the resource access status corresponding to the second resource identifier to indicate that the second resource is being accessed, and executes the task to be executed corresponding to the second resource.
[0037] Through the above steps, the first thread reads and removes the first resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the first resource identifier. When the resource access status corresponding to the first resource identifier indicates that the first resource is being accessed, the first thread inserts the first resource identifier into the tail of the resource identifier queue. At this time, the first thread continues to read and remove the second resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the second resource identifier. When the resource access status corresponding to the second resource identifier indicates that the second resource is not being accessed, the first thread accesses the second resource and executes the task to be executed corresponding to the second resource. At the same time, the resource access status corresponding to the second resource identifier is set to the second resource being accessed. In other words, when a thread obtains the first resource identifier and determines that the first resource is being accessed, it inserts the first resource identifier into the tail of the resource identifier queue. It then obtains the next resource identifier, the second resource identifier. When it is determined that the second resource corresponding to the second resource identifier is not being accessed, it accesses the second resource and executes the task corresponding to it. This avoids the problem of low resource access efficiency and low task execution efficiency caused by the thread continuing to wait when the resource identifier is being accessed, as well as the problem of low resource access efficiency in related technologies. Therefore, it solves the problem of low resource access efficiency in related technologies and achieves the effect of improving resource access efficiency.
[0038] The execution entity of the above steps can be a program or a thread, such as the first thread mentioned above. It can be any thread in a thread pool, a processor with human-computer interaction capabilities configured on a storage device, or a processing device or unit with similar processing capabilities, but is not limited to these. The following explanation uses the first thread executing the above operations as an example (this is merely an illustrative example; in actual operation, other devices or modules can also perform the above operations):
[0039] In the above embodiment, the first thread reads and removes the first resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the first resource identifier. Here, the first resource identifier is the identifier of the first resource. In practical applications, the resource identifier queue may store resource identifiers corresponding to one or more different resources, such as... Figure 3 As shown, Figure 3 This is an example of a resource identifier queue according to an embodiment of the present invention. Figure 1 The first thread reads the first resource identifier from the head of the queue, such as... Figure 3 In Res1, of course, the resource identifier queue may also be empty; when the resource access status corresponding to the first resource identifier indicates that the first resource is being accessed, the first thread inserts the first resource identifier into the tail of the resource identifier queue, that is, when the first resource identifier (such as...) is determined... Figure 3 When the resource access status corresponding to Res1 is "the first resource is being accessed", the first thread inserts the first resource identifier into the tail of the resource identifier queue, such as... Figure 4 As shown, Figure 4 This is an example of a resource identifier queue according to an embodiment of the present invention. Figure 2 Res1 is inserted at the tail of the queue, and Res2 is now at the head of the queue. The first thread reads and removes the second resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the second resource identifier. Here, the second resource identifier is the identifier of the second resource. For example, if the second resource identifier is... Figure 4 In the context of Res2, the head of the queue corresponds to Res2. When the resource access status corresponding to the second resource identifier indicates that the second resource is not accessed, the first thread accesses the second resource, sets the resource access status corresponding to the second resource identifier to indicate that the second resource is being accessed, and executes the pending task corresponding to the second resource. That is, when it is determined that the second resource is not currently accessed by other threads, the first thread will access the second resource and execute the pending task corresponding to the second resource. It should be noted that when the first thread determines that the resource access status corresponding to the first resource identifier is that the first resource is not accessed, the first thread will access the first resource and execute the pending task corresponding to the first resource. This embodiment avoids the problem of low resource access efficiency and low task execution efficiency caused by threads continuing to wait when the resource corresponding to the resource identifier is being accessed, as well as the problem of low resource access efficiency in related technologies. Therefore, it solves the problem of low resource access efficiency in related technologies and achieves the effect of improving resource access efficiency.
[0040] Combination Figure 5 The above embodiments will be further described below. Figure 5 This is an example of a resource identifier queue according to an embodiment of the present invention. Figure 3 The first thread reads the first resource identifier (e.g., ...) from the head of the resource identifier queue. Figure 5In the first resource identifier queue, the resource access status corresponding to the first resource identifier (e.g., Res1) is obtained. If the resource access status corresponding to the first resource identifier is that the resource corresponding to Res1 is being accessed, Res1 is removed and inserted into the tail of the resource identifier queue. If the resource access status corresponding to the first resource identifier is that the resource corresponding to Res1 is not being accessed, Res1 is removed. At this time, the first thread can access the resource corresponding to Res1 and execute the corresponding task.
[0041] It should be noted that the resource identifier queue stores identifiers for different resources, such as... Figure 3 Res1 to ResN and ResX are identifiers for different resources. Each resource identifier is created when a task is received and is stored in the queue sequentially from the tail of the resource identifier queue. That is, the resource identifier closer to the head indicates the resource identifier stored earlier, and the resource identifier closer to the tail indicates the resource identifier stored later. Figure 3 The entries Res1 to ResN are merely examples and do not necessarily mean that Res1 is stored in the queue before Res2. The order depends on the resource access required by the newly created task. For instance, if a task requests access to ResN before a task requests access to Res1, then ResN is earlier than Res1, and in this case, ResN is closer to the head of the queue than Res1. It should also be noted that the resource identifiers Res1 to ResN in the queue may appear repeatedly. For example, from the head to the tail of the queue, the sequence might be Res1-Res2-Res1-Res10-ResN-Res2…
[0042] In an optional embodiment, the step of executing the task to be executed corresponding to the second resource according to the second resource includes: reading a first task identifier located at the head of a task identifier queue corresponding to the second resource, wherein the task identifier queue corresponding to the second resource is used to record task identifiers of tasks requesting access to the second resource; and executing the first task represented by the first task identifier according to the second resource. In this embodiment, during the execution of the task to be executed corresponding to the second resource, the aforementioned first thread reads the first task identifier located at the head of the task identifier queue corresponding to the second resource, such as... Figure 6 The task identifier at the head of the task queue for the second resource (such as the resource corresponding to Res) is Task. 2-1 (i.e., the first task identifier). In practical applications, the task queue may contain one or more task identifiers. Each task identifier in the task queue corresponds to the identifier of the task requesting access to the corresponding resource. For example... Figure 6 The task queue corresponding to Res1 records the task identifier (Task) for requests to access Res1 resources. 1-1 ~Task1-X The task queue corresponding to Res2 records the task identifier (Task) for requests to access Res2 resources. 2-1 ~Task 2-Y The task queue corresponding to ResN records the task identifier (Task) for requests to access ResN resources. N-1 ~Task N-Z In practical applications, the task identifiers in each task queue are also stored sequentially in the order in which the tasks were created. For example, Task 2-1 The corresponding task was created earlier than the Task. 2-2 Task 2-2 The corresponding task was created earlier than the Task. 2-J The first thread reads the first task identifier (such as the Task identifier mentioned above). 2-1 After that, execute the first task indicated by the first task identifier, i.e., Task. 2-1 The identified task. This embodiment achieves the goal of executing the task corresponding to the first task identifier by reading the first task identifier at the head of the task identifier queue.
[0043] In an optional embodiment, the method further includes: after completing the first task, the first thread sets the resource access status corresponding to the second resource identifier to indicate that the second resource is not accessed, and removes the first task identifier from the task identifier queue corresponding to the second resource. In this embodiment, after the first thread completes the first task, the first thread sets the resource access status corresponding to the second resource identifier (such as Res2 mentioned above) to unaccessed, that is, other threads can access the resource corresponding to the second resource identifier at this time. At the same time, the first thread also removes the first task identifier (such as Task mentioned above) from the task identifier queue. 2-1 Remove it from the task identifier queue corresponding to the second resource (such as the task queue corresponding to Res2 mentioned above), such as... Figure 7 As shown, after removal, the head of the task queue corresponding to Res2 is the task identifier (Task). 2-2 That is, when another thread or the next task executed by the first thread requests access to a resource corresponding to Res2 and the access status of that resource is unaccessed, the Task will be executed. 2-2 The corresponding task. Through this embodiment, after the first task is completed, the resource access status corresponding to the second resource identifier is promptly set to an unaccessed state, thereby allowing other threads to continue accessing the second resource.
[0044] In an optional embodiment, the method further includes: during the execution of the task to be executed corresponding to the second resource, if there is an idle second thread in the thread pool, the second thread reads and removes the third resource identifier located at the head of the resource identifier queue, and obtains the resource access status corresponding to the third resource identifier, wherein the third resource identifier is the identifier of the third resource; if the resource access status corresponding to the third resource identifier indicates that the third resource is not accessed, the second thread accesses the third resource, sets the resource access status corresponding to the third resource identifier to indicate that the third resource is being accessed, and executes the task to be executed corresponding to the third resource according to the third resource. In this embodiment, when the first thread is executing the task to be executed corresponding to the second resource, other threads in the thread pool can continue to read the resource identifiers at the head of the resource identifier queue. For example, when there is an idle second thread in the thread pool, the second thread can read and remove the resource identifier at the head of the queue, such as the third resource identifier mentioned above, and obtain the resource access status corresponding to the third resource identifier. If the resource access status corresponding to the third resource identifier is that the third resource has not been accessed, the second thread can access the third resource and execute the task to be executed corresponding to the third resource. Through this embodiment, multiple threads in the thread pool can continuously obtain the resource identifiers (such as the third resource identifier mentioned above) at the head of the queue from the resource identifier queue, and execute the tasks in the task identifier queue corresponding to the resource identifier when the resource corresponding to the resource identifier has not been accessed. This achieves the goal of not blocking or waiting during thread execution until the resource identifier queue and all task queues are empty, thus achieving the goal of efficiently accessing resources and executing tasks.
[0045] In an optional embodiment, the method further includes: upon receiving a new task to be executed, determining the fourth resource requested by the new task; creating a fourth resource identifier to identify the fourth resource; inserting the fourth resource identifier into the tail of the resource identifier queue, and inserting the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource, wherein the task identifier queue corresponding to the fourth resource is used to record the task identifiers of tasks requesting access to the fourth resource. For example, when the task allocator receives a new task to be executed, it determines the fourth resource requested by the new task, creates a fourth resource identifier corresponding to the fourth resource, and inserts the fourth resource identifier into the tail of the resource identifier queue. That is, for each new task, according to the resource required by the new task, the unique identifier of the resource is inserted into the tail of the resource identifier queue; simultaneously, the task identifier corresponding to the new task is also inserted into the tail of the task identifier queue corresponding to the fourth resource. Figure 8 As shown, the task identifier corresponding to the new task (e.g.) Figure 8 Task 4-W The task identifier is inserted at the tail of the task identifier queue corresponding to the fourth resource (e.g., Res4). That is, when a new task is obtained, a corresponding resource identifier is created and inserted at the tail of the resource identifier queue, and the task identifier is inserted at the tail of the task identifier queue. The earlier a task is obtained, the earlier its corresponding task identifier is stored in the corresponding task identifier queue, i.e., closer to the head of the queue. Therefore, this achieves the goal of storing all tasks that need to access the corresponding resource sequentially in the task queue, thus enabling threads to retrieve tasks from the task identifier queue in order.
[0046] In an optional embodiment, inserting the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource includes: if it is determined that the task identifier queue corresponding to the fourth resource is not present in memory, creating the task identifier queue corresponding to the fourth resource, and inserting the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource. In this embodiment, when the task allocator obtains a new task, if it is determined that the task identifier queue corresponding to the fourth resource (i.e., the resource that the new task needs to access) is not present in memory, the task allocator will create the task identifier queue corresponding to the fourth resource and insert the identifier of the new task into the tail of the newly created task identifier queue corresponding to the fourth resource.
[0047] Obviously, the embodiments described above are only some embodiments of the present invention, and not all embodiments. The present invention will be specifically described below with reference to the embodiments.
[0048] Figure 9 This is a flowchart of a resource access method according to a specific embodiment of the present invention, the process including:
[0049] S902: First, for all resources that the thread pool will access and that need protection, a unique resource identifier, a resource access status, and a task queue are created. The access status and task queue of this resource can be obtained through the unique resource identifier. The resource access status is divided into an accessed state and an unaccessed state, which respectively indicate that this resource is currently being accessed by a certain thread or is not being accessed by any thread. The task queue stores all tasks that need to access this resource in sequence. Then, a resource identifier queue is created, which can store the unique identifier of the resource.
[0050] S904, for each new task, based on the resource that the task needs to access, insert the task into the tail of the task queue of the resource it needs, and at the same time insert the unique identifier of this resource into the tail of the resource identifier queue.
[0051] S906, a thread in the thread pool reads and removes the resource identifier at the head of the resource identifier queue.
[0052] S908, determine whether the resource identifier has been obtained.
[0053] S910, if the judgment result of step S908 is negative, that is, the resource identifier queue is empty. If the resource identifier queue is empty, it means that there is no task to be executed at this time. At this time, you can return to step S906 to obtain a new resource identifier.
[0054] S912, if the judgment result of the above step S908 is negative, the access status of the resource is obtained through this resource identifier.
[0055] S914 determines the access status of a resource, that is, whether the resource is in an accessed state or an inaccessible state.
[0056] S916, if the status is not accessed, change the status to accessed.
[0057] S918, and reads a task from the head of the task queue of this resource for execution.
[0058] S920: After execution, remove this task from the task queue of this resource and change the access status of this resource back to inaccessible. Then return to step S906 and repeat the above steps.
[0059] S922, if the result of step S914 indicates that the resource is in an access state, then the resource identifier is reinserted into the tail of the resource identifier queue. Then, the above steps are repeated starting from step S906. This is because the resource is currently being accessed by another thread in the thread pool. Since each thread in the thread pool first determines the access state of the resource it needs to access, it ensures that while the current thread is executing this task, other threads in the thread pool will not access this resource, thus avoiding the situation where multiple threads in the thread pool access the same resource.
[0060] In the above embodiments, there are no blocking and waiting stages during thread execution. When a resource is in an accessed state, there is no need to wait for the resource's accessed state to change to an inaccessible state. Instead, the resource identifier is reinserted into the tail of the resource identifier queue, and the loop continues. This process is repeated until a resource with an inaccessible state is obtained. For each resource, any thread in the thread pool may access it, but at most one thread will access it at any given time. Furthermore, because the task queue for each resource stores the tasks that need to access it sequentially, the tasks retrieved by the thread currently accessing the resource are also sequential. This sequential execution of tasks further ensures the accuracy of resource access.
[0061] In the above embodiments, by creating a resource protection state for resources, threads can determine the current access status of resources, avoiding multiple threads accessing the same resource simultaneously. When a thread determines that the current resource is in an access state, it continues to execute the next loop until it finds an unaccessed resource, avoiding waiting for occupied resources and improving the utilization of the thread pool. For each new task, based on the resource it needs to access, the unique identifier of this resource is inserted into the tail of the resource identifier queue, so that all threads in the thread pool can obtain the task to be executed by reading this resource identifier queue, improving thread utilization. For each new task, based on the resource it needs to access, the task is inserted into the tail of the task queue of the resource it needs, ensuring that business operations that perform the same operation are executed sequentially.
[0062] The technical solution of this invention has the following advantages: (1) It is applicable to scenarios where resources need to be protected; by creating a resource protection state for the resource, it can be ensured that multiple threads in the thread pool will not access the same resource together, which will cause an exception; (2) It improves the utilization rate of the thread pool; by using the resource identifier queue, the thread that obtains the resource protection state is in the access state does not need to block and wait for other threads to release the resource occupation, but can continue to execute the next loop in the resource identifier queue until an unaccessed resource is found and the task is executed; (3) By creating a task queue for the resource, it can be ensured that multiple services that need to operate on the same resource are executed sequentially, which further ensures the accuracy of the resource.
[0063] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods described in the various embodiments of the present invention.
[0064] This embodiment also provides a task execution device. Figure 10 This is a structural block diagram of a task execution device according to an embodiment of the present invention, such as... Figure 10 As shown, the device includes:
[0065] The first processing module 1002 is used to read and remove the first resource identifier located at the head of the resource identifier queue through the first thread, and obtain the resource access status corresponding to the first resource identifier, wherein the first resource identifier is the identifier of the first resource.
[0066] The insertion module 1004 is used to insert the first resource identifier into the tail of the resource identifier queue through the first thread when the resource access status corresponding to the first resource identifier indicates that the first resource is being accessed.
[0067] The second processing module 1006 is used to read and remove the second resource identifier located at the head of the resource identifier queue through the first thread, and obtain the resource access status corresponding to the second resource identifier, wherein the second resource identifier is the identifier of the second resource;
[0068] The third processing module 1008 is used to access the second resource through the first thread when the resource access status corresponding to the second resource identifier indicates that the second resource is not accessed, set the resource access status corresponding to the second resource identifier to indicate that the second resource is being accessed, and execute the pending task corresponding to the second resource according to the second resource.
[0069] In an optional embodiment, the third processing module 1008 includes: a first reading unit, configured to read a first task identifier located at the head of the task identifier queue corresponding to the second resource through the first thread, wherein the task identifier queue corresponding to the second resource is used to record the task identifiers of tasks requesting access to the second resource; and a first execution unit, configured to execute the first task represented by the first task identifier according to the second resource through the first thread.
[0070] In an optional embodiment, the above apparatus further includes: a fourth processing module, configured to, upon completion of the first task, set the resource access status corresponding to the second resource identifier to indicate that the second resource is not accessed via the first thread, and remove the first task identifier from the task identifier queue corresponding to the second resource.
[0071] In an optional embodiment, the above apparatus further includes: a fifth processing module, configured to, during the execution of a task to be executed corresponding to the second resource, if there is an idle second thread in the thread pool, read and remove a third resource identifier located at the head of the resource identifier queue through the second thread, and obtain the resource access status corresponding to the third resource identifier, wherein the third resource identifier is an identifier of the third resource; and a sixth processing module, configured to, if the resource access status corresponding to the third resource identifier indicates that the third resource is not accessed, access the third resource through the second thread, set the resource access status corresponding to the third resource identifier to indicate that the third resource is being accessed, and execute the task to be executed corresponding to the third resource according to the third resource.
[0072] In an optional embodiment, the apparatus further includes: a determining module, configured to determine the fourth resource requested by the new task upon receiving a new task to be executed; a creating module, configured to create a fourth resource identifier for identifying the fourth resource; and a seventh processing module, configured to insert the fourth resource identifier into the tail of the resource identifier queue and insert the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource, wherein the task identifier queue corresponding to the fourth resource is used to record the task identifiers of tasks requesting access to the fourth resource.
[0073] In an optional embodiment, the seventh processing module includes a processing unit, configured to create a task identifier queue corresponding to the fourth resource when it is determined that the memory does not contain a task identifier queue corresponding to the fourth resource, and insert the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource.
[0074] It should be noted that the above modules can be implemented by software or hardware. For the latter, they can be implemented in the following ways, but are not limited to: all the above modules are located in the same processor; or, the above modules are located in different processors in any combination.
[0075] Embodiments of the present invention also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to perform the steps in any of the above method embodiments when executed.
[0076] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.
[0077] Embodiments of the present invention also provide an electronic device including a memory and a processor, the memory storing a computer program and the processor being configured to run the computer program to perform the steps in any of the above method embodiments.
[0078] In one exemplary embodiment, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0079] Specific examples in this embodiment can be found in the examples described in the above embodiments and exemplary implementations, and will not be repeated here.
[0080] It is obvious to those skilled in the art that the modules or steps of the present invention described above can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. They can be implemented using computer-executable program code, and thus can be stored in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those described herein, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, the present invention is not limited to any particular combination of hardware and software.
[0081] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, or improvements made within the principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A task execution method characterized by, The method comprises: a first thread reads and removes a first resource identifier at the head of a resource identifier queue, and obtains a resource access state corresponding to the first resource identifier, wherein the first resource identifier is an identifier of a first resource; in a case where the resource access state corresponding to the first resource identifier indicates that the first resource is being accessed, the first thread inserts the first resource identifier at the tail of the resource identifier queue; the first thread reads and removes a second resource identifier at the head of the resource identifier queue, and obtains a resource access state corresponding to the second resource identifier, wherein the second resource identifier is an identifier of a second resource; in a case where the resource access state corresponding to the second resource identifier indicates that the second resource is not being accessed, the first thread accesses the second resource, sets the resource access state corresponding to the second resource identifier to indicate that the second resource is being accessed, and executes a to-be-executed task corresponding to the second resource according to the second resource.
2. The method of claim 1, wherein, The execution of the to-be-executed task corresponding to the second resource according to the second resource comprises: reading a first task identifier at the head of a task identifier queue corresponding to the second resource, wherein the task identifier queue corresponding to the second resource is used to record task identifiers of tasks requesting to access the second resource; executing a first task represented by the first task identifier according to the second resource.
3. The method of claim 2, wherein, The method further comprises: in a case where the first task is executed, the first thread sets the resource access state corresponding to the second resource identifier to indicate that the second resource is not being accessed, and removes the first task identifier from the task identifier queue corresponding to the second resource.
4. The method of claim 1, wherein, The method further comprises: in a case where there is an idle second thread in a thread pool during the execution of the to-be-executed task corresponding to the second resource, the second thread reads and removes a third resource identifier at the head of the resource identifier queue, and obtains a resource access state corresponding to the third resource identifier, wherein the third resource identifier is an identifier of a third resource; in a case where the resource access state corresponding to the third resource identifier indicates that the third resource is not being accessed, the second thread accesses the third resource, sets the resource access state corresponding to the third resource identifier to indicate that the third resource is being accessed, and executes a to-be-executed task corresponding to the third resource according to the third resource.
5. The method of claim 1, wherein, The method further comprises: in a case where a new task to be executed is obtained, determining a fourth resource requested to be accessed by the new task; creating a fourth resource identifier used to identify the fourth resource; inserting the fourth resource identifier at the tail of the resource identifier queue, and inserting a task identifier of the new task at the tail of a task identifier queue corresponding to the fourth resource, wherein the task identifier queue corresponding to the fourth resource is used to record task identifiers of tasks requesting to access the fourth resource.
6. The method of claim 5, wherein, The inserting the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource comprises: In a case where it is determined that the task identifier queue corresponding to the fourth resource is not included in the memory, creating the task identifier queue corresponding to the fourth resource, and inserting the task identifier of the new task into the tail of the task identifier queue corresponding to the fourth resource.
7. A task execution apparatus characterized by comprising: Comprise: The first processing module is configured to read and remove a first resource identifier located at the head of a resource identifier queue through a first thread, and acquire a resource access state corresponding to the first resource identifier, wherein the first resource identifier is an identifier of a first resource; The inserting module is configured to insert the first resource identifier into the tail of the resource identifier queue through the first thread in a case where the resource access state corresponding to the first resource identifier indicates that the first resource is being accessed; The second processing module is configured to read and remove a second resource identifier located at the head of the resource identifier queue through the first thread, and acquire a resource access state corresponding to the second resource identifier, wherein the second resource identifier is an identifier of a second resource; The third processing module is configured to access the second resource through the first thread in a case where the resource access state corresponding to the second resource identifier indicates that the second resource is not being accessed, set the resource access state corresponding to the second resource identifier to indicate that the second resource is being accessed, and execute a to-be-executed task corresponding to the second resource according to the second resource.
8. The apparatus of claim 7, wherein, The third processing module comprises: A first reading unit is configured to read a first task identifier located at the head of a task identifier queue corresponding to the second resource, wherein the task identifier queue corresponding to the second resource is used to record task identifiers of tasks requesting to access the second resource; A first execution unit is configured to execute a first task represented by the first task identifier according to the second resource.
9. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program, wherein the computer program is executed by the processor to implement the steps of the method in any one of claims 1 to 6.
10. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor executes the computer program to implement the steps of the method in any one of claims 1 to 6.
Citation Information
Patent Citations
Computer-Implemented Systems For Resource Level Locking Without Resource Level Locks
US20100242043A1