Task execution method and device, electronic equipment and storage medium

By creating an object containing a context replacement method in the parent thread and performing context replacement before executing tasks in the thread pool, the problem of parent thread context delivery is solved, and the normal execution of tasks in the thread pool is achieved.

CN119829222APending Publication Date: 2025-04-15MASHANG CONSUMER FINANCE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311325279.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-10-12
Publication Date
2025-04-15

AI Technical Summary

Technical Problem

When the thread pool handles the parent thread task, the context of the parent thread cannot be correctly passed to the thread in the thread pool, affecting the normal execution of the task.

Method used

Create an object in the parent thread that contains execution methods, context replacement methods, and member variables to store the context of the parent thread. Submit the task through the task submission interface of the thread pool, and before the child thread executes the task, the context replacement method is called to overwrite the context of the parent thread to the child thread's context.

Benefits of technology

It realizes the accurate delivery of the parent thread context to the thread in the thread pool, ensuring the normal execution of the parent thread task in the thread pool, and avoiding the tedious process of manual copying and cleaning the context.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119829222A_ABST
    Figure CN119829222A_ABST
Patent Text Reader

Abstract

The invention discloses a task execution method and device, electronic equipment and a storage medium, and the method comprises the steps that a first object is created in a parent thread, the first object comprises an execution method, a context replacement method and a first member variable, and the first member variable is used for storing a context of the parent thread; creating a task by using the first object, and submitting the task to the thread pool through a task submission interface of the thread pool; and under the condition that the sub-thread in the thread pool is allocated to execute the task and before the sub-thread calls the execution method to execute the task, calling a context replacement method, and covering the context of the sub-thread by using the context stored in the first member variable. Therefore, by changing the implementation logic of the parent thread and the execution logic of the task, the context of the parent thread can be accurately transmitted to the thread in the thread pool, so that the task of the parent thread can be ensured to be normally executed in the thread pool.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a task execution method, device, electronic device and storage medium. Background Art

[0002] When a parent thread is executing a task, it can usually create a child thread and hand over the task to the child thread. When a child thread is executing a task, it usually needs to obtain the context of the parent thread, that is, the parent thread needs to pass its context to the child thread so that the child thread can execute the task based on the context of the parent thread, thereby ensuring the normal execution of the task.

[0003] In related technologies, in order to improve task execution efficiency and reduce waiting time, thread pool technology can usually be used to execute tasks in parallel, that is, the tasks of the parent thread are submitted to the thread pool, and the thread pool allocates corresponding threads for processing. When processing the tasks of the parent thread, the threads in the thread pool also need to obtain the context of the parent thread. However, in actual applications, the threads in the thread pool are reused, which will cause the context obtained by the thread when executing the task to always be the same, which will cause problems in the transfer of the parent thread's context, affecting the normal execution of the task. Summary of the invention

[0004] Embodiments of the present application provide a task execution method, device, electronic device, and storage medium, which are used to solve the problem that the context of a parent thread cannot be correctly passed to threads in a thread pool.

[0005] To solve the above technical problems, the embodiments of the present application are implemented as follows:

[0006] Firstly, a task execution method is proposed, comprising:

[0007] Creating a first object in a parent thread, wherein the first object includes an execution method, a context replacement method, and a first member variable, wherein the first member variable is used to store the context of the parent thread;

[0008] Use the first object to create a task, and submit the task to the thread pool through the task submission interface of the thread pool;

[0009] When a child thread in the thread pool is assigned to execute the task, and before the child thread calls the execution method to execute the task, the context replacement method is called to overwrite the context of the child thread with the context stored in the first member variable.

[0010] In a second aspect, a task execution device is provided, comprising:

[0011] A creation module is used to create a first object in a parent thread, wherein the first object includes an execution method, a context replacement method, and a first member variable, and the first member variable is used to store the context of the parent thread;

[0012] A submission module, using the first object to create a task, and submitting the task to the thread pool through the task submission interface of the thread pool;

[0013] The execution module calls the context replacement method to overwrite the context of the child thread with the context stored in the first member variable when the child thread in the thread pool is assigned to execute the task and before the child thread calls the execution method to execute the task.

[0014] In a third aspect, the present application provides an electronic device, including:

[0015] processor;

[0016] a memory for storing instructions executable by the processor;

[0017] The processor is configured to execute the instructions to implement the method as described in the first aspect.

[0018] In a fourth aspect, the present application provides a computer-readable storage medium. When instructions in the storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the method described in the first aspect.

[0019] At least one of the above technical solutions adopted in the embodiments of the present application can achieve the following beneficial effects:

[0020] In the scenario where the thread pool processes the parent thread's tasks, an embodiment of the present application can create a first object in the parent thread, in which an execution method for executing tasks, a context replacement method for performing context replacement, and a first member variable for storing the parent thread context are defined. When creating the parent thread's tasks, the creation can be performed through the first object, and then the parent thread's tasks can be submitted to the thread pool through the thread pool's task submission interface. When a child thread in the thread pool is assigned to execute a task, the context replacement method in the first object can be called first, and the original context of the child thread can be overwritten with the context of the parent thread stored in the first member variable, and then the execution method in the first object can be called to execute the task. Thus, by changing the implementation logic of the parent thread and the execution logic of the task, the context of the parent thread can be accurately passed to the thread in the thread pool, thereby ensuring that the parent thread's tasks can be executed normally in the thread pool. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0022] Figure 1 It is a flowchart of a task execution method of an embodiment of the present application;

[0023] Figure 2 It is a flowchart of a task execution method of an embodiment of the present application;

[0024] Figure 3 This is a schematic diagram of methods and variables included in ContextThreadLocal, an embodiment of the present application;

[0025] Figure 4 This is a schematic diagram of methods and variables included in ContextRunnable, an embodiment of the present application;

[0026] Figure 5 It is a flowchart of determining whether a user is eligible for a loan based on a task execution method according to an embodiment of the present application;

[0027] Figure 6 is a schematic diagram of the structure of an electronic device according to an embodiment of the present application;

[0028] Figure 7 It is a structural diagram of a task execution device according to an embodiment of the present application. DETAILED DESCRIPTION

[0029] Currently, after creating a task, the parent thread can submit the task to the thread pool for execution in order to improve the efficiency of task execution. When executing a task, the thread in the thread pool needs to obtain the context of the parent thread to ensure the correct execution of the task. However, since the threads in the thread pool are reused, the context obtained by the thread when executing a task is always the same, which in turn causes problems in the transfer of the parent thread's context, affecting the normal execution of the task.

[0030] In the related art, when the parent thread passes its context, it can create a child thread in the parent thread, and then pass the parent thread's context (that is, ThreadLocal value) to the child thread through the InheritableThreadLocal technology. However, the InheritableThreadLocal technology passes the parent thread's context to the child thread during the child thread creation phase, and the threads in the thread pool are created in advance. Therefore, for the threads in the thread pool, the parent thread's context cannot be copied through the InheritableThreadLocal technology.

[0031] In order to ensure that the context of the parent thread can be accurately passed to the threads in the thread pool, one solution is to manually copy the context of the parent thread and encapsulate it in a Runnable task, and then pass it to the thread in the thread pool for use, so that the context of the parent thread can be accurately passed to the threads in the thread pool. However, in actual applications, this solution requires knowing what contexts the parent thread has and how each context is obtained in business development, and then copying and passing them one by one during the development process. The whole process is relatively cumbersome. In addition, when manually passing the context of the parent thread to the thread in the thread pool, it is necessary to do a good job of cleaning up. If the cleaning operation is omitted, the context obtained by the thread will be the last one, resulting in business logic errors. However, too many cleaning operations may cause the problem of context loss. For example, when the thread pool is full and the rejection strategy of CallerRunsPolicy is used, the task submitted by the parent thread to the thread pool will be executed in the parent thread, and the cleaning operation at this time cleans up the context of the parent thread, resulting in the loss of the context of the parent thread.

[0032] The embodiments of the present application provide a task execution method, device, electronic device and storage medium. By changing the implementation logic of the parent thread and the execution logic of the task, the context of the parent thread can be accurately passed to the thread in the thread pool, ensuring that the task of the parent thread can be normally executed in the thread pool. Compared with the above-mentioned method of passing the context of the parent thread by manual copying, the technical solution provided by the embodiments of the present application can automatically pass the parent thread context to the thread in the thread pool when the task is executed, which is more efficient, and there is no need to perform context cleanup operations when passing the context, which is less prone to errors.

[0033] In order to enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below in conjunction with the drawings in the embodiments of this application. Obviously, the described embodiments are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of this application.

[0034] The terms "first", "second", etc. in this specification and claims are used to distinguish similar objects, and are not used to describe a particular order or precedence. It should be understood that the terms used in this way are interchangeable where appropriate, so that the embodiments of this specification can be implemented in an order other than those illustrated or described herein. In addition, "and / or" in this specification and claims means at least one of the connected objects, and the character " / " generally means that the objects associated with each other are in an "or" relationship.

[0035] The core inventive concept of the technical solution provided by the embodiment of the present application is: the implementation logic of the parent thread and the task execution logic of the thread pool are improved, so that the timing of the transfer of the parent thread context can be delayed from thread creation to task execution, so that the context of the parent thread can be accurately transferred to the thread in the thread pool. Among them, the improvement of the parent thread implementation logic is specifically to create a first object in the parent thread, the first object includes an execution method for executing tasks, and a context replacement method for context replacement and a first member variable for storing the parent thread context are added. The improvement of the task execution logic is specifically that when a child thread in the thread pool is assigned to perform a task, before the child thread calls the execution method in the first object to perform the task of the parent thread, the context replacement method in the first object is called first, and the parent thread context stored in the first member variable is used to overwrite the original context of the child thread. In this way, when the child thread executes the task of the parent thread, it can be executed based on the context of the parent thread, so that the transfer of the parent thread context can be realized.

[0036] Furthermore, when improving the implementation logic of the parent thread, a second object can be created in the parent thread. The second object inherits from the original inheritable thread local variable object of the parent thread, rewrites the data writing method and the data reading method, and adds a second member variable. In this way, when writing data to the context of the parent thread through the data writing method, the data will also be written to the second member variable. The second member variable can assign the context of the parent thread to the first member variable in the first object, so that the context of the parent thread can be stored in the first member variable, thereby successfully capturing the context of the parent thread, so that the context of the parent thread can be passed to the child thread in the thread pool based on the first member variable. In addition, when creating the first object in the parent thread, a context recovery method can also be defined in the first object. Further, the improvement of the task execution logic also includes that after the task is executed, the context recovery method in the first object is called to overwrite the current parent thread context with the original context of the child thread, thereby ensuring that the child thread can restore the original context after the task is executed.

[0037] It should be noted that the execution subject of the task execution method provided in the embodiment of the present application can be a processor or module such as a CPU, GPU, etc. in the client or server that can create tasks through a parent thread and execute tasks in parallel through a thread pool.

[0038] The technical solutions provided by various embodiments of the present application are described in detail below in conjunction with the accompanying drawings.

[0039] Figure 1 FIG. 1 is a flow chart of a task execution method according to an embodiment of the present application. The task execution method is described as follows.

[0040] S102: Create a first object in the parent thread, the first object includes an execution method, a context replacement method and a first member variable, and the first member variable is used to store the context of the parent thread.

[0041] In the case where the task of the parent thread is executed by the thread pool, a first object can be created in the parent thread. When creating the first object, an execution method, a context replacement method and a first member variable can be defined in the first object. The execution method is used to execute the task of the parent thread, and the context replacement method is used to overwrite or replace the context, that is, the context of the parent thread overwrites the context of the child thread in the thread pool used to execute the task of the parent thread. The first member variable can be used to store the context of the parent thread. When executing the task of the parent thread, the context of the parent thread stored in the first member variable will be passed to the child thread in the thread pool used to execute the task of the parent thread.

[0042] When creating the first object in the parent thread, optionally, in one implementation, it can be created based on the existing thread pool task submission interface. Take the existing thread pool task submission interface as the Runnable interface as an example. Specifically, the Runnable interface includes a run method. When creating the first object based on the Runnable interface, the run method in the Runnable interface can be used as the execution method in the first object. At the same time, a context replacement method and a first member variable are added on the basis of the Runnable interface, thereby creating the first object.

[0043] The first member variable is used to store the context of the parent thread. To facilitate storing the context of the parent thread in the first member variable, optionally, when creating the first object in the parent thread, a second object can also be created in the parent thread. The second object includes a second member variable, which is also used to store the context of the parent thread. Specifically, when writing data to the context of the parent thread, the written data can also be captured and written to the second member variable, that is, the context of the parent thread is written to the second member variable, thereby storing the context of the parent thread in the second member variable. Afterwards, the parent thread context in the second member variable can be assigned to the first member variable in the first object, so that the context of the parent thread can be stored in the first member variable.

[0044] When creating a second object in the parent thread, optionally, in one implementation, it can be created based on the original inheritable thread local variable object of the parent thread. Specifically, the original inheritable thread local variable object of the parent thread includes a data writing method and a data reading method. When creating the second object, the data writing method and the data reading method in the inheritable thread local variable object can be redefined, that is, the specific implementation of the data writing method and the data reading method is changed, and at the same time, a second member variable is added to the inheritable thread local variable object, thereby creating the second object. Among them, the inheritable thread local variable object can be an InheritableThreadLocal object, the data writing method can be a set method, and the data reading method can be a get method.

[0045] After the second object is created based on the inheritable thread-local variable object, when the context of the parent thread is written into the second member variable of the second object, the written data may be captured and stored in the second member variable of the second object when the data is written into the context of the parent thread through the data writing method in the second object. That is to say, compared with the data writing method in the existing inheritable thread-local variable object, the data writing method redefined in the second object performs one more operation when writing data into the context of the parent thread, which is to collect the written data into the second member variable again, so that the context of the parent thread can be stored in the second member variable.

[0046] S104: Use the first object to create a task, and submit the task to the thread pool through the task submission interface of the thread pool.

[0047] Since the execution method, context replacement method and first member variable are defined in the first object, the first object can be used to create the task. In this way, when the task is subsequently executed by the child thread in the thread pool, the method defined in the first object can be called and the member variable defined in the first object can be used to realize the transfer of the parent thread context.

[0048] The task submission interface of the thread pool is an interface that must be implemented for tasks submitted to the thread pool. In an embodiment of the present application, the interface can be implemented by a first object, that is, when submitting a task to the thread pool, it can be implemented not based on the original task submission interface of the thread pool (such as the Runnable interface), but through the first object. Compared with the original task submission interface of the thread pool, in addition to the execution method for executing the task, the first object also adds a context replacement method and a first member variable, so that when the child thread in the thread pool executes the task of the parent thread later, the context of the parent thread can be passed to the child thread in the thread pool based on the first object.

[0049] Among them, the creation and submission of tasks can be implemented by the parent thread.

[0050] S106: When a child thread in the thread pool is assigned to execute a task, and before the child thread calls the execution method to execute the task, the context replacement method is called to overwrite the context of the child thread with the context stored in the first member variable.

[0051] After submitting the task of the parent thread to the thread pool, the thread pool can automatically assign a thread to execute the task. When assigning threads, the thread pool can assign threads based on existing logic. Here, we will not explain in detail how to assign threads. For the sake of distinction and understanding, the threads assigned by the thread pool can be represented as child threads.

[0052] It should be noted that when the first object is used to create a task in S104, multiple tasks can be created and submitted to the thread pool, and the thread pool can assign multiple sub-threads to process the multiple tasks in parallel. Among them, for each task, the thread pool can assign a sub-thread to execute, and each sub-thread can be implemented based on the same method when executing the task. Therefore, for the sake of convenience, the embodiment of the present application only takes creating a task and assigning a sub-thread to execute as an example for explanation.

[0053] In the case where the task is executed by the child thread in the thread pool, since the task submitted to the thread pool is created based on the first object, the child thread can call the method in the first object to execute the task when executing the task. Specifically, the context replacement method in the first object can be called first, and the context of the parent thread stored in the first member variable of the first object is used to overwrite the original context of the child thread, and then the execution method in the first object is called to execute the task. In this way, compared with the existing direct call to the run method to execute the task, since the embodiment of the present application improves the business execution logic, that is, the context replacement method called first overwrites the original context of the child thread with the context of the parent thread, and then the execution method is called to execute the task, the context of the parent thread can be accurately passed to the child thread in the thread pool, and the child thread executes the task based on the context of the parent thread, thereby realizing the transfer of the parent thread context and ensuring that the parent thread's task can be executed normally in the thread pool.

[0054] In an embodiment of the present application, in order to avoid the loss of the original context of the child thread, before calling the context replacement method to overwrite the context of the child thread with the context stored in the first member variable, the context of the child thread can also be backed up. Optionally, in one implementation, the original context of the child thread can be backed up by calling the context replacement method in the first object, that is, two operations can be performed when calling the context replacement method, namely, first backing up the original context of the child thread, and then overwriting the original context of the child thread with the context of the parent thread. In this way, by backing up the original context of the child thread, the loss of the context of the child thread can be avoided.

[0055] On the basis of backing up the original context of the child thread, after calling the execution method in the first object to execute the task, the backed-up context can also overwrite the current context of the child thread (the context of the parent thread), thereby restoring the context of the child thread. Optionally, in one implementation, when creating the first object, a context recovery method can be defined in the first object, and the context recovery method is used to restore the context. In this way, after calling the execution method in the first object to execute the task, the context recovery method in the first object can be called to overwrite the current context of the child thread (the context of the parent thread) with the backed-up context (the original context of the child thread), that is, to restore the context of the child thread to the context (the original context of the child thread) backed up when calling the context replacement method. In this way, the context of the child thread can be guaranteed to be consistent before and after executing the task of the parent thread.

[0056] In the case where the first object includes a context replacement method, a context recovery method and a first member variable, when creating the first object, optionally, it can also be created based on the existing thread pool task submission interface. Take the existing thread pool task submission interface as the Runnable interface as an example. Specifically, the Runnable interface includes a run method. When creating the first object based on the Runnable interface, the run method in the Runnable interface can be used as the execution method in the first object, and the context replacement method, the first member variable and the context recovery method are added to the Runnable interface, thereby creating the first object. In this way, when executing a task, the context replacement method in the first object can be called first to back up the original context of the child thread and use the context of the parent thread stored in the first member variable to overwrite the original context of the child thread, and then call the execution method in the first object to execute the task. After the task is executed, call the context recovery method in the first object to restore the original context of the child thread.

[0057] Based on the technical solution provided by the embodiment of the present application, by changing the implementation logic of the parent thread and the execution logic of the task, the context of the parent thread can be accurately passed to the thread in the thread pool, thereby ensuring that the task of the parent thread can be normally executed in the thread pool. Furthermore, since the original context of the child thread is backed up before the child thread in the thread pool executes the task, the original context of the child thread is restored after the task is executed, thus avoiding the loss of the context of the child thread and ensuring that the context of the child thread is consistent before and after the task is executed.

[0058] In order to facilitate understanding of the technical solutions provided in the embodiments of the present application, Figure 2 A more specific implementation method shown in FIG. is described in detail as an example. Figure 2In the illustrated embodiment, the first object may be represented as ContextRunnable, the execution method may be represented as the run method, the context replacement method may be represented as the replay method, the context recovery method may be represented as the restore method, and the first member variable may be represented as ThreadLocalValues. The second object may be represented as ContextThreadLocal, and the second member variable in the second object may be represented as ThreadLocalValues.

[0059] Figure 2 The illustrated embodiment includes three parts of implementation logic, namely, parent thread logic, thread pool logic and task logic. These three parts of implementation logic will be described below respectively.

[0060] (1) Parent thread logic

[0061] The embodiment of the present application improves the parent thread logic by using ContextThreadLocal to store the context of the parent thread and by using ContextRunnable to create tasks.

[0062] ContextThreadLocal contains member variables and methods such as Figure 3 As shown. ContextThreadLocal inherits from InheritableThreadLocal, overrides the set and get methods, and adds a ThreadLocalValues ​​member variable. When the parent thread writes data to the context through the set method as usual, the set method does something more, that is, collects the written data into the ThreadLocalValues ​​member variable again, so that the context of the parent thread can be restored when the thread in the thread pool executes the task created by the parent thread. Among them, from the perspective of business development, the operation of using ContextThreadLocal to store the context of the parent thread is the same as the normal use of InheritableThreadLocal. Writing data to the context of the current thread will not add additional business operations.

[0063] ContextRunnable contains member variables and methods such as Figure 4As shown. ContextRunnable implements the Runnable interface (this is the interface that tasks submitted to the thread pool must implement), can inherit from the Runnable interface, and adds the replay method, restore method, and contextThreadLocal member variable. The purpose of the replay method is: before the task is executed, the parent thread's context is restored under the current thread through this method, that is, the context of the current thread is overwritten (a new variable is used to back it up before overwriting). The purpose of the restore method is: after the task is executed, the original context of the current thread is restored through this method (the content backed up by the replay method is the context of the current thread, which can then be overwritten).

[0064] (2) Thread pool logic

[0065] The thread pool logic is still the Java logic, without any changes. After the parent thread logic submits the task (ContextRunnable) to the thread pool, the thread pool will automatically assign a thread to execute the task. Figure 2 As shown, the thread pool assigns thread 1 to execute the task.

[0066] (3) Task Logic

[0067] The task logic is implemented through ContextRunnable. The parent thread only needs to create a new ContextRunnable and then assign a value to the ContextThreadLocal variable. The embodiment of the present application improves the task logic. Specifically, when thread 1 in the thread pool executes the run method of ContextRunnable, the run method will first call the replay method to overwrite the context of thread 1 with the parent thread context in threadLocalValues ​​(and back it up at the same time), and then actually execute the task logic, and finally call the restore method to restore the original context of thread 1. Among them, from the perspective of business development, the extra steps are to use ContextRunnable instead of Java native Runnable, and assign a value to contextThreadLocal. There are no other additional operations.

[0068] The embodiment of the present application constructs a new task execution method, which can automatically pass the context of the parent thread into ContextThreadLocal to the execution thread in the thread pool. Before the execution thread actually executes the task of the parent thread, the correct context of the parent thread can be automatically restored, that is, the context of the parent thread overwrites the current context of the execution thread, so that the task execution can get the correct context data. After the task execution is completed, the original context of the execution thread can be automatically restored. Thus, the context data of the parent thread can be accurately, safely and conveniently transferred in the thread pool, ensuring the normal execution of the task and the correctness of the business logic. Compared with the existing solution, it overcomes the cumbersome and error-prone problems of manually passing context parameters.

[0069] The technical solution provided by the embodiment of the present application can be applied to various scenarios in which business processing is performed based on parent threads and thread pools. The following will use the financial risk control field to explain in detail how to apply the technical solution provided by the embodiment of the present application to perform business processing in this field.

[0070] In the field of financial risk control, when evaluating whether a user is eligible for a loan, Figure 5 The described embodiments are implemented. Figure 5 This is a flowchart of determining whether a user is eligible for a loan based on a task execution method according to an embodiment of the present application, which may include the following steps.

[0071] Step 1: The client passes the user ID to the server. The server queries the user's basic information (user ID, user name, ID number, mobile phone number, etc.) based on the user ID, and puts the queried user basic information into the ContextThreadLocal context of the parent thread.

[0072] Step 2: The server queries the user's credit report, the user's cumulative number of loans, the user's cumulative loan amount, liveness comparison, student status verification on China Higher Education Student Information and Career Center, personal tax payment information and other data based on the user's basic information. When querying, the server constructs multiple data source ContextRunnables, each ContextRunnable corresponds to a data source request task (i.e., multiple query tasks), and then submits the multiple data source ContextRunnables to the pre-created thread pool for concurrent execution. When executing tasks, the threads in the thread pool can pass the ContextThreadLocal context (i.e., the user's basic information) to multiple threads based on the task execution method provided in the embodiment of the present application. Multiple threads concurrently query the user's credit report, the user's cumulative number of loans, the user's cumulative loan amount, liveness comparison, student status verification on China Higher Education Student Information and Career Center, personal tax payment information and other data based on the user's basic information.

[0073] Step 3: After querying the corresponding results, the thread pool returns the results to the parent thread. At this time, the server can put all the returned results into the ContextThreadLocal context.

[0074] Step 4: The server further queries model information such as the capital risk model, cumulative credit model, and scientific decision-making model based on the query results of step 3. When querying, multiple query model tasks can also be created, that is, multiple model ContextRunnables are constructed, each ContextRunnable corresponds to a data source request task, and then multiple model ContextRunnables are submitted to the thread pool for concurrent execution. When executing tasks, the threads in the thread pool can pass the ContextThreadLocal context (i.e., the query results in step 3) to multiple threads based on the task execution method provided in the embodiment of the present application, and multiple threads concurrently query model information such as the capital risk model, cumulative credit model, and scientific decision-making model based on the query results in step 3.

[0075] Step 5: After querying the model information, the thread pool returns the model information to the parent thread. At this time, the server can determine whether the user is eligible for a loan based on the queried model information statistics, and then return the conclusion of whether the user is eligible for a loan to the client.

[0076] In the two queries in the above steps 1 to 5, based on the technical solution provided in the embodiment of the present application, the context of the parent thread (in the first query, the context is the user's basic information, and in the second query, the context is the user's various data queried based on the user's basic information) can be accurately passed to the thread in the thread pool, so that the corresponding results can be queried to ensure the normal execution of the business logic.

[0077] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0078] Figure 6 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present application. Figure 6At the hardware level, the electronic device includes a processor, and optionally also includes an internal bus, a network interface, and a memory. The memory may include a memory, such as a high-speed random access memory (RAM), and may also include a non-volatile memory (non-volatile memory), such as at least one disk storage. Of course, the electronic device may also include hardware required for other services.

[0079] The processor, network interface and memory can be interconnected through an internal bus, which can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 6 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0080] The memory is used to store the program. Specifically, the program may include a program code, and the program code includes a computer operation instruction. The memory may include a memory and a non-volatile memory, and provides instructions and data to the processor.

[0081] The processor reads the corresponding computer program from the non-volatile memory into the memory and then runs it, forming a task execution device at the logical level. The processor executes the program stored in the memory and is specifically used to perform the following operations:

[0082] Creating a first object in a parent thread, wherein the first object includes an execution method, a context replacement method, and a first member variable, wherein the first member variable is used to store the context of the parent thread;

[0083] Use the first object to create a task, and submit the task to the thread pool through the task submission interface of the thread pool;

[0084] When a child thread in the thread pool is assigned to execute the task, and before the child thread calls the execution method to execute the task, the context replacement method is called to overwrite the context of the child thread with the context stored in the first member variable.

[0085] The above application Figure 6The method performed by the task execution device disclosed in the illustrated embodiment can be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in the processor or an instruction in the form of software. The above processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application can be directly embodied as a hardware decoding processor for execution, or a combination of hardware and software modules in the decoding processor for execution. The software module can be located in a storage medium mature in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory, and the processor reads the information in the memory and completes the steps of the above method in combination with its hardware.

[0086] The electronic device may also perform Figure 1 , Figure 2 and Figure 5 The method and the task execution device are implemented in Figure 1 , Figure 2 and Figure 5 The functions in the illustrated embodiment will not be described in detail in the embodiments of the present application.

[0087] Of course, in addition to software implementation methods, the electronic device of the present application does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc. That is to say, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.

[0088] The embodiment of the present application also provides a computer-readable storage medium, which stores one or more programs, wherein the one or more programs include instructions, which, when executed by a portable electronic device including a plurality of application programs, enable the portable electronic device to execute Figure 1 , Figure 2 and Figure 5 The method of the embodiment shown is specifically used to perform the following operations:

[0089] Creating a first object in a parent thread, wherein the first object includes an execution method, a context replacement method, and a first member variable, wherein the first member variable is used to store the context of the parent thread;

[0090] Use the first object to create a task, and submit the task to the thread pool through the task submission interface of the thread pool;

[0091] When a child thread in the thread pool is assigned to execute the task, and before the child thread calls the execution method to execute the task, the context replacement method is called to overwrite the context of the child thread with the context stored in the first member variable.

[0092] Figure 7 This is a schematic diagram of the structure of a task execution device 70 according to an embodiment of the present application. Figure 7 In a software implementation, the task execution device 70 may include: a creation module 71, a submission module 72 and an execution module 73, wherein:

[0093] A creation module 71 is used to create a first object in a parent thread, wherein the first object includes an execution method, a context replacement method and a first member variable, and the first member variable is used to store the context of the parent thread;

[0094] A submission module 72, using the first object to create a task, and submitting the task to the thread pool through a task submission interface of the thread pool;

[0095] The execution module 73 calls the context replacement method to overwrite the context of the child thread with the context stored in the first member variable when the child thread in the thread pool is assigned to execute the task and before the child thread calls the execution method to execute the task.

[0096] Optionally, the creation module 71 further includes:

[0097] Creating a second object in the parent thread, wherein the second object includes a second member variable;

[0098] Capturing the context of the parent thread and writing it into the second member variable;

[0099] The context written into the second member variable is assigned to the first member variable.

[0100] Optionally, the creation module 71 creates the second object in the parent thread, including:

[0101] The data writing method and the data reading method in the inheritable thread local variable object of the parent thread are redefined, and the second member variable is newly added to the inheritable thread local variable object to obtain the second object.

[0102] Optionally, the creation module 71 captures the context of the parent thread and writes the second member variable, including:

[0103] When data is written into the context of the parent thread through the data writing method, the written data is captured and stored in the second member variable.

[0104] Optionally, before calling the context replacement method to overwrite the context of the child thread with the context stored in the first member variable, the execution module 73 further includes:

[0105] The context replacement method is called to back up the context of the child thread.

[0106] Optionally, the first object further includes a context recovery method; after the execution module 73 calls the execution method to execute the task, it further includes:

[0107] The context recovery method is called to restore the context of the child thread to the context backed up when the context replacement method is called.

[0108] Optionally, the creation module 71 creates the first object in the parent thread, including:

[0109] The context replacement method, the context recovery method and the first member variable are newly added to the task submission interface of the thread pool to obtain the first object.

[0110] The task execution device 70 provided in the embodiment of the present application can also execute Figure 1 , Figure 2 and Figure 5 The method and the task execution device are implemented in Figure 1 , Figure 2 and Figure 5 The functions of the illustrated embodiment will not be described in detail in the embodiments of the present application.

[0111] In short, the above description is only a preferred embodiment of the present application and is not intended to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.

[0112] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.

[0113] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0114] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0115] Each embodiment in this application is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.

Claims

1. A task execution method, characterized in that: include: Creating a first object in a parent thread, wherein the first object includes an execution method, a context replacement method, and a first member variable, wherein the first member variable is used to store the context of the parent thread; Use the first object to create a task, and submit the task to the thread pool through the task submission interface of the thread pool; When a child thread in the thread pool is assigned to execute the task, and before the child thread calls the execution method to execute the task, the context replacement method is called to overwrite the context of the child thread with the context stored in the first member variable.

2. The task execution method according to claim 1, characterized in that: Also includes: Creating a second object in the parent thread, wherein the second object includes a second member variable; Capturing the context of the parent thread and writing it into the second member variable; The context written into the second member variable is assigned to the first member variable.

3. The task execution method according to claim 2, characterized in that: The step of creating a second object in the parent thread includes: The data writing method and the data reading method in the inheritable thread local variable object of the parent thread are redefined, and the second member variable is newly added to the inheritable thread local variable object to obtain the second object.

4. The task execution method according to claim 3, characterized in that: The capturing the context of the parent thread and writing the second member variable comprises: When data is written into the context of the parent thread through the data writing method, the written data is captured and stored in the second member variable.

5. The task execution method according to claim 1, characterized in that: Before calling the context replacement method to overwrite the context of the child thread with the context stored in the first member variable, the method further includes: The context replacement method is called to back up the context of the child thread.

6. The task execution method according to claim 5, characterized in that: The first object also includes a context recovery method; after calling the execution method to execute the task, it also includes: The context recovery method is called to restore the context of the child thread to the context backed up when the context replacement method is called.

7. The task execution method according to claim 6, characterized in that: The step of creating a first object in the parent thread includes: The context replacement method, the context recovery method and the first member variable are added to the task submission interface of the thread pool to obtain the first object.

8. A task execution device, characterized in that: include: A creation module is used to create a first object in a parent thread, wherein the first object includes an execution method, a context replacement method, and a first member variable, and the first member variable is used to store the context of the parent thread; A submission module, using the first object to create a task, and submitting the task to the thread pool through the task submission interface of the thread pool; The execution module calls the context replacement method to overwrite the context of the child thread with the context stored in the first member variable when the child thread in the thread pool is assigned to execute the task and before the child thread calls the execution method to execute the task.

9. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: When the instructions in the storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the method as claimed in any one of claims 1 to 7.