Thread determination method and device, electronic equipment and computer readable medium

The global thread pool is used to schedule threads to execute tasks from the candidate threads of the target application, realize thread reuse, and dynamically adjust the number of thread pool threads, solving the memory overflow and unresponsiveness problems caused by threads occupying resources in complex applications, and improving operation Effect.

CN118535294APending Publication Date: 2024-08-23BEIJING ZITIAO NETWORK TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202310188076.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-02-22
Publication Date
2024-08-23

AI Technical Summary

Technical Problem

During the running of complex applications, a large number of threads occupy resources, causing memory overflow and unresponsiveness, which affects the running effect.

Method used

Using the thread reuse method, threads are scheduled from the candidate threads of the target application to execute the tasks to be executed through the global thread pool, and the number of threads in the thread pool is dynamically adjusted to adapt to task requirements.

Benefits of technology

It effectively reduces the waste of thread resources, improves the running effect and user experience of the application, and avoids memory overflow and unresponsiveness problems.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118535294A
    Figure CN118535294A
Patent Text Reader

Abstract

The invention discloses a thread determination method and device, electronic equipment and a computer readable medium, and the method comprises the steps that after a global thread pool receives a to-be-executed task in a target application, the global thread pool receives the to-be-executed task from at least one candidate thread corresponding to the target application, and scheduling the target thread to execute the to-be-executed task in some threads created for the target application, so that the purpose of multiplexing the target thread can be achieved, the purpose of converging the threads by means of a thread multiplexing mode can be achieved, and the application running effect can be improved.
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 thread determination method, device, electronic device, and computer-readable medium. Background Art

[0002] For some operating systems (eg, Android), in order to improve concurrency, some operations (eg, some time-consuming operations) can usually be executed in child threads.

[0003] In fact, for an application, if the application is relatively complex, there may be a large number of threads running at the same time. At this time, these threads will occupy a large amount of resources (for example, computing resources or memory resources, etc.), which may cause the application to be prone to problems such as Out of Memory (OOM) and Application Not Response (ANR), thereby affecting the operation of the application. Summary of the invention

[0004] In order to solve the above technical problems, the present application provides a thread determination method, device, electronic device and computer-readable medium, which can achieve the purpose of thread convergence by means of thread multiplexing, thereby helping to improve the application operation effect.

[0005] In order to achieve the above objectives, the technical solutions provided by this application are as follows:

[0006] The present application provides a thread determination method, which is applied to a global thread pool, and the method comprises:

[0007] Receive tasks to be executed in the target application;

[0008] A target thread is scheduled to execute the task to be executed from at least one candidate thread corresponding to the target application; the candidate thread refers to a thread that has been created for the target application.

[0009] In a possible implementation manner, receiving a task to be executed in a target application includes:

[0010] Receive the thread object corresponding to the task to be executed; the thread object is obtained by instantiating a pre-built thread base class; the thread object is used to describe the task to be executed; the thread base class is determined based on the existing thread classes in the preset language system; the start method of the thread base class is different from the start method of the thread class; the thread object is submitted to the global thread pool by calling the start method of the thread base class.

[0011] In a possible implementation manner, the thread base class is obtained by rewriting and customizing at least one item of configuration information of the thread class; the at least one item of configuration information includes the startup method.

[0012] In a possible implementation manner, the project file of the target application contains information on using the thread base class, but does not contain information on using the thread class.

[0013] In a possible implementation manner, the process of determining the project file includes:

[0014] Obtaining the original project file corresponding to the target application; the original project file contains information using the above thread class; the original project file includes several class files;

[0015] Traversing the several types of files;

[0016] When a class described in a class file meets the thread subclass condition, the parent class of the described class is modified to the thread base class;

[0017] When the class described by a class file does not meet the thread subclass condition, create instructions in all methods of the described class;

[0018] When the class name of the thread class exists in a creation instruction, the class name of the thread class existing in the creation instruction is modified to the class name of the thread base class.

[0019] In a possible implementation manner, scheduling a target thread from at least one candidate thread corresponding to the target application to execute the task to be executed includes:

[0020] Selecting a target thread from the at least one candidate thread;

[0021] Determine a to-be-used thread pool corresponding to the target thread from a plurality of service thread pools corresponding to the target application;

[0022] The task to be executed is allocated to the thread pool to be used; the thread pool to be used is used to execute the task to be executed using the target thread.

[0023] In a possible implementation manner, the service thread pool satisfies at least one of the following conditions:

[0024] The business thread pool is configured to allow timeout;

[0025] The business thread pool uses a non-blocking interface to obtain tasks.

[0026] In a possible implementation manner, the method further includes:

[0027] Periodically checking the task waiting queue of the global thread pool;

[0028] If there is an intersection between the task waiting queue of the current cycle and the task waiting queue of the previous cycle, the number of threads in the global thread pool is increased.

[0029] In a possible implementation manner, after submitting the task to be executed to the global thread pool, the method further includes:

[0030] Determine whether the task waiting queue is empty;

[0031] The periodic detection of the task waiting queue of the global thread pool includes:

[0032] If it is determined that the task waiting queue is not empty, the task waiting queue is periodically checked.

[0033] In a possible implementation manner, increasing the number of threads in the global thread pool includes:

[0034] According to the first number, increase the number of threads in the global thread pool;

[0035] The method further comprises:

[0036] When a preset reduction condition is reached, the number of threads in the global thread pool is reduced according to a second number; the second number is smaller than the first number.

[0037] In a possible implementation manner, the first number is determined according to the number of tasks in the intersection.

[0038] The present application provides a thread determination device, comprising:

[0039] A receiving unit, used for receiving a task to be executed in a target application;

[0040] The submitting unit is used to schedule a target thread from at least one candidate thread corresponding to the target application to execute the task to be executed; the candidate thread refers to a thread that has been created for the target application.

[0041] The present application provides an electronic device, the device comprising: a processor and a memory;

[0042] The memory is used to store instructions or computer programs;

[0043] The processor is used to execute the instructions or computer programs in the memory so that the electronic device executes the thread determination method provided in the present application.

[0044] The present application provides a computer-readable medium, in which instructions or computer programs are stored. When the instructions or computer programs are executed on a device, the device executes the thread determination method provided by the present application.

[0045] The present application provides a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program contains program codes for executing the thread determination method provided by the present application.

[0046] Compared with the prior art, this application has at least the following advantages:

[0047] In the technical solution provided by the present application, for a terminal device, when a target application is deployed on the terminal device and the target application is in a running state, after obtaining the task to be executed in the target application, the task to be executed is first submitted to the global thread pool, so that the global thread pool schedules the target thread to execute the task to be executed from at least one candidate thread corresponding to the target application (that is, some threads created for the target application). In this way, the purpose of reusing the target thread can be achieved, thereby achieving the purpose of converging threads through thread reuse, which is beneficial to improving the application operation effect. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] 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 any creative work.

[0049] Figure 1 A flowchart of a thread determination method provided in an embodiment of the present application;

[0050] Figure 2 A schematic diagram of a global resource pool provided in an embodiment of the present application;

[0051] Figure 3 A schematic diagram of an information adjustment process of a project file provided in an embodiment of the present application;

[0052] Figure 4 A schematic diagram of a method for determining a delayed task provided in an embodiment of the present application;

[0053] Figure 5 A schematic diagram of a process for dynamically adjusting the number of threads provided in an embodiment of the present application;

[0054] Figure 6A schematic diagram of the structure of a thread determination device provided in an embodiment of the present application;

[0055] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0056] Through research, it is found that for a certain application, in order to achieve the purpose of thread convergence, a thread pool can be directly encapsulated for each business in the application, so that the thread pool can use some threads to execute the tasks under its corresponding business. Among them, because these thread pools are independent of each other, all tasks under the same business can only be executed with the help of threads existing in the thread pool corresponding to the business. Therefore, when the thread pool corresponding to a business cannot meet the thread requirements of the business, the thread pool corresponding to the business cannot reuse the idle threads in the thread pools corresponding to other businesses to execute tasks, which easily leads to poor reuse of thread resources.

[0057] Based on the research content shown in the previous paragraph, the present application provides a thread determination method, which includes: for a terminal device, when a target application is deployed on the terminal device and the target application is in a running state, after obtaining the task to be executed in the target application, the task to be executed is first submitted to the global thread pool, so that the global thread pool schedules the target thread to execute the task to be executed from at least one candidate thread corresponding to the target application (that is, some threads created for the target application), so that the purpose of reusing the target thread can be achieved, thereby achieving the purpose of converging threads through thread reuse, which is beneficial to improving the application operation effect.

[0058] It can be seen that the global thread pool above can directly call all threads created for the target application. It does not care about which business these threads are created for, but only cares whether these threads can be called. This can effectively overcome the defect that threads in thread pools corresponding to different businesses cannot be used across businesses, thereby effectively improving the thread reuse effect, and then better achieve the purpose of convergent threads with the help of thread reuse, which is conducive to improving the application operation effect.

[0059] In addition, the present application does not limit the execution subject of the above thread determination method. For example, the thread determination method provided in the embodiment of the present application can be applied to a device with data processing function such as a terminal device or a server. For another example, the thread determination method provided in the embodiment of the present application can also be implemented by means of a data communication process between different devices (for example, a terminal device and a server, two terminal devices, or two servers). Among them, the terminal device can be a smart phone, a computer, a personal digital assistant (PDA) or a tablet computer. The server can be a stand-alone server, a cluster server or a cloud server.

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

[0061] In order to better understand the technical solution provided by the present application, the thread determination method provided by the present application is described below with reference to some drawings. Figure 1 As shown, the thread determination method applied to the global thread pool provided in the embodiment of the present application includes the following S1-S2. Figure 1 A flowchart of a thread determination method provided in an embodiment of the present application.

[0062] S1: The global thread pool receives tasks to be executed in the target application.

[0063] Among them, the target application refers to an application (Application, APP) deployed on a terminal device and having thread resource requirements; and when the target application is in operation, some threads will be created on the terminal device (for example, some threads are created for each business in the target application), so that these threads can be used to complete some tasks involved in the target application (for example, page building tasks, data loading tasks, etc.)

[0064] The pending tasks refer to tasks that need to be executed with the help of thread resources when the target application is in the running state; and the present application does not limit the pending tasks, for example, it can refer to any task in the target application that needs to be executed with the help of thread resources.

[0065] In addition, the present application does not limit the receiving process of the above global thread pool for the task to be executed. For example, when the global thread pool is deployed on an electronic device, it may specifically include the following steps 1-2.

[0066] Step 1: The electronic device obtains the task to be executed in the target application.

[0067] It should be noted that the present application does not limit the implementation method of step 1. For example, it can be implemented by any existing or future method that can obtain the task of an APP.

[0068] For another example, in some application scenarios (e.g., Android system), a task that requires thread resources can be represented by a Thread object, so that the acquisition process of the task to be executed can be implemented with the help of the acquisition process of a Thread object. Among them, the Thread object can usually be created with the instruction new Thread(new TaskRunnable()); and the TaskRunnable() object is used to describe the logic that needs to be executed in the thread (that is, the TaskRunnable() object is used to describe a task that needs to be executed with the help of thread resources).

[0069] In fact, for the above Thread, by analyzing the source code of the Thread (for example, the code shown in Table 1 below), we can know that: ① Because the Runnable object it passes in (for example, the "TaskRunnable() object" in the above paragraph) is private, it is impossible to directly obtain the Runnable object. ② Because Thread implements the Runnable interface, and its run method simply calls the run method of the target, the Thread object can be regarded as a proxy for the Runnable object (that is, the Thread object can be used to directly represent the Runnable object). Based on this, it can be seen that in one possible implementation, the Thread object can be directly used as the Runnable object of a task.

[0070] Based on the above content and Table 1 below, it can be seen that in some application scenarios (for example, Android system), the present application also provides a possible implementation of step 1 above, which can be specifically: the electronic device instantiates the pre-built thread base class to obtain the thread object corresponding to the task to be executed above (for example, Figure 2The thread object 1) shown in the figure can describe the task to be executed, so that the thread object can act as a proxy for the task to be executed, and then the thread object can be used as the task to be executed later.

[0071]

[0072] Table 1 Tread classes in the Java language system

[0073] The “thread base class” mentioned above refers to an inheritable class determined in advance based on an existing thread class in a preset language system; and the start method of the thread base class is different from the start method of the thread class. Among them, the preset language system refers to the language system (for example, Java) adopted by the operating system (for example, Android) in the terminal device where the target application is deployed. The thread class refers to a class that has been pre-set in the preset language system. For example, it may refer to the Tread class represented by the source code in Table 1 above. The “start method of the thread base class” is used to implement the submission of the thread object instantiated based on the thread base class to the global thread pool (for example, Figure 2 The global thread pool shown in FIG. 1 is used to realize the submission process of the task described by the thread object by means of the submission process of the thread object. The "start method of the thread class" is used to realize the application of real thread resources. For the relevant content of the "global thread pool", please refer to the relevant content of S2 below.

[0074] In addition, the present application does not limit the construction process of the above "thread base class". For example, it can be specifically: rewrite and customize at least one configuration information of the existing thread class in the preset language system to obtain the thread base class, so that the thread base class can submit the task in Thread to the global thread pool (for example, Figure 2 The purpose of scheduling execution in the global thread pool shown in the figure is to schedule execution in the global thread pool shown in the figure so that the thread base class can be used to replace the thread class in the thread resource determination process for any task. Among them, the "at least one configuration information" refers to the customized information that needs to be rewritten in the process of determining the thread base class; and the "at least one configuration information" can be determined according to the actual application scenario. In addition, the present application does not limit the implementation method of the "at least one configuration information", for example, it can at least include a start method.

[0075] In fact, for the Java language system, it can generally first create a thread object through the instruction "new Thread(newTaskRunnable());" and then call the Thread.start method (for example, the source code of the Thread.start method shown in Table 1 above) to apply for real thread resources. In addition, since whether it is ThreadPoolExecutor, ScheduledThreadPoolExecutor, HandlerThread, etc., they all apply for thread resources through the above process, it can be determined that the Thread.start method is the only entry for the Java language system to apply for thread resources. Based on this, in order to achieve the purpose of thread reuse, the content related to thread resource application involved in the source code of Thread above can be rewritten and customized to obtain a rewritten and customized thread base class, so that the thread base class can achieve the purpose of not applying for real thread resources, but forwarding the corresponding tasks to the global process pool for scheduling and execution.

[0076] Based on the content of the previous paragraph and the relevant content of the Thread.start method, it can be known that in order to achieve the effect shown in the previous paragraph, the Thread.start method can be rewritten to achieve the purpose of submitting the Thread object directly to the global thread pool for scheduling and execution. It can be seen that in a possible implementation, the startup method of the thread base class above is obtained by rewriting the startup method of the thread class above; and the startup method of the thread base class is used to submit the thread object instantiated based on the thread base class to the global thread pool, so that the submission process of the task described by the thread object can be completed with the help of the submission process of the thread object.

[0077] Based on the relevant content of the thread base class above, it can be known that the thread base class can be obtained by rewriting and customizing the existing thread class in the preset language system, so that the start method of the thread base class is different from the start method of the thread class, so that the thread base class can achieve the purpose of forwarding the corresponding task to the global process pool for scheduling and execution.

[0078] In addition, the present application does not limit the determination process of the above thread object. For example, it can be specifically as follows: based on the task logic representation object of the task to be executed above, the pre-constructed thread base class is instantiated to obtain the thread object corresponding to the task to be executed. Among them, the "task logic representation object of the task to be executed" is used to characterize the abstract logic of the task to be executed, so that the "task logic representation object of the task to be executed" can describe the task to be executed. For example, the "task logic representation object of the task to be executed" can be implemented using the above TaskRunnable() object. It should be noted that the present application does not limit the method of obtaining the "task logic representation object of the task to be executed".

[0079] In fact, in some application scenarios, in order to better submit the tasks to be executed to the global thread pool, the present application also provides a possible implementation method of the above thread object determination process, which may specifically include the following steps 11 to 13.

[0080] Step 11: Obtain the original project file corresponding to the target application described above; the original project file contains information about using the thread class described above.

[0081] The project file refers to the file required to be used when the target application is running; and the present application does not limit the project file, for example, the project file at least includes several class files. The class file is used to record the classes required to be used when the target application is running.

[0082] The above "information using the above thread class" refers to the content related to the thread class recorded in the above project file; and this application does not limit the "information using the above thread class", for example, it may at least include: subclasses of the thread class, and creation instructions including the type of the thread class, etc.

[0083] Step 12: According to the above thread base class, the information of using the thread class in the above project file is adjusted so that the adjusted project file contains information of using the thread base class.

[0084] It should be noted that the present application does not limit the implementation method of the above step 12. For example, when the above project file includes several types of files, the step 12 may specifically include the following steps 121 to 124.

[0085] Step 121: Traverse the above several types of files.

[0086] In this application, for a target application, the project file of the target application may include several class files, so after obtaining these class files, the relevant files that need to replace the thread class with the thread base class can be found by traversing all class files. That is, for the target application, all class files of the target application can be traversed to ensure that all information related to the thread class involved in the target application is modified, which is conducive to improving the thread convergence effect.

[0087] Step 122: When a class described in a class file meets the thread subclass condition, the parent class of the described class is modified to be a thread base class.

[0088] The thread subclass condition refers to the condition satisfied by the subclass of the thread class mentioned above; and the present application does not limit the thread subclass condition. For example, it can be specifically: for the nth class file, if the class described by the nth class file is a custom Tread class, then it can be determined that the class described by the nth class file satisfies the thread subclass condition. For another example, it can also be: if it is determined that the parent class of the class described by the nth class file is the thread class mentioned above, then it can be determined that the class described by the nth class file satisfies the thread subclass condition. Where n is a positive integer, n≤N, N is a positive integer, and N represents the total number of class files of the target application mentioned above.

[0089] Based on the relevant content of step 122 above, it can be known that for the nth class file corresponding to the target application above, if the class described by the nth class file is a custom Tread class, it can be determined that the class described by the nth class file meets the thread subclass condition, and thus it can be determined that the class described by the nth class file belongs to a subclass of the above thread class, so the parent class of the class described by the nth class file can be directly modified to the above thread base class.

[0090] Step 123: When the class described by a class file does not satisfy the thread subclass condition, create instructions in all methods of the described class.

[0091] In this application, for the nth class file corresponding to the target application above, if the class described by the nth class file is not a custom Tread class, it can be determined that the class described by the nth class file does not meet the thread subclass condition, and thus it can be determined that the class described by the nth class file does not belong to the subclass of the thread class above, so it can be further traversed to create instructions (for example, NEW instructions) in all methods of the class described by the nth class file, so as to be able to continue to confirm whether the class name of the thread class above exists in the creation instruction. The creation instruction is used to create something; and this application does not limit the creation instruction, for example, it can be a NEW instruction.

[0092] Step 124: When the class name of the thread class mentioned above exists in a creation instruction, the class name of the thread class existing in the creation instruction is modified to the class name of the thread base class mentioned above.

[0093] In the present application, for the mth creation instruction existing in all methods of the class described by the nth class file, if the class name of the thread class mentioned above (for example, the string "Thread") exists in the mth creation instruction, it can be determined that the mth creation instruction belongs to the thread creation instruction, so the class name of the thread class existing in the mth creation instruction can be directly modified to the class name of the thread base class mentioned above, so that the class name of the thread base class exists in the modified mth creation instruction, but the class name of the thread class does not exist. Among them, the thread creation instruction is used to create a thread; and the embodiment of the present application does not limit the thread creation instruction, for example, the thread creation instruction satisfies the following conditions: the class name Thread (that is, the class name of the thread class mentioned above) exists in the NEW instruction. m is a positive integer, m≤M, M is a positive integer, and M represents the total number of creation instructions existing in all methods of the target class mentioned above.

[0094] Based on the relevant contents of steps 121 to 124 above, it can be known that for the target application above, when the original project file corresponding to the target application includes several types of files, such as Figure 3 As shown, all class files can be traversed first to determine whether the class described by the current class file meets the thread subclass condition (for example, to determine whether the class described by the current class file is a custom Thread class). If yes, the parent class of the class described by the current class file is modified to the thread base class mentioned above; if not, the creation instructions (for example, NEW instructions) in all methods of the class described by the current class file are traversed, and it is determined whether the class name followed by the creation instruction is the class name of the thread class mentioned above (for example, Thread). If yes, it is changed to the class name of the thread base class mentioned above. Otherwise, the search continues, and after the search is completed, the next class file is traversed.

[0095] Based on the relevant content of step 12 above, it can be known that for the target application, the thread class related information involved in the project file of the target application can be replaced with the relevant information of the thread base class (for example, all business Threads of the target application are inherited to the thread base class, etc.) with the help of the hook method for Java Thread, so that the thread base class can be used in the subsequent operation of the target application to assist in completing the thread resource determination process for each task.

[0096] Step 13: Instantiate the pre-built thread base class to obtain the thread object corresponding to the task to be executed above, so that the thread object is used to describe the task to be executed.

[0097] It should be noted that the relevant content of step 13 is mentioned above, and for the sake of brevity, it will not be repeated here.

[0098] Based on the relevant contents of steps 11 to 13 above, it can be known that for the target application, the thread class involved in the target application can be hooked to replace the thread class related information involved in the project file of the target application with the relevant information of the thread base class; and the thread base class can be rewritten and customized based on the thread class so that the thread base class can achieve the purpose of submitting the task directly to the global thread pool for scheduling and execution. In this way, the update and replacement process of the thread resource determination process in the project file of the target application can be realized with the help of two processes.

[0099] Based on the relevant content of step 1 above, it can be known that for a terminal device deployed with a target application, when the target application is running on the terminal device, if one wants to use thread resources to implement the above-mentioned task to be executed, the thread object corresponding to the task to be executed can be determined by instantiating the pre-built thread base class, so that the thread object can represent the task to be executed to participate in the subsequent thread determination process (for example, the process shown in S2 below).

[0100] Step 2: The electronic device submits the above tasks to be executed to the global thread pool.

[0101] It should be noted that this application does not limit the task submission method involved in the global thread pool in step 2 above. For example, it can be specifically: after the electronic device obtains the thread object corresponding to the task to be executed above, call the start method of the thread base class to submit the thread object to the global thread pool, so that the purpose of submitting the task to be executed to the global thread pool can be achieved with the help of the submission process of the thread object. Among them, the relevant content of the "start method of the thread base class" can be found above, and for the sake of brevity, it will not be repeated here.

[0102] Based on the relevant content of steps 1 to 2 above, it can be known that for an electronic device deployed with a global thread pool, after the electronic device obtains a thread object corresponding to a task, the electronic device can use the thread object to replace the task and submit it directly to the global thread pool, so that the global thread pool can subsequently perform thread determination processing for the task.

[0103] It can be seen that, in a possible implementation mode, the above S1 can specifically be: receiving the thread object corresponding to the task to be executed above; the thread object is obtained by instantiating a pre-built thread base class; the thread object is used to describe the task to be executed; the thread base class is determined based on the existing thread classes in the preset language system; the start method of the thread base class is different from the start method of the thread class; the thread object is submitted to the global thread pool by calling the start method of the thread base class.

[0104] Based on the relevant content of S1 above, it can be known that for a global thread pool deployed on an electronic device, the global thread pool can receive the task (or the thread object corresponding to the task) acquired by the electronic device, so that the global thread pool can subsequently perform thread determination processing for the task.

[0105] S2: The global thread pool schedules a target thread from at least one candidate thread corresponding to the target application to execute the task to be executed; the candidate thread refers to a thread that has been created for the target application.

[0106] The global thread pool is used to centrally manage and schedule all threads. For example, when the target application includes several services, and the service thread pool corresponding to each service is used to perform tasks with the help of several threads, the global thread pool is used to centrally manage the threads that can be used by all these service thread pools. For example, the global thread pool can be Figure 2 The global thread pool shown.

[0107] In addition, for the global thread pool mentioned above, the global thread pool can be used to schedule the same thread to execute different tasks, so that multiple tasks with thread resource requirements can be processed by only one thread, thereby effectively reducing thread overhead. Figure 2 For the global thread pool shown in FIG. 1 , after the thread object 1, the thread object 2, and the thread object 3 are submitted to the global thread pool, the global thread pool may only need to reuse one thread (for example, Figure 2 The thread 1) shown can complete the execution process for the thread object 1 to the thread object 3, which can effectively avoid unnecessary thread overhead.

[0108] In addition, the present application does not limit the implementation method of the above global thread pool. For example, it may refer to a resource pool that is pre-encapsulated for the above target application and has the function of uniformly scheduling all threads created for the target application.

[0109] The “candidate thread” mentioned above refers to a thread that has been created for the target application mentioned above, so that the candidate thread can be used to execute tasks under the target application.

[0110] In addition, the present application does not limit the above "at least one candidate thread corresponding to the target application", for example, it may include all threads that have been created for the above target application. That is, when the above target application includes several services, and a service thread pool is encapsulated for each service, the "at least one candidate thread corresponding to the target application" may include all threads that can be used by the first service thread pool, all threads that can be used by the second service thread pool, ..., all threads that can be used by the Kth service thread pool. Wherein, K is a positive integer, and K represents the number of services in the "several services".

[0111] It should be noted that the present application does not limit the creation method and creation timing of the above “at least one candidate thread corresponding to the target application”.

[0112] The "target thread" in the above text refers to the thread used to execute the task to be executed in the above text. For example, when the task to be executed can be executed using Figure 2 When the thread object 1 shown in the figure is used as a proxy, the target thread may refer to Figure 2 Thread 1 is shown.

[0113] In addition, the present application does not limit the determination process of the target thread described above, for example, it may include the following steps 21 to 23.

[0114] Step 21: The global thread pool selects a target thread from at least one candidate thread.

[0115] In this application, for the global thread pool, after the global thread pool receives the task to be executed (or, the thread object used to proxy the task to be executed), the global thread pool can select a thread as the target thread from all threads that have been created for the target application above, so that the task to be executed (or, the thread object used to proxy the task to be executed) can be executed on the target thread later. It should be noted that this application does not limit the screening process of the target thread. For example, it can be implemented using thread scheduling rules pre-set for the global thread pool.

[0116] Step 22: The global thread pool determines a to-be-used thread pool corresponding to the target thread from a number of business thread pools corresponding to the target application.

[0117] Among them, the business thread pool is used to use the threads specified by the global thread pool above to execute its corresponding tasks; and this application does not limit the business thread pool. For example, it may refer to a thread pool pre-packaged for a project in the target application above.

[0118] That is, for any business thread pool, the business thread pool can not only reuse all threads of the business thread pool according to its own configured thread reuse rules; it can also obey the thread reuse instructions sent by the global thread pool above and reuse all threads of the business thread pool. This is conducive to making full use of all threads of the business thread pool, thereby better improving the reuse rate of all threads of the business thread pool, and further better improving the thread reuse effect.

[0119] In fact, for any business thread pool, in some application scenarios, the business thread pool usually implements thread reuse through blocking queues, so that the release of threads in the business thread pool depends on the result of getting tasks from the blocking queue; and the business thread pool usually uses the getTask source code to get tasks. However, through the analysis of the getTask source code, it can be seen that when getting tasks from the blocking queue, it uses blocking APIs such as poll and take, so even if there are no tasks in the business thread pool, the thread resources will not be released immediately, which can easily affect the thread reuse rate.

[0120] In fact, for the above-mentioned business thread pool, the purpose of using blocking APIs such as poll and take for task acquisition processing is to avoid frequent creation and destruction of threads. However, in this application, because thread resources are uniformly scheduled by the global thread pool, so that the threads released by the business thread pool will not be truly destroyed, but returned to the global thread pool for scheduling, in order to better improve the use effect of thread resources, this application also provides a possible implementation of the business thread pool, in which the business thread pool can achieve the effect that when there is no task in the business thread pool, the business thread pool will release thread resources as quickly as possible.

[0121] After research, it was found that in order to achieve the above effect, it is necessary to destroy the thread keep-alive conditions involved in the above business thread pool; and through analysis of the above getTask source code, it can be seen that the release of threads by the business thread pool needs to meet the two conditions of exceeding the number of threads or obtaining the task timeout.

[0122] Based on the above content, in order to achieve the goal of releasing thread resources as quickly as possible when there are no tasks in the business thread pool, we can start with task timeout, and task timeout also needs to meet two conditions: timeout allowed and timeout. Among them, the first condition can be solved in the following way: allow the core thread pool to time out when constructing the method and override the allowCoreThreadTimeOut method to avoid the business closing this logic. The second condition can be achieved by proxying the BlockingQueue object and overriding blocking methods such as take and poll, so as to modify the non-blocking API involved in the getTask source code above into a non-blocking API, so that tasks can be obtained with the help of non-blocking APIs in the future, which can effectively avoid invalid waiting when there are no tasks.

[0123] Based on the above three analyses, it can be seen that in one possible implementation mode, the above business thread pool satisfies at least one of the conditions shown in ①-② below, so that the business thread pool can achieve the effect that when there is no task in the business thread pool, the business thread pool will release thread resources as quickly as possible, thereby effectively improving the thread reuse rate.

[0124] ①The business thread pool is configured to allow timeout.

[0125] ②The business thread pool uses a non-blocking interface to acquire tasks.

[0126] Based on the relevant content of the business thread pool above, it can be seen that for the business thread pool encapsulated for the target application, the business thread pool can use the threads specified by the global thread pool above to execute its corresponding tasks, and the business thread pool can achieve the effect that when there is no task in the business thread pool, the business thread pool will release thread resources as quickly as possible, which is conducive to improving the thread reuse rate.

[0127] The above "thread pool to be used" refers to the business thread pool that can use the above target thread to execute the above task to be executed; and this application does not limit the determination process of the thread pool to be used. For example, it can be specifically: find the business thread pool corresponding to the above target thread from the pre-built mapping relationship and determine it as the thread pool to be used. Among them, the mapping relationship is used to record the threads that can be used by each business thread pool corresponding to the above target application.

[0128] Based on the relevant content of step 22 above, it can be known that for the global thread pool above, after the global thread pool schedules the target thread to the task to be executed above, the thread pool to be used corresponding to the target thread can be determined from all business thread pools corresponding to the target application above, so that the thread pool to be used can subsequently use the target thread to execute the task to be executed.

[0129] Step 23: The global thread pool allocates the above-mentioned task to be executed to the thread pool to be used, so that the thread pool to be used is used to execute the task to be executed using the above-mentioned target thread.

[0130] In the present application, for the above-mentioned global thread pool, after the global thread pool schedules the target thread to the above-mentioned task to be executed, if the global thread pool determines that the above-mentioned thread pool to be used can use the target thread to execute the task, then the global thread pool can directly assign the above-mentioned task to be executed to the thread pool to be used, so that the thread pool to be used can use the target thread to execute the task to be executed, thereby achieving the purpose of scheduling the execution of the task to be executed by the global thread pool.

[0131] Based on the relevant contents of steps 21 to 23 above, it can be known that for the global thread pool provided in the present application, after the global thread pool receives the task to be executed (or, the thread object used to proxy the task to be executed), the global thread pool can first filter out a thread from all the threads that have been created for the target application above; then assign the task to be executed to the business thread pool corresponding to the thread obtained by the filter, so that the business thread pool can use the thread obtained by the filter to execute the task to be executed, thereby achieving the purpose of scheduling the execution of the task to be executed by the global thread pool, thereby achieving the purpose of centralized scheduling of all threads by the global thread pool, which is beneficial to improving the thread reuse rate.

[0132] Based on the relevant contents of S1 to S2 above, it can be known that for the thread determination method provided in the embodiment of the present application, when a global thread pool is deployed on an electronic device, after the electronic device obtains the task to be executed corresponding to the target application, the electronic device submits the task to be executed to the global thread pool, so that the global thread pool schedules the target thread to execute the task to be executed from at least one candidate thread corresponding to the target application (that is, some threads created for the target application). In this way, the purpose of multiplexing the target thread can be achieved, thereby achieving the purpose of converging threads with the help of thread multiplexing, which is beneficial to improving the application operation effect.

[0133] After research, it was found that for the global thread pool mentioned above, if the number of threads in the global thread pool is set too low, the first submitted task will occupy all the thread resources in the thread pool, and the subsequent submitted tasks will be forced to join the queue and wait for execution due to the limit on the number of threads. Generally speaking, a short delay in the execution of a task will not have a big impact, but in some extreme cases, the task will not be executed for a long time, which will affect the functional experience.

[0134] Based on the above findings, in order to better improve the functional experience, the number of threads in the global thread pool can be dynamically adjusted to minimize the delay of the task. Based on this, the present application also provides a possible implementation of the thread determination method above. In this implementation, the thread determination method may include steps 31-32 below in addition to S1-S2 above. It should be noted that the present application does not limit the execution time of step 31.

[0135] Step 31: Periodically check the task waiting queue of the global thread pool above.

[0136] Among them, the task waiting queue is used to record some tasks that have been submitted to the global thread pool above and are waiting for thread scheduling. That is, the task waiting queue is used to record some tasks that have been submitted to the global thread pool above but have not yet been executed. For example, it can be Figure 4 The task queue is shown.

[0137] In addition, the present application does not limit the detection content involved in the "periodic detection" in step 31 above. For example, it can be used to detect whether there are the same tasks between all tasks in the task waiting queue in the current cycle and all tasks in the task waiting queue in the previous cycle (for example, Figure 4 It should be noted that for this Figure 4 For example, the Figure 4 The "1", "2", ..., "8" involved in the task are used to uniquely identify a task (for example, Figure 4 The "1" involved in the task is used to uniquely identify task 1. Figure 4 The "2" involved in is used to uniquely identify task 2, ...).

[0138] In addition, the present application does not limit the period involved in the "periodic detection" in step 31 above, for example, it can be set in advance according to the application scenario. For another example, the time interval between adjacent periods involved in the "periodic detection" can be a relatively small value (especially, the time interval between adjacent periods involved in the "periodic detection" is less than the time interval between the submission times of adjacent tasks), which is conducive to achieving the timeliness of adjusting the number of threads in the global thread pool above, so that the number of threads in the global thread pool can meet the current thread number requirements as much as possible.

[0139] In fact, in some cases, if the global thread pool can perform thread scheduling for each task in a timely manner, so that the task waiting queue is continuously empty, the periodic detection of the task waiting queue during this period is invalid, which leads to a waste of computing resources. Therefore, in order to better save computing resources, the present application also provides a possible implementation method of the timing of the "periodic detection" in step 31 above, which may specifically include the following steps 311-312. Among them, the execution time of step 311 is later than the execution time of S2 above.

[0140] Step 311: After submitting the above task to be executed to the global thread pool, determine whether the task waiting queue is empty.

[0141] In the present application, for a terminal device, after the terminal device submits a task to the global thread pool, the global thread pool determines whether the corresponding task waiting queue is empty. If it is empty, it can be determined that there are no delayed tasks at the current moment, so it is determined that the global thread pool can schedule threads for each task in a timely manner; if it is not empty, it can be determined that there are some tasks in a delayed state at the current moment, so it can be determined that the global thread pool cannot schedule threads for each task in a timely manner, and thus it can be determined that some tasks may not be executed for a long time in the global thread pool, so it is necessary to perform periodic detection and processing on the task waiting queue to determine whether there are tasks that have not been executed for a long time in the task waiting queue.

[0142] Step 312: If it is determined that the task waiting queue is not empty, the task waiting queue is periodically checked.

[0143] In the present application, when it is determined that the task waiting queue of the global thread pool is not empty, the task waiting queue may be periodically checked according to a preset period (for example, Figure 4 The detection content shown) is used to confirm whether there are tasks in the task waiting queue that have not been executed for a long time, so as to confirm whether some tasks in the global thread pool have not been executed for a long time.

[0144] Step 313: If it is determined that the above task waiting queue is empty, then the periodic detection process for the task waiting queue is not started.

[0145] In the present application, when it is determined that the task waiting queue of the global thread pool above is empty, it can be determined that there are no tasks waiting for thread scheduling at the current moment, so that it can be determined that the global thread pool can perform thread scheduling for each task in a timely manner, and further it can be determined that there will basically be no phenomenon that some tasks in the global thread pool are not executed for a long time, so there is no need to perform periodic detection on the task waiting queue.

[0146] Based on the relevant content of step 31 above, it can be known that for a terminal device, after the terminal device submits a task with thread resource requirements to the global thread pool, it can first determine whether the task waiting queue of the global thread pool is empty. If it is not empty, a periodic check of the task waiting queue is performed to confirm whether there are tasks in the task waiting queue that have not been executed for a long time, so as to confirm whether some tasks in the global thread pool have not been executed for a long time, and then confirm whether it is necessary to increase the number of threads in the global thread pool.

[0147] Step 32: If there is an intersection between the task waiting queue of the current cycle and the task waiting queue of the previous cycle, the number of threads in the above global thread pool is increased.

[0148] In the present application, for the periodic detection process of the task waiting queue of the global thread pool above, when the detection process is being performed on the task waiting queue in the tth cycle, if it is determined that there is an intersection between the task waiting queue in the tth cycle and the task waiting queue in the t-1th cycle, it can be determined that there is at least one task in a delayed state in the task waiting queue in the tth cycle, so in order to avoid the adverse effects caused by the long delay of these tasks, the number of threads in the global thread pool can be increased immediately, so that the global thread pool can perform thread scheduling for these tasks in the delayed state as soon as possible, which is conducive to improving the efficiency of task execution. Wherein, t is a positive integer.

[0149] In addition, the present application does not limit the implementation method of "increasing the number of threads in the global thread pool" in step 32 above. For example, it can be specifically: increasing the number of threads in the global thread pool according to the first number. The first number refers to the number required for increasing the number of threads in the global thread pool; and the present application does not limit the determination process of the first number. For example, it can be set in advance according to the application scenario.

[0150] For example, in order to better improve the effect of adding threads, the first number above can be determined according to the number of delayed tasks at the current moment (for example, Figure 5Based on this, it can be seen that the above "determination process of the first number" can be specifically: if there is an intersection between the task waiting queue of the current cycle and the task waiting queue of the previous cycle, then according to the number of tasks in the intersection, the first number is determined, so that the first number is positively correlated with the number of tasks in the intersection, so that the number of threads in the above global thread pool can be increased according to the first number later. It should be noted that the present application does not limit the specific implementation method of the determination process of the first number. For example, it can be specifically: the number of tasks in the intersection is directly determined as the first number.

[0151] The "delayed task" mentioned above refers to a task that is in a delayed state (e.g. Figure 4 The task marked with "4" or the task marked with "5" as shown); and the present application does not limit the implementation method of the delayed task. For example, the delayed task may refer to a waiting time in the "task waiting queue of the global thread pool" that exceeds the interval time of adjacent cycles involved in the "periodic detection" above.

[0152] That is, for the periodic detection process of the task waiting queue of the global thread pool above, when the detection process is being performed on the task waiting queue in the tth cycle, if it is determined that there is an intersection between the task waiting queue in the tth cycle and the task waiting queue in the t-1th cycle, it can be determined that the waiting time of all tasks in the intersection exceeds the interval time of the adjacent cycles involved in the periodic detection, so that it can be determined that there are delayed tasks in the task waiting queue in the tth cycle; however, if it is determined that there is no intersection between the task waiting queue in the tth cycle and the task waiting queue in the t-1th cycle, it can be determined that there are no delayed tasks in the task waiting queue in the tth cycle. Wherein, t is a positive integer.

[0153] It can be seen that in one possible implementation mode, for the current cycle, if there is an intersection between the task waiting queue of the current cycle and the task waiting queue of the previous cycle, it can be determined that there are delayed tasks in the above task waiting queue within the current cycle; if there is no intersection between the task waiting queue of the current cycle and the task waiting queue of the previous cycle, it can be determined that there are no delayed tasks in the above task waiting queue within the current cycle.

[0154] Based on the above four paragraphs, it can be known that for a terminal device, after the terminal device has started periodic detection processing for the task waiting queue of the global thread pool above, when the detection processing is being performed on the task waiting queue in the tth cycle, if it is determined that there is an intersection between the task waiting queue in the tth cycle and the task waiting queue in the t-1th cycle, it can be determined that at least one task in the task waiting queue in the tth cycle has been delayed. Therefore, in order to avoid the adverse effects caused by the long delay of these tasks, the number of threads in the global thread pool can be increased according to the number of tasks in the intersection, so that the increased number of threads can meet the thread resource requirements at the current moment as much as possible, so that the global thread pool can perform thread scheduling for these delayed tasks as soon as possible, which is conducive to improving task execution efficiency. Wherein, t is a positive integer.

[0155] Based on the relevant contents of steps 31 to 32 above, it can be known that for a terminal device, the terminal device can timely adjust the number of threads in the global thread pool according to the changes in the tasks in the waiting state, so that the adjusted number of threads (for example, Figure 5 The number of threads shown) can meet the thread resource requirements at the current moment as much as possible, which can effectively avoid the phenomenon that some tasks in the global thread pool are not executed for a long time, thereby helping to improve the execution effect of these tasks, and further helping to improve the user experience of the terminal device.

[0156] In fact, for a terminal device, the amount of tasks in the terminal device may fluctuate greatly, sometimes more and sometimes less, so in order to better apply to this situation, it is necessary not only to increase the number of threads in the global thread pool above, but also to reduce the number of threads in the global thread pool. Based on this, the present application also provides a possible implementation of the thread determination method above, in which the thread determination method may include step 33 below in addition to all or part of the steps above. Among them, the present application does not limit the execution time of step 33.

[0157] Step 33: When the preset reduction condition is reached, the number of threads in the global thread pool is reduced.

[0158] The preset reduction condition refers to a pre-set condition for triggering a process for reducing the number of threads in the global thread pool described above.

[0159] In addition, the present application does not limit the above preset reduction conditions. For example, it can be specifically: there are no delayed tasks in the above "task waiting queue of the global thread pool" within a continuous preset number of cycles. For the relevant content of delayed tasks, please refer to the above. The preset number can be set in advance according to the application scenario.

[0160] In addition, in order to ensure that the number of threads in the global thread pool is not less than the preset thread number threshold, the present application also provides another possible implementation of the preset reduction condition, which can be: there are no delayed tasks in the "task waiting queue of the global thread pool" in the continuous preset number of cycles, and the number of threads in the global thread pool at the current moment is higher than the thread number threshold. The thread number threshold can be set in advance according to the application scenario.

[0161] In addition, the present application does not limit the implementation method of "reducing the number of threads in the global thread pool" in step 33 above. For example, it can be specifically: reducing the number of threads in the global thread pool according to a pre-set second number. The second number refers to the number required for reducing the number of threads in the global thread pool; and the present application does not limit the second number. For example, the second number can be set in advance according to the application scenario.

[0162] In fact, in order to avoid frequent fluctuations in the number of threads, the strategy of "fast expansion and slow decline" can be used to adjust the number of threads. Figure 5 As shown, when the number of delayed tasks is too large, the number of threads in the global thread pool can expand rapidly until the tasks are no longer delayed; and the number of threads will be gradually reduced only when there is no task delay for a certain period of time. This can not only avoid the problem of long task delays to the greatest extent, but also effectively avoid the problem of frequent thread creation / destruction caused by concurrent number jitter. Based on this, the application also provides a possible implementation of the second number above, for example, the second number is smaller than the first number above (for example, Figure 5 As shown, the second number can be preset to 1).

[0163] Based on the relevant content of step 33 above, it can be known that for a terminal device, the terminal device can not only increase the number of threads in the global thread pool in a timely manner according to the changes in the tasks in the waiting state, but also reduce the number of threads in the global thread pool in a timely manner according to the changes in the tasks in the waiting state. In this way, it is possible to achieve the purpose of saving thread resources as much as possible while ensuring that the number of threads in the global thread pool can meet the thread resource requirements at the current moment.

[0164] Based on the thread determination method provided in the embodiment of the present application, the embodiment of the present application also provides a thread determination device. Figure 6 Explain and illustrate. Figure 6 This is a schematic diagram of the structure of a thread determination device provided in an embodiment of the present application. It should be noted that for the technical details of the thread determination device provided in an embodiment of the present application, please refer to the relevant content of the thread determination method above.

[0165] like Figure 6 As shown, the thread determination device 600 provided in the embodiment of the present application includes:

[0166] A receiving unit 601 is used to receive a task to be executed in a target application;

[0167] The submitting unit 602 is used to schedule a target thread from at least one candidate thread corresponding to the target application to execute the task to be executed; the candidate thread refers to a thread that has been created for the target application.

[0168] In a possible implementation, the receiving unit 601 is specifically used to: receive the thread object corresponding to the task to be executed; the thread object is obtained by instantiating a pre-built thread base class; the thread object is used to describe the task to be executed; the thread base class is determined based on the existing thread classes in a preset language system; the start method of the thread base class is different from the start method of the thread class; the thread object is submitted to the global thread pool by calling the start method of the thread base class.

[0169] In a possible implementation, the thread base class is obtained by rewriting and customizing at least one item of configuration information of the thread class; the at least one item of configuration information includes the startup method.

[0170] In a possible implementation, the project file of the target application contains information on using the thread base class, but does not contain information on using the thread class.

[0171] In a possible implementation manner, the process of determining the project file includes:

[0172] Obtaining the original project file corresponding to the target application; the original project file contains information using the above thread class; the original project file includes several class files;

[0173] Traversing the several types of files;

[0174] When a class described in a class file meets the thread subclass condition, the parent class of the described class is modified to the thread base class;

[0175] When the class described by a class file does not meet the thread subclass condition, create instructions in all methods of the described class;

[0176] When the class name of the thread class exists in a creation instruction, the class name of the thread class existing in the creation instruction is modified to the class name of the thread base class.

[0177] In a possible implementation manner, the submitting unit 602 is specifically configured to:

[0178] Selecting a target thread from the at least one candidate thread;

[0179] Determine a to-be-used thread pool corresponding to the target thread from a plurality of service thread pools corresponding to the target application;

[0180] The task to be executed is allocated to the thread pool to be used; the thread pool to be used is used to execute the task to be executed using the target thread.

[0181] In a possible implementation, the service thread pool satisfies at least one of the following conditions:

[0182] The business thread pool is configured to allow timeout;

[0183] The business thread pool uses a non-blocking interface to obtain tasks.

[0184] In a possible implementation manner, the thread determination device 600 further includes:

[0185] A detection unit, used for periodically detecting the task waiting queue of the global thread pool;

[0186] The increasing unit is used to increase the number of threads in the global thread pool if there is an intersection between the task waiting queue of the current cycle and the task waiting queue of the previous cycle.

[0187] In a possible implementation manner, the thread determination device 600 further includes:

[0188] A judging unit, used for judging whether the task waiting queue is empty after submitting the task to be executed to the global thread pool;

[0189] The detection unit is specifically used to: if it is determined that the task waiting queue is not empty, periodically detect the task waiting queue.

[0190] In a possible implementation manner, the increasing unit is specifically configured to: increase the number of threads in the global thread pool according to the first number;

[0191] The thread determination device 600 further includes:

[0192] The reduction unit is used to reduce the number of threads in the global thread pool according to a second number when a preset reduction condition is met; the second number is smaller than the first number.

[0193] In a possible implementation manner, the first number is determined according to the number of tasks in the intersection.

[0194] Based on the relevant content of the above-mentioned thread determination device 600, it can be known that for the thread determination device 600 provided in the embodiment of the present application, after receiving the task to be executed in the target application, the target thread is scheduled to execute the task to be executed from at least one candidate thread corresponding to the target application (that is, some threads that have been created for the target application). In this way, the purpose of multiplexing the target thread can be achieved, thereby achieving the purpose of thread convergence through thread multiplexing, which is beneficial to improving the application operation effect.

[0195] In addition, an embodiment of the present application also provides an electronic device, which includes a processor and a memory: the memory is used to store instructions or computer programs; the processor is used to execute the instructions or computer programs in the memory, so that the electronic device executes any implementation of the thread determination method provided in the embodiment of the present application.

[0196] See also Figure 7 , which shows a schematic diagram of the structure of an electronic device 700 suitable for implementing the embodiment of the present disclosure. The terminal device in the embodiment of the present disclosure may include but is not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle terminals (such as vehicle navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 7 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.

[0197] like Figure 7 As shown, the electronic device 700 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage device 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the electronic device 700 are also stored. The processing device 701, the ROM 702, and the RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.

[0198] Typically, the following devices may be connected to the I / O interface 705: an input device 706 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 707 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 708 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 709. The communication device 709 may allow the electronic device 700 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 7 The electronic device 700 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.

[0199] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication device 709, or installed from the storage device 708, or installed from the ROM 702. When the computer program is executed by the processing device 701, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.

[0200] The electronic device provided by the embodiment of the present disclosure and the method provided by the above embodiment belong to the same inventive concept, and the technical details not fully described in this embodiment can be referred to the above embodiment, and this embodiment has the same beneficial effects as the above embodiment.

[0201] An embodiment of the present application further provides a computer-readable medium, in which instructions or computer programs are stored. When the instructions or computer programs are executed on a device, the device executes any implementation of the thread determination method provided in the embodiment of the present application.

[0202] It should be noted that the computer-readable medium disclosed above may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, device or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.

[0203] In some embodiments, the client and the server may communicate using any currently known or future developed network protocol such as HTTP (Hyper Text Transfer Protocol), and may be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or future developed network.

[0204] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.

[0205] The computer-readable medium carries one or more programs, and when the one or more programs are executed by the electronic device, the electronic device can execute the method.

[0206] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages ​​or a combination thereof, including, but not limited to, object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).

[0207] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0208] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware, wherein the name of a unit / module does not, in some cases, constitute a limitation on the unit itself.

[0209] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.

[0210] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0211] It should be noted that the various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments, and the same or similar parts between the various embodiments can be referred to each other. For the system or device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description.

[0212] It should be understood that in the present application, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.

[0213] It should also be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article 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, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.

[0214] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.

[0215] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A thread determination method, characterized in that: Applied to the global thread pool, the method includes: Receive tasks to be executed in the target application; A target thread is scheduled to execute the task to be executed from at least one candidate thread corresponding to the target application; the candidate thread refers to a thread that has been created for the target application.

2. The method according to claim 1, characterized in that: The receiving of the tasks to be executed in the target application includes: Receive the thread object corresponding to the task to be executed; the thread object is obtained by instantiating a pre-built thread base class; the thread object is used to describe the task to be executed; the thread base class is determined based on the existing thread classes in the preset language system; the start method of the thread base class is different from the start method of the thread class; the thread object is submitted to the global thread pool by calling the start method of the thread base class.

3. The method according to claim 2, characterized in that The thread base class is obtained by rewriting and customizing at least one item of configuration information of the thread class; the at least one item of configuration information includes the startup method.

4. The method according to claim 2, characterized in that: The project file of the target application contains information for using the thread base class, but does not contain information for using the thread class.

5. The method according to claim 4, characterized in that The process of determining the project documents includes: Obtaining the original project file corresponding to the target application; the original project file contains information using the above thread class; the original project file includes several class files; Traversing the several types of files; When a class described in a class file meets the thread subclass condition, the parent class of the described class is modified to the thread base class; When the class described by a class file does not meet the thread subclass condition, create instructions in all methods of the described class; When the class name of the thread class exists in a creation instruction, the class name of the thread class existing in the creation instruction is modified to the class name of the thread base class.

6. The method according to claim 1, characterized in that The step of scheduling a target thread from at least one candidate thread corresponding to the target application to execute the task to be executed includes: Selecting a target thread from the at least one candidate thread; Determine a to-be-used thread pool corresponding to the target thread from a plurality of service thread pools corresponding to the target application; The task to be executed is allocated to the thread pool to be used; the thread pool to be used is used to execute the task to be executed using the target thread.

7. The method according to claim 6, characterized in that The business thread pool meets at least one of the following conditions: The business thread pool is configured to allow timeout; The business thread pool uses a non-blocking interface to obtain tasks.

8. The method according to claim 1, characterized in that The method further comprises: Periodically checking the task waiting queue of the global thread pool; If there is an intersection between the task waiting queue of the current cycle and the task waiting queue of the previous cycle, the number of threads in the global thread pool is increased.

9. The method according to claim 8, characterized in that After submitting the task to be executed to the global thread pool, the method further includes: Determine whether the task waiting queue is empty; The periodic detection of the task waiting queue of the global thread pool includes: If it is determined that the task waiting queue is not empty, the task waiting queue is periodically checked.

10. The method according to claim 8, characterized in that The step of increasing the number of threads in the global thread pool comprises: According to the first number, increase the number of threads in the global thread pool; The method further comprises: When a preset reduction condition is reached, the number of threads in the global thread pool is reduced according to a second number; the second number is smaller than the first number.

11. The method according to claim 10, characterized in that The first number is determined according to the number of tasks in the intersection.

12. A thread determination device, characterized in that: include: A receiving unit, used for receiving a task to be executed in a target application; A submitting unit, configured to schedule a target thread from at least one candidate thread corresponding to the target application to execute the task to be executed; The candidate thread refers to a thread that has been created for the target application.

13. An electronic device, characterized in that: The device comprises: a processor and a memory; The memory is used to store instructions or computer programs; The processor is used to execute the instructions or computer programs in the memory so that the electronic device executes the method according to any one of claims 1 to 11.

14. A computer readable medium, characterized in that The computer-readable medium stores instructions or computer programs, and when the instructions or computer programs are executed on a device, the device executes the method according to any one of claims 1 to 11.

Citation Information

Cited By

  • Task processing method and related device

    WO2026097787A1