A method for copying thread variables during thread switching and related equipment
By using HolderManager and thread local variables in the thread switching process, the copying process of thread variables is simplified, the problem of cumbersome copying in the existing technology is solved, and complete and efficient copying of thread variables is achieved.
Patent Information
- Application Number
- CN202011459956.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-11
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2040-12-11
AI Technical Summary
During the thread switching process, the existing technology needs to copy thread variables one by one, and if the information held by all thread variables cannot be understood, it is difficult to complete the complete copy, resulting in a cumbersome process.
By determining all thread variable holding classes of the original thread to execute a unified interface class, obtain the implementation class list in HolderManager, extract thread variable information, and set the information to the new thread through thread local variables and bytecode enhancement. , to realize the copying of thread variables.
It simplifies the thread switching process, avoids the cumbersome operation of copying thread variables one by one, improves efficiency, and ensures complete copying of thread variables.
Smart Images

Figure CN112463412B_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of the present specification relate to the technical field of thread replication, and in particular, to a method for replicating thread variables during a thread switching process and related devices. Background Art
[0002] In existing technical solutions, shared variable information, such as full transaction codes and business public information, is often transmitted through thread variables. However, when switching threads, all held thread variables need to be actively extracted and variables in the new thread need to be assigned values. However, it is necessary to clearly know all thread variable holding information. If the information is not fully known, it is difficult to copy all thread variables. Therefore, when switching threads, the thread variables in the original thread need to be copied to the new thread one by one. Summary of the invention
[0003] In view of this, an object of one or more embodiments of the present specification is to provide a method and related devices for copying thread variables during thread switching, so as to solve the problem of cumbersome copying of thread variables during thread switching.
[0004] Based on the above purpose, one or more embodiments of this specification provide a method for copying thread variables during thread switching, including:
[0005] Determine that all thread variable holder classes of the original thread execute a unified interface class to obtain the thread variable binding manager HolderManager;
[0006] Get a list of all implementation classes in HolderManager;
[0007] Extracting thread variable information in the thread variable holding class of the original thread according to the implementation class list;
[0008] Setting the thread variable information into the new thread;
[0009] All thread variables in the original thread are copied to the new thread.
[0010] Furthermore, copying all thread variables in the original thread to the new thread specifically includes:
[0011] Through the thread local variable ThreadLocal information and the local thread variable identifier held in the implementation class list, all thread variables in the original thread are input into the ThreadLocal in the new thread through the set interface.
[0012] Furthermore, the thread local variable ThreadLocal information is obtained by calling a public method of HolderManager based on a list of classes that implement the HolderManager interface.
[0013] Furthermore, the list of all implementation classes in the HolderManager is obtained, including:
[0014] Get the list of implementation classes that currently implement the HolderManager interface through the native application program interface API of the application framework Spring.
[0015] Further, the setting of the thread variable information into the new thread specifically includes:
[0016] The original bound thread variable information in the new thread is cleared, and the corresponding processing method is called at the point where the thread switch occurs in the form of bytecode enhancement, and the extracted thread variable information is placed in the new thread.
[0017] Further, the extracting of the thread variable information in the thread variable holding class of the original thread further includes:
[0018] Before thread switching is performed, the thread variable information is stored in the form of local variables.
[0019] Furthermore, the thread variable information includes a transaction code TCode, a user ID and / or binding information held by a specific component.
[0020] Based on the same inventive concept, one or more embodiments of this specification further provide a device for copying thread variables during thread switching, including:
[0021] Determine interface class module: configured to determine that all thread variable holder classes of the original thread execute a unified interface class to obtain a thread variable binding manager HolderManager;
[0022] Implementation class list acquisition module: configured to obtain all implementation class lists in HolderManager;
[0023] A thread variable information extraction module: configured to extract the thread variable information in the thread variable holding class of the original thread according to the implementation class list;
[0024] Thread variable information setting module: configured to set the thread variable information into the new thread;
[0025] Thread variable copy module: configured to copy all thread variables in the original thread to the new thread.
[0026] Based on the same inventive concept, one or more embodiments of this specification also provide an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements any of the methods described above when executing the program.
[0027] Based on the same inventive concept, one or more embodiments of the present specification further provide a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute any of the above methods.
[0028] From the above description, it can be seen that the method for copying thread variables during thread switching provided by one or more embodiments of the present specification enables the holding class of all thread variables to execute a unified interface class through HolderManager, and then obtains the implementation class list in HolderManager, implements the interface class, and then extracts the thread variable information and sets it to the new thread, and finally copies the thread variables in the original thread to the new thread. This method can avoid copying the thread variables in the original thread to the new thread one by one during thread switching, saving a lot of time and solving the problem of cumbersome copying thread variables during thread switching. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] In order to more clearly illustrate one or more embodiments of the present specification 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 one or more embodiments of the present specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0030] Figure 1 A flowchart of a method for copying thread variables during a thread switching process according to one or more embodiments of this specification;
[0031] Figure 2 A schematic diagram of a device structure for copying thread variables during thread switching according to one or more embodiments of this specification;
[0032] Figure 3 A schematic diagram of the structure of an electronic device according to one or more embodiments of the present specification. DETAILED DESCRIPTION
[0033] In order to make the objectives, technical solutions and advantages of the present disclosure more clearly understood, the present disclosure is further described in detail below in combination with specific embodiments and with reference to the accompanying drawings.
[0034] It should be noted that, unless otherwise specified, the technical terms or scientific terms used in one or more embodiments of this specification should be understood by people with ordinary skills in the field to which this disclosure belongs. The words "include" or "comprise" and the like used in one or more embodiments of this specification mean that the elements or objects appearing before the word include the elements or objects listed after the word and their equivalents, but do not exclude other elements or objects.
[0035] As described in the background art, the existing copying of thread variables during thread switching requires knowing all thread variable holding information. If all thread variable holding information is not known, the thread variables of the original thread cannot be completely copied to the new thread.
[0036] In view of this, one or more embodiments of this specification provide a method for copying thread variables during thread switching, referring to Figure 1 ,include:
[0037] Step S101: Determine that all thread variable holder classes of the original thread execute a unified interface class to obtain a thread variable binding manager (HolderManager).
[0038] In this step, the original thread refers to the thread currently executing the application method. The interface class can provide a standard for the smooth interaction of different classes in the original thread. By making the thread variable holding class execute a unified interface class, a clear instruction is given to the user, giving the task to be implemented. According to the interface reference, the corresponding class implementing the interface is executed. The unified interface class is more convenient for code maintenance and expansion. Accordingly, the interface class can be determined by the following statement to control the actions of all thread variables.
[0039] static Map<String,Object> get(){…} / / Used to get the object in the thread variable;
[0040] static void set(Map<String,Object> contextData){…} / / Used to set the object in the thread variable;
[0041] static void clear(){...} / / Used to clear objects in thread variables;
[0042] default Map<String,Object> getAll(){…} / / Used to get all objects of thread variables;
[0043] default void putAll(Map<String,Object> contextData){…} / / Used to set the value of thread variables;
[0044] default void removeAll(){…} / / Used to clear all thread variables.
[0045] It is understandable that, in actual application, other statements may be selected to determine that all thread variables of the original thread hold a unified interface class, and no specific limitation is made here.
[0046] Step S102: Get a list of all implementation classes in HolderManager.
[0047] In this step, the application programming interface (API) provided by the application framework Spring Framework can be used to obtain a list of all implementation classes in the above HolderManager, and the above implementation classes are used to implement all methods in the above interface classes.
[0048] The Spring framework can effectively organize middle-tier objects, eliminate the need for property files in various custom formats, unify configuration information, and enable programming against interfaces.
[0049] Step S103: extracting thread variable information in the thread variable holding class of the original thread according to the implementation class list.
[0050] In this step, the thread variable information includes the transaction code (TCode), the user's ID and / or the binding information held by a specific component. The thread variable information is extracted through programming code, or the commonly used thread switching class is identified through bytecode. Specifically, the thread variable information can be extracted through the following statement.
[0051] Map<String,Object> result=HolderManager.get();
[0052] Furthermore, it can be determined whether the thread variable information in the copied implementation class list needs to be copied based on the status mark agreed in the interface.
[0053] It is understandable that, in actual application, other statements may be selected to extract the thread variable information of the thread variable holding class of the original thread, which is not specifically limited here.
[0054] Step S104: setting the thread variable information into the new thread.
[0055] In this step, the new thread refers to the method in the original thread that may start a new thread during execution to asynchronously process some content. Before setting, the original bound thread variable information in the new thread is cleared, and the corresponding processing method is called at the point where the thread switch occurs through bytecode enhancement, and the extracted thread variable information is placed in the new thread.
[0056] The following statement can be used to set the above thread variable information to the new thread.
[0057] static void set(Map<String,Object> contextData){…} / /
[0058] default void putAll(Map<String,Object> contextData){…} / /
[0059] It is understandable that in actual application, other statements may be used to set thread variable information to a new thread, which is not specifically limited here.
[0060] Step S105: copy all thread variables in the original thread to the new thread.
[0061] In this step, all thread variables in the original thread are copied to the new thread. Through the thread local variable (ThreadLocal) information and the local thread variable identifier held in the implementation class list, all thread variables in the original thread are input into the ThreadLocal in the new thread through the set interface. Providing the transfer of ThreadLocal values can solve the problem of context transfer during asynchronous execution.
[0062] Furthermore, the above ThreadLocal information is obtained by identifying a list of classes that implement the HolderManager interface and calling a public method in the HolderManager, without knowing in advance the structure and type of the information stored in the ThreadLocal.
[0063] ThreadLocal is a storage class within a thread that can store data within a specified thread. After the data is stored, only the specified thread can get the stored data; that is, ThreadLocal provides the ability to store thread variables within a thread. The difference between thread variables is that the thread variables read by each thread are independent of each other. The value corresponding to the current thread can be obtained through the get interface and set interface.
[0064] In this embodiment, the set interface is used. It can be understood that in the actual application process, all thread variables in the original thread can also be input into the new thread through the get interface. The specific details are not limited here.
[0065] Below, a specific application scenario of the method for copying thread variables in the thread switching process of this embodiment is given. When grayscale deployment is performed, thread variables need to be grayscale marked, but thread variables with grayscale marks will be lost when threads are switched. Therefore, it can be determined that all grayscale marked thread variable holding classes of the original thread execute a unified interface class, thereby controlling the actions of all grayscale marked thread variables; then obtain a list of all implementation classes used to implement the above interface class, and extract the thread variable information of the above grayscale marked thread variables in the form of programming code according to the above implementation class list; clear the existing thread variable information in the new thread to be switched, and call the corresponding interface at the point where the thread switch occurs, and put the grayscale marked thread variable information into the new thread; finally, copy the above grayscale marked thread variables to the new thread through the set interface or get interface.
[0066] As an optional embodiment, after step S103, the following steps are further included:
[0067] Before thread switching is performed, the thread variable information is stored in the form of local variables.
[0068] Specifically, by storing the above thread variables in the form of local variables, the data can be stored for a long time, and the stored data has no expiration time.
[0069] It should be noted that the method of one or more embodiments of this specification can be performed by a single device, such as a computer or server. The method of this embodiment can also be applied in a distributed scenario and completed by multiple devices cooperating with each other. In the case of such a distributed scenario, one of the multiple devices can only perform one or more steps in the method of one or more embodiments of this specification, and the multiple devices will interact with each other to complete the described method.
[0070] It should be noted that the above is a description of a specific embodiment of the present specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0071] Based on the same inventive concept, corresponding to any of the above-mentioned embodiments and methods, one or more embodiments of this specification further provide a device for copying thread variables during thread switching.
[0072] refer to Figure 2 , the device for copying the thread variable during the thread switching process includes:
[0073] Determine interface class module 201: configured to determine that all thread variable holder classes of the original thread execute a unified interface class to obtain a thread variable binding manager HolderManager;
[0074] Implementation class list acquisition module 202: configured to acquire all implementation class lists in HolderManager;
[0075] The thread variable information extraction module 203 is configured to extract the thread variable information in the thread variable holding class of the original thread according to the implementation class list;
[0076] Thread variable information setting module 204: configured to set the thread variable information into the new thread;
[0077] The thread variable copying module 205 is configured to copy all thread variables in the original thread to the new thread.
[0078] For the convenience of description, the above devices are described in terms of functions divided into various modules. Of course, when implementing one or more embodiments of this specification, the functions of each module can be implemented in the same or multiple software and / or hardware.
[0079] The device of the above embodiment is used to implement a method for copying a thread variable during a thread switching process corresponding to any of the above embodiments, and has the beneficial effects of the corresponding method embodiment, which will not be described in detail here.
[0080] Based on the same inventive concept, corresponding to any of the above-mentioned embodiments and methods, one or more embodiments of this specification also provide an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method for copying thread variables during thread switching as described in any of the above embodiments is implemented.
[0081] Figure 3A more specific schematic diagram of the hardware structure of an electronic device provided in this embodiment is shown, and the device may include: a processor 1010, a memory 1020, an input / output interface 1030, a communication interface 1040, and a bus 1050. The processor 1010, the memory 1020, the input / output interface 1030, and the communication interface 1040 are connected to each other through the bus 1050 in the device.
[0082] The processor 1010 can be implemented by a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this specification.
[0083] The memory 1020 may be implemented in the form of ROM (Read Only Memory), RAM (Random Access Memory), static storage device, dynamic storage device, etc. The memory 1020 may store an operating system and other application programs. When the technical solutions provided in the embodiments of this specification are implemented by software or firmware, the relevant program codes are stored in the memory 1020 and are called and executed by the processor 1010.
[0084] The input / output interface 1030 is used to connect the input / output module to realize information input and output. The input / output module can be configured in the device as a component (not shown in the figure), or it can be externally connected to the device to provide corresponding functions. The input device may include a keyboard, a mouse, a touch screen, a microphone, various sensors, etc., and the output device may include a display, a speaker, a vibrator, an indicator light, etc.
[0085] The communication interface 1040 is used to connect a communication module (not shown) to realize communication interaction between the device and other devices. The communication module can realize communication through a wired mode (such as USB, network cable, etc.) or a wireless mode (such as mobile network, WIFI, Bluetooth, etc.).
[0086] The bus 1050 includes a path that transmits information between the various components of the device (eg, the processor 1010, the memory 1020, the input / output interface 1030, and the communication interface 1040).
[0087] It should be noted that, although the above device only shows the processor 1010, the memory 1020, the input / output interface 1030, the communication interface 1040 and the bus 1050, in the specific implementation process, the device may also include other components necessary for normal operation. In addition, it can be understood by those skilled in the art that the above device may also only include the components necessary for implementing the embodiments of the present specification, and does not necessarily include all the components shown in the figure.
[0088] The electronic device of the above embodiment is used to implement the method of copying the corresponding thread variables in the thread switching process in any of the above embodiments, and has the beneficial effects of the corresponding method embodiment, which will not be repeated here.
[0089] Based on the same inventive concept, corresponding to any of the above-mentioned embodiments and methods, one or more embodiments of the present specification also provide a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute the method of copying thread variables during thread switching as described in any of the above embodiments.
[0090] The computer-readable medium of this embodiment includes permanent and non-permanent, removable and non-removable media, and information storage can be implemented by any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, read-only compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device.
[0091] The computer instructions stored in the storage medium of the above embodiment are used to enable the computer to execute the method of copying thread variables during thread switching as described in any of the above embodiments, and have the beneficial effects of the corresponding method embodiments, which will not be repeated here.
[0092] Those skilled in the art should understand that the discussion of any of the above embodiments is merely illustrative and is not intended to imply that the scope of the present disclosure (including the claims) is limited to these examples. In line with the present disclosure, the technical features in the above embodiments or different embodiments may be combined, the steps may be implemented in any order, and there are many other variations of different aspects of one or more embodiments of the present specification as described above, which are not provided in detail for the sake of simplicity.
[0093] In addition, to simplify the description and discussion, and in order not to obscure one or more embodiments of the present specification, known power / ground connections to integrated circuit (IC) chips and other components may or may not be shown in the provided figures. In addition, the device may be shown in the form of a block diagram so as to avoid obscuring one or more embodiments of the present specification, and this also takes into account the fact that the details of the implementation of these block diagram devices are highly dependent on the platform on which one or more embodiments of the present specification will be implemented (i.e., these details should be fully within the scope of understanding of those skilled in the art). Where specific details (e.g., circuits) are set forth to describe exemplary embodiments of the present disclosure, it is apparent to those skilled in the art that one or more embodiments of the present specification may be implemented without these specific details or with variations in these specific details. Therefore, these descriptions should be considered illustrative rather than restrictive.
[0094] Although the present disclosure has been described in conjunction with specific embodiments of the present disclosure, many replacements, modifications and variations of these embodiments will be apparent to those skilled in the art from the foregoing description. For example, other memory architectures (e.g., dynamic RAM (DRAM)) may use the embodiments discussed.
[0095] One or more embodiments of this specification are intended to cover all such substitutions, modifications and variations that fall within the broad scope of the appended claims. Therefore, any omissions, modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of one or more embodiments of this specification should be included in the scope of protection of this disclosure.
Claims
1. A method for copying thread variables during thread switching, characterized in that: include: Determine that all thread variable holder classes of the original thread execute a unified interface class to obtain the thread variable binding manager HolderManager; Get a list of all implementation classes in HolderManager; including: Get the list of implementation classes that currently implement the HolderManager interface through the native application programming interface API of the application framework Spring; Extracting thread variable information in the thread variable holding class of the original thread according to the implementation class list; including: judging whether the thread variable information in the copied implementation class list needs to be copied according to the state mark agreed in the interface, and extracting the thread variable information according to the judgment result; wherein the thread variable information includes at least one of the transaction code TCode, the user's id, and the binding information held by a specific component; Setting the thread variable information into the new thread; Copies all thread variables from the original thread to the new thread, including: Through the thread local variable ThreadLocal information and the local thread variable identifier held in the implementation class list, all thread variables in the original thread are input into the ThreadLocal in the new thread through the set interface.
2. The method according to claim 1, characterized in that The extracting the thread variable information in the thread variable holding class of the original thread specifically includes: The thread variable information is extracted by programming code, or the thread switching class is identified by bytecode to extract the thread variable information.
3. The method according to claim 1, characterized in that The step of setting the thread variable information to the new thread specifically includes: The original bound thread variable information in the new thread is cleared, and the corresponding processing method is called at the point where the thread switch occurs in the form of bytecode enhancement, and the extracted thread variable information is placed in the new thread.
4. The method according to claim 1, characterized in that: The extracting of the thread variable information in the thread variable holding class of the original thread further includes: Before thread switching is performed, the thread variable information is stored in the form of local variables.
5. A device for copying thread variables during thread switching, characterized in that: include: Determine interface class module: configured to determine that all thread variable holder classes of the original thread execute a unified interface class to obtain a thread variable binding manager HolderManager; Implementation class list acquisition module: is configured to obtain all implementation class lists in HolderManager; including: Get the list of implementation classes that currently implement the HolderManager interface through the native application programming interface API of the application framework Spring; The thread variable information extraction module is configured to extract the thread variable information in the thread variable holding class of the original thread according to the implementation class list; including: judging whether the thread variable information in the copied implementation class list needs to be copied according to the state mark agreed in the interface, and extracting the thread variable information according to the judgment result; wherein the thread variable information includes at least one of the transaction code TCode, the user's id, and the binding information held by a specific component; A thread variable information setting module: configured to set the thread variable information into a new thread; The thread variable copy module is configured to copy all thread variables in the original thread to the new thread, including: Through the thread local variable ThreadLocal information and the local thread variable identifier held in the implementation class list, all thread variables in the original thread are input into the ThreadLocal in the new thread through the set interface.
6. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the method according to any one of claims 1 to 4 is implemented.
7. A non-transitory computer-readable storage medium, characterized in that: The non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to cause the computer to execute any one of claims 1 to 4.
Citation Information
Patent Citations
Cross-thread task processing method, device, server and storage medium
CN111625330A
