A data processing method and system
By creating a child thread with the parent thread attribute in a multi-threaded environment and using the reflection mechanism to obtain the thread local variable value, the problem of low efficiency of child thread obtaining the local variable value of the upper thread is solved, and the purpose of simplifying concurrent access control and improving efficiency is achieved.
Patent Information
- Application Number
- CN202110850139.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-27
- Publication Date
- 2025-04-11
- Estimated Expiration
- 2041-07-27
AI Technical Summary
In a multi-threaded environment, child threads are less efficient in obtaining the local variable values of the superior thread, and need to maintain complex thread upper and lower relationship data structures and thread variable Map objects, resulting in complex and inefficient concurrent access control.
By creating a child thread with parent thread attributes, traverse the upper thread based on the parent thread's parent thread attributes, and using the reflection mechanism to obtain thread local variable values, concurrent access control of thread parent-child relationship data structure and thread variable Map objects is avoided.
It improves the efficiency of child threads to obtain local variable values of the superior thread in a multi-threaded environment, simplifies the process of inter-thread variable access, and reduces the complexity of concurrent control.
Smart Images

Figure CN113485839B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of thread processing, and more specifically, to a data processing method and system. Background Art
[0002] In the Java computing programming language, by placing data in a thread-local variable (ThreadLocal) and allowing each thread to create a copy of the variable, ThreadLocal can efficiently provide a separate copy of the thread-local variable value for each thread that uses it.
[0003] In the prior art, if you need to access a variable value of a parent thread, you need to maintain a data structure of thread superior-subordinate relationships, such as a tree, where the tree nodes are child threads; and maintain a thread variable Map to store the variable set of each thread, where the key of the Map is the thread object, and the value of the Map is the variable set. When you need to access the variable value of the parent thread, you need to find the child thread node in the tree that stores the thread superior-subordinate relationship, and then traverse the tree from bottom to top to obtain its parent thread; for each parent thread, check whether the thread in the thread variable Map has the required variable. Since the Map object stores the variable values of multiple threads, when multiple threads access the Map object concurrently, for example, when thread A and thread B concurrently access the Map object to obtain the value of ThreadLocal, the Map object to be accessed is locked, thread B waits, thread A obtains the variable value from the locked Map object, and after thread A completes the operation of obtaining the variable value, the Map object is released, thread B accesses the Map object again, and the data structure storing the superior-subordinate relationship of threads also needs to perform the same concurrency control, which makes the efficiency of obtaining variable values when multiple threads concurrently access the Map object low. Summary of the invention
[0004] In view of this, the present invention discloses a data processing method and system to achieve the purpose of improving the efficiency of a child thread acquiring a local variable value of an upper-level thread in a multi-threaded environment.
[0005] In order to achieve the above purpose, the technical solution disclosed in the present invention is as follows:
[0006] A first aspect of the present invention discloses a data processing method, the method comprising:
[0007] Create a child thread childThread with the properties of the parent thread parentThread;
[0008] Based on the properties of the parent thread parentThread of the child thread childThread, traverse the parent thread of the child thread childThread, and determine whether the parent thread has a thread local variable value localValue;
[0009] If the thread local variable value localValue exists in the upper thread, obtain the thread local variable value localValue based on the thread object findThread of the upper thread obtained in advance;
[0010] The variable access operation is performed based on the thread local variable value localValue.
[0011] Preferably, the step of creating a child thread childThread having the attributes of the parent thread parentThread comprises:
[0012] Create a ParentAwareThread subclass of the current thread Thread class;
[0013] Based on the ParentAwareThread class, create a child thread childThread;
[0014] Assign the pre-acquired current thread object findThread to the property of the parent thread parentThread of the child thread childThread.
[0015] Preferably, the traversing the parent thread of the child thread childThread based on the property of the parent thread parentThread of the child thread childThread, and judging whether the parent thread has a thread local variable value localValue, comprises:
[0016] Based on the property of the parent thread parentThread of the child thread childThread, determine whether the pre-acquired thread object findThread is a null value null;
[0017] If the thread object findThread is null, it is determined that the thread local variable value localValue does not exist in the upper-level thread;
[0018] If the thread object findThread is not a null value, it is determined that the thread local variable value localValue exists in the upper-level thread.
[0019] Preferably, it also includes:
[0020] If the thread local variable value localValue does not exist in the upper thread, a first exception message is generated, and it is determined whether the type of the thread object findThread is the ParentAwareThread class;
[0021] If the type of the thread object findThread is the ParentAwareThread class, set the thread object findThread to the attribute of the parent thread parentThread;
[0022] If the type of the thread object findThread is not the ParentAwareThread class, it is determined that the thread object findThread is a null value null.
[0023] Preferably, the obtaining the thread local variable value localValue based on the thread object findThread of the upper-level thread obtained in advance includes:
[0024] Obtaining the attribute value of the thread local variable threadLocals of the thread object findThread of the parent thread based on a preset reflection mechanism;
[0025] Determine whether the attribute value of the thread local variable threadLocals is null;
[0026] If the attribute value of the thread local variable threadLocals is null, a second exception information is generated;
[0027] If the attribute value of the thread local variable threadLocals is not null, the attribute value of the thread local variable threadLocals is called based on the pre-acquired local parameter to obtain the entry class;
[0028] Determine whether the attribute value of the entry class is null;
[0029] If the attribute value of the entry class is null, the second exception information is generated;
[0030] If the attribute value of the entry class is not null, the thread local variable value localValue is obtained based on the preset reflection mechanism.
[0031] A second aspect of the present invention discloses a data processing system, the system comprising:
[0032] Create a unit for creating a child thread childThread with the properties of the parent thread parentThread;
[0033] A traversal unit, used for traversing the parent thread of the child thread childThread based on the property of the parent thread parentThread of the child thread childThread, and determining whether the parent thread has a thread local variable value localValue;
[0034] an acquiring unit, configured to acquire the thread local variable value localValue based on the thread object findThread of the upper-level thread acquired in advance if the thread local variable value localValue exists in the upper-level thread;
[0035] An operation unit is used to perform variable access operations based on the thread local variable value localValue.
[0036] Preferably, the creation unit includes:
[0037] The first creation module is used to create a ParentAwareThread subclass of the current thread Thread class;
[0038] The second creation module is used to create a child thread childThread based on the ParentAwareThread class;
[0039] The first setting module is used to assign the pre-acquired properties of the current thread object findThread to the properties of the parent thread parentThread of the child thread childThread.
[0040] Preferably, the traversal unit includes:
[0041] A first judgment module is used to judge whether the pre-acquired thread object findThread is a null value null based on the attribute of the parent thread parentThread of the child thread childThread;
[0042] A first determination module is used to determine that the thread local variable value localValue does not exist in the upper-level thread if the thread object findThread is a null value null;
[0043] The second determining module is used to determine that the thread local variable value localValue exists in the upper-level thread if the thread object findThread is not a null value null.
[0044] Preferably, it also includes:
[0045] a judging unit, configured to generate first exception information if the thread local variable value localValue does not exist in the upper-level thread, and to judge whether the type of the thread object findThread is a ParentAwareThread class;
[0046] A setting unit, configured to set the thread object findThread as an attribute of the parent thread parentThread if the type of the thread object findThread is of the ParentAwareThread class;
[0047] A determining unit is used to determine that the thread object findThread is a null value null if the type of the thread object findThread is not the ParentAwareThread class.
[0048] Preferably, the acquisition unit includes:
[0049] A first acquisition module is used to acquire the attribute value of the thread local variable threadLocals of the thread object findThread of the upper thread based on a preset reflection mechanism;
[0050] A first judging module is used to judge whether the attribute value of the thread local variable threadLocals is a null value;
[0051] A first generating module, configured to generate second exception information if the attribute value of the thread local variable threadLocals is null;
[0052] A calling module, configured to, if the attribute value of the thread local variable threadLocals is not null, call the attribute value of the thread local variable threadLocals based on the pre-acquired local parameter to obtain the entry class;
[0053] The second judgment module is used to judge whether the attribute value of the entry class is null;
[0054] A second generating module, configured to generate the second exception information if the attribute value of the entry class is null;
[0055] The second acquisition module is used to acquire the thread local variable value localValue based on the preset reflection mechanism if the attribute value of the entry class is not null.
[0056] Through the above technical solution, it can be known that a child thread with the parent thread parentThread attribute is created, based on the parent thread parentThread attribute of the child thread childThread, the parent thread of the child thread childThread is traversed, and it is determined whether the parent thread ParentAwareThread has a thread local variable value localValue, if the parent thread ParentAwareThread has a thread local variable value localValue, the thread local variable value localValue is obtained based on the thread object findThread of the current thread, and the thread local variable value localValue is obtained based on the thread object of the thread of the parent thread and the variable access operation is performed. Through the above solution, when the child thread obtains the local variable value of the parent thread in a multi-threaded environment, there is no need to perform concurrent access control on the thread parent-child relationship data structure and the thread variable Map object to obtain the local variable value of the parent thread, that is, there is no need to additionally maintain the thread parent-child relationship data structure and the thread variable Map object, only the parent thread of the child thread needs to be traversed according to the thread parent-child relationship to obtain the thread local variable value, thereby achieving the purpose of improving the efficiency of the child thread obtaining the local variable value of the parent thread in a multi-threaded environment. BRIEF DESCRIPTION OF THE DRAWINGS
[0057] In order to more clearly illustrate the embodiments of the present invention 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 embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0058] Figure 1 A flowchart of a data processing method disclosed in an embodiment of the present invention;
[0059] Figure 2 A schematic diagram of a process for creating a child thread with parent thread attributes disclosed in an embodiment of the present invention;
[0060] Figure 3 A flowchart of a process for determining whether a thread local variable value localValue exists in a superior thread according to an embodiment of the present invention;
[0061] Figure 4 A schematic diagram of a process for obtaining a thread local variable value localValue based on a pre-acquired thread object findThread disclosed in an embodiment of the present invention;
[0062] Figure 5 A schematic diagram of the structure of a data processing system disclosed in an embodiment of the present invention. DETAILED DESCRIPTION
[0063] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0064] In this application, the terms "comprises", "comprising" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device comprising 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, an element defined by the sentence "comprising a ..." does not exclude the presence of other identical elements in the process, method, article or device comprising the element.
[0065] As known from the background technology, when multiple threads concurrently access a shared resource object, the efficiency of obtaining thread variable values is low.
[0066] In order to solve this problem, the embodiment of the present invention discloses a data processing method and system, which does not need to maintain the data structure of the parent-child relationship of threads and the Map object of thread variables. It only needs to traverse the parent thread of the child thread according to the parent-child relationship of the thread to obtain the thread local variable value, thereby achieving the purpose of improving the efficiency of multi-threaded acquisition of thread local variable values. The specific implementation method is specifically described through the following embodiment.
[0067] refer to Figure 1 FIG. 1 is a flow chart of a data processing method disclosed in an embodiment of the present invention. The data processing method mainly includes the following steps:
[0068] Step S101: Create a child thread childThread with the attributes of the parent thread parentThread.
[0069] In step S101, a subclass ParentAwareThread of the current thread Thread is created. The ParentAwareThread class carries a type variable representing the parent thread parentThread.
[0070] When creating a child thread childThread, you need to assign the current thread object findThread to the parent thread parentThread property of the child thread childThread.
[0071] By inheriting the current thread Thread, a new subclass is created, which contains the attributes representing the parent thread object. The created child threads are represented by the new subclass.
[0072] The specific process of creating a child thread with the properties of the parent thread parentThread is as follows:
[0073] First, create a ParentAwareThread subclass of the current thread Thread class. Secondly, based on the ParentAwareThread class, create a child thread childThread. Then, assign the pre-acquired current thread object findThread to the properties of the parent thread parentThread of the child thread childThread. Finally, start the execution of the child thread childThread with the properties of the parent thread parentThread.
[0074] Before starting the execution of the child thread childThread with the properties of the parent thread parentThread, its execution content must be defined.
[0075] The current thread object findThread can be obtained through Thread.currentThread().
[0076] Step S102: Based on the properties of the parent thread parentThread of the child thread childThread, traverse the parent thread of the child thread childThread, and determine whether the parent thread has a thread local variable value localValue. If so, execute steps S103-S104; if not, execute step S105.
[0077] In step S102, to obtain the value of the thread local variable in the upper thread in the lower thread, it is necessary to traverse the upper thread through the parent thread parentThread attribute of the thread and check whether the upper thread has the thread local variable value localValue.
[0078] When looking for whether the parent thread has a thread-local variable value, traverse through the parent thread attribute of the new subclass.
[0079] In Java, ThreadLocal represents a thread-local variable. By placing data in a thread-local variable ThreadLocal, each thread can create a copy of the variable, thus avoiding thread safety issues of concurrent access. The thread-local variable value refers specifically to the value in the current thread's copy of the thread-local variable. ThreadLocal provides a T get() method to obtain the thread-local variable value localValue, and ThreadLocal provides a set(T value) method to set the thread-local variable value localValue.
[0080] The process involved in determining whether the thread local variable value localValue exists in the upper-level thread in the above step S102 is as follows:
[0081] Based on the properties of the parent thread parentThread of the child thread childThread, determine whether the pre-acquired thread object findThread is null; if the thread object findThread is null, it is determined that the thread local variable value localValue does not exist in the parent thread; if the thread object findThread is not null, it is determined that the thread local variable value localValue exists in the parent thread.
[0082] In order to facilitate the understanding of the relationship between the upper-level thread and the lower-level thread, an example is given here:
[0083] For example, if thread A creates thread B, thread A is the parent thread of thread B, and thread B is the child thread of thread A. If thread B creates thread C, thread C is the descendant thread of thread A, and thread A is the ancestor thread of thread C. Thread C's parent thread B and ancestor thread A are called the superior threads of thread C, and thread A's superior thread is also the superior thread of thread C. Thread A's child thread B and thread A's descendant thread C are called the subordinate threads of thread A, and thread C's subordinate thread is also the subordinate thread of thread A.
[0084] Step S103: obtaining the thread local variable value localValue based on the thread object findThread of the upper thread obtained in advance.
[0085] The ThreadLocal class of the Java language does not actually save the local variable values of the thread. Instead, there is a threadLocals variable of the ThreadLocalMap class in the Thread class, which is used to save the local values of all ThreadLocal variables of the thread. However, the Thread class does not provide operations on threadLocals, and the ThreadLocalMap class does not expose methods to the outside world. Therefore, it is necessary to use the Java reflection mechanism to operate to obtain the thread local variable value localValue.
[0086] The specific process of obtaining the thread local variable value localValue based on the thread object findThread of the upper thread obtained in advance is shown in A1-A7.
[0087] A1: Get the property value of the thread local variable threadLocals of the thread object findThread of the parent thread based on the preset reflection mechanism.
[0088] The preset reflection mechanism is the Java reflection mechanism, the core of which is to dynamically load classes and obtain detailed information of classes when the program is running, so as to operate the properties and methods of classes or objects. The essence is that after the JVM obtains the class object, it decompiles the class object to obtain various information of the object.
[0089] A2: Determine whether the attribute value of the thread local variable threadLocals is null.
[0090] Among them, the thread local variable threadlocals is a storage class within a thread, which can store data within a specified thread. After the data is stored, only the specified thread can obtain the stored data.
[0091] A3: If the attribute value of the thread local variable threadLocals is null, the second exception information is generated.
[0092] The second exception information is generated to indicate the exception NoLocalException thrown by the lack of a local value of the thread local variable local in the current thread object findThread.
[0093] A4: If the attribute value of the thread local variable threadLocals is not null, the attribute value of the thread local variable threadLocals is called based on the local parameter to obtain the entry class.
[0094] Among them, the entry class is a static inner class of Java, which implements Map.Entry<K,V> This interface can form a one-way linked list through the entry class.
[0095] A5: Determine whether the attribute value of the entry class is null.
[0096] A6: If the attribute value of the entry class is null, the second exception information is generated.
[0097] A7: If the attribute value of the entry class is not null, the thread local variable value localValue is obtained based on the preset reflection mechanism.
[0098] Step S104: performing a variable access operation based on the thread local variable value localValue.
[0099] The method of obtaining the local variable value of the upper thread in this solution is simple to call when using this device for application development and is non-invasive to the application.
[0100] In order to facilitate the understanding of the process of accessing variables based on the thread local variable value localValue, the following application scenarios are used as examples:
[0101] In a specific application scenario embodiment, when there are multiple threads, thread A generates multiple child threads, that is, thread A is the parent thread. If the child thread needs to obtain the file object of thread A, the file object is the thread local variable stored in thread A. When the child thread needs to obtain the file object, it obtains the file object by accessing the thread local variable of thread A. The child thread accesses the thread local variable of its parent thread based on the obtained thread local variable value localValue, thereby obtaining the file object in the thread variable of the parent thread. The file object includes other file-related information such as the file name.
[0102] Step S105: Generate the first exception information, and determine whether the type of the thread object findThread is the ParentAwareThread class. If so, execute step S106; if not, execute step S107.
[0103] In step S105, first exception information is generated to indicate the exception NoLocalException thrown when the local value of the thread local variable local is not found in the current thread object findThread.
[0104] Step S106: Set the thread object findThread as the attribute of the parent thread parentThread.
[0105] Step S107: Determine whether the thread object findThread is a null value.
[0106] In an embodiment of the present invention, when a child thread obtains a local variable value of an upper-level thread in a multi-threaded environment, there is no need to perform concurrent access control on a thread parent-child relationship data structure and a thread variable Map object to obtain the local variable value of the upper-level thread, that is, there is no need to additionally maintain a thread parent-child relationship data structure and a thread variable Map object, and it is only necessary to traverse the upper-level thread of the child thread according to the thread parent-child relationship to obtain the thread local variable value, thereby achieving the purpose of improving the efficiency of the child thread in obtaining the local variable value of the upper-level thread in a multi-threaded environment.
[0107] refer to Figure 2 , which is the process of creating a child thread with parent thread attributes involved in the above step S101, mainly includes the following steps:
[0108] Step S201: Create a ParentAwareThread subclass of the current thread Thread class.
[0109] Step S202: Based on the ParentAwareThread class, create a child thread childThread.
[0110] Step S203: assign the current thread object findThread obtained in advance to the attribute of the parent thread parentThread of the child thread childThread.
[0111] The execution principle of steps S201 to S203 is consistent with the execution principle of the above-mentioned step S101, which can be used for reference and will not be repeated here.
[0112] In an embodiment of the present invention, a ParentAwareThread class of the current thread Thread is created, and based on the ParentAwareThread class, a child thread childThread is created, and the pre-acquired current thread object findThread is assigned to the properties of the parent thread parentThread of the child thread childThread, thereby achieving the purpose of creating a child thread with the properties of the parent thread parentThread.
[0113] refer to Figure 3 , which is the process involved in the above step S102, based on the parent thread parentThread attribute of the child thread childThread, traversing the parent thread of the child thread childThread, and determining whether the parent thread has a thread local variable value localValue, mainly includes the following steps:
[0114] Step S301: Based on the properties of the parent thread parentThread of the child thread childThread, determine whether the pre-acquired thread object findThread is a null value null. If so, execute step S302; if not, execute step S303.
[0115] Step S302: It is determined that the thread local variable value localValue does not exist in the upper-level thread.
[0116] Step S303: determining that the upper-level thread has a thread local variable value localValue.
[0117] The execution principle of steps S301 to S303 is consistent with the execution principle of the above-mentioned step S102, which can be used for reference and will not be repeated here.
[0118] In the embodiment of the present invention, the property of the parent thread parentThread of the child thread childThread is used to determine whether the pre-acquired thread object findThread is a null value null, thereby achieving the purpose of determining whether the upper-level thread has a thread local variable value localValue.
[0119] refer to Figure 4 , which is the process involved in the above step S103 of obtaining the thread local variable value localValue based on the thread object findThread of the upper thread obtained in advance, mainly includes the following steps:
[0120] Step S401: obtaining the attribute value of the thread local variable threadLocals of the thread object findThread of the parent thread based on a preset reflection mechanism.
[0121] Step S402: Determine whether the attribute value of the thread local variable threadLocals is null, if so, execute step S406, if not, execute step S403.
[0122] Step S403: Based on the pre-acquired local parameter, the attribute value of the thread local variable threadLocals is called to obtain the entry class.
[0123] Step S404: Determine whether the attribute value of the entry class is null, if so, execute step S406, if not, execute step S405.
[0124] Step S405: Obtain the thread local variable value localValue based on a preset reflection mechanism.
[0125] Step S406: Generate second abnormal information.
[0126] The execution principle of steps S401 to S406 is consistent with the execution principle of the above-mentioned step S103, which can be referred to and will not be repeated here.
[0127] In the embodiment of the present invention, the thread local variable value localValue is obtained through the reflection mechanism and the thread object findThread of the upper thread, so as to achieve the purpose of obtaining the thread local variable value localValue.
[0128] Based on the above embodiments Figure 1 A data processing method is disclosed, and an embodiment of the present invention also discloses a data processing system, such as Figure 5 As shown, the system mainly includes a creation unit 501 , a traversal unit 502 , an acquisition unit 503 and an operation unit 504 .
[0129] The creation unit 501 is used to create a child thread childThread with the attributes of the parent thread parentThread.
[0130] The traversal unit 502 is used to traverse the parent thread of the child thread childThread based on the properties of the parent thread parentThread of the child thread childThread, and determine whether the parent thread has a thread local variable value localValue.
[0131] The acquiring unit 503 is configured to acquire the thread local variable value localValue based on the thread object findThread of the upper-level thread acquired in advance if the upper-level thread has a thread local variable value localValue.
[0132] The operation unit 504 is used to perform variable access operations based on the thread local variable value localValue.
[0133] Furthermore, the creation unit 501 includes a first creation module, a second creation module and a first setting module.
[0134] The first creation module is used to create a ParentAwareThread subclass of the current thread Thread class.
[0135] The second creation module is used to create a child thread childThread based on the ParentAwareThread class.
[0136] The first setting module is used to assign the current thread object findThread obtained in advance to the property of the parent thread parentThread of the child thread childThread.
[0137] Furthermore, the traversal unit 502 includes a first judgment module, a first determination module and a second determination module.
[0138] The first judgment module is used to judge whether the pre-acquired thread object findThread is a null value null based on the parent thread parentThread attribute of the child thread childThread.
[0139] The first determining module is used to determine that the thread local variable value localValue does not exist in the upper-level thread if the thread object findThread is a null value null.
[0140] The second determination module is used to determine if the thread object findThread is not a null value null, then determine if the thread local variable value localValue exists in the upper-level thread.
[0141] Furthermore, it also includes a judging unit, a setting unit and a determining unit.
[0142] The judging unit generates first exception information if the thread local variable value localValue does not exist in the upper-level thread, and judges whether the type of the thread object findThread is the ParentAwareThread class.
[0143] A setting unit is used to set the thread object findThread to the property of the parent thread parentThread if the type of the thread object findThread is the ParentAwareThread class;
[0144] A determination unit is used to determine that the thread object findThread is a null value null if the type of the thread object findThread is not the ParentAwareThread class.
[0145] Furthermore, the acquisition unit 503 includes a first acquisition module, a first judgment module, a first generation module, a calling module, a second judgment module, a second generation module and a second acquisition module.
[0146] The first acquisition module is used to obtain the attribute value of the thread local variable threadLocals of the thread object findThread based on a preset reflection mechanism.
[0147] The first judgment module is used to judge whether the attribute value of the thread local variable threadLocals is a null value.
[0148] The first generation module generates second exception information if the attribute value of the thread local variable threadLocals is null.
[0149] The calling module is used to call the attribute value of the thread local variable threadLocals to obtain the entry class based on the pre-acquired local parameter if the attribute value of the thread local variable threadLocals is not null.
[0150] The second judgment module is used to judge whether the attribute value of the entry class is null.
[0151] The second generating module is used to generate the second exception information if the attribute value of the entry class is null.
[0152] The second acquisition module is used to obtain the thread local variable value localValue based on a preset reflection mechanism if the attribute value of the entry class is not null.
[0153] In an embodiment of the present invention, when a child thread obtains a local variable value of an upper-level thread in a multi-threaded environment, there is no need to perform concurrent access control on a thread parent-child relationship data structure and a thread variable Map object to obtain the upper-level thread local variable value, that is, there is no need to additionally maintain a thread parent-child relationship data structure and a thread variable Map object, and it is only necessary to traverse the upper-level thread of the child thread according to the thread parent-child relationship to obtain the thread local variable value, thereby achieving the purpose of improving the efficiency of the child thread in obtaining the upper-level thread local variable value in a multi-threaded environment.
[0154] For the above-mentioned method embodiments, for the sake of simplicity, they are all described as a series of action combinations, but those skilled in the art should know that the present invention is not limited by the order of the actions described, because according to the present invention, some steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present invention.
[0155] It should be noted that each embodiment in this specification is described in a progressive manner, and each embodiment focuses on the differences from other embodiments, and the same or similar parts between the embodiments can be referred to each other. For system embodiments, since they are basically similar to method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0156] The steps in the methods of the embodiments of the present invention may be adjusted in order, combined, or deleted according to actual needs.
[0157] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are merely used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations.
[0158] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present invention. 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 invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
[0159] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A data processing method, characterized in that: The method comprises: Create a child thread childThread with the properties of the parent thread parentThread; Based on the properties of the parent thread parentThread of the child thread childThread, traverse the parent thread of the child thread childThread, and determine whether the parent thread has a thread local variable value localValue; If the thread local variable value localValue exists in the upper thread, obtain the thread local variable value localValue based on the thread object findThread of the upper thread obtained in advance; Perform variable access operation based on the thread local variable value localValue; If the thread local variable value localValue does not exist in the upper thread, a first exception message is generated, and it is determined whether the type of the thread object findThread is the ParentAwareThread class; If the type of the thread object findThread is the ParentAwareThread class, set the thread object findThread to the attribute of the parent thread parentThread; If the type of the thread object findThread is not the ParentAwareThread class, it is determined that the thread object findThread is a null value null.
2. The method according to claim 1, characterized in that The step of creating a child thread childThread with the attributes of the parent thread parentThread includes: Create a ParentAwareThread subclass of the current thread Thread class; Based on the ParentAwareThread class, create a child thread childThread; Assign the pre-acquired current thread object findThread to the property of the parent thread parentThread of the child thread childThread.
3. The method according to claim 1, characterized in that The method of traversing the parent thread of the child thread childThread based on the property of the parent thread parentThread of the child thread childThread, and determining whether the parent thread has a thread local variable value localValue, includes: Based on the property of the parent thread parentThread of the child thread childThread, determine whether the pre-acquired thread object findThread is a null value null; If the thread object findThread is null, it is determined that the thread local variable value localValue does not exist in the upper-level thread; If the thread object findThread is not a null value, it is determined that the thread local variable value localValue exists in the upper-level thread.
4. The method according to claim 1, characterized in that The obtaining of the thread local variable value localValue based on the thread object findThread of the upper thread obtained in advance includes: Obtaining the attribute value of the thread local variable threadLocals of the thread object findThread of the parent thread based on a preset reflection mechanism; Determine whether the attribute value of the thread local variable threadLocals is null; If the attribute value of the thread local variable threadLocals is null, a second exception information is generated; If the attribute value of the thread local variable threadLocals is not null, the attribute value of the thread local variable threadLocals is called based on the pre-acquired local parameter to obtain the entry class; Determine whether the attribute value of the entry class is null; If the attribute value of the entry class is null, the second exception information is generated; If the attribute value of the entry class is not null, the thread local variable value localValue is obtained based on the preset reflection mechanism.
5. A data processing system, characterized in that: The system comprises: Create a unit for creating a child thread childThread with the properties of the parent thread parentThread; A traversal unit, used for traversing the parent thread of the child thread childThread based on the property of the parent thread parentThread of the child thread childThread, and determining whether the parent thread has a thread local variable value localValue; an acquiring unit, configured to acquire the thread local variable value localValue based on the thread object findThread of the upper-level thread acquired in advance if the thread local variable value localValue exists in the upper-level thread; An operation unit, used for performing variable access operation based on the thread local variable value localValue; a judging unit, configured to generate first exception information if the thread local variable value localValue does not exist in the upper-level thread, and to judge whether the type of the thread object findThread is a ParentAwareThread class; A setting unit, configured to set the thread object findThread as an attribute of the parent thread parentThread if the type of the thread object findThread is of the ParentAwareThread class; A determining unit is used to determine that the thread object findThread is a null value null if the type of the thread object findThread is not the ParentAwareThread class.
6. The system according to claim 5, characterized in that The creation unit comprises: The first creation module is used to create a ParentAwareThread subclass of the current thread Thread class; The second creation module is used to create a child thread childThread based on the ParentAwareThread class; The first setting module is used to assign the pre-acquired properties of the current thread object findThread to the properties of the parent thread parentThread of the child thread childThread.
7. The system according to claim 5, characterized in that The traversal unit comprises: A first judgment module is used to judge whether the pre-acquired thread object findThread is a null value null based on the attribute of the parent thread parentThread of the child thread childThread; A first determination module is used to determine that the thread local variable value localValue does not exist in the upper-level thread if the thread object findThread is a null value null; The second determining module is used to determine that the thread local variable value localValue exists in the upper-level thread if the thread object findThread is not a null value null.
8. The system according to claim 5, characterized in that The acquisition unit comprises: A first acquisition module is used to acquire the attribute value of the thread local variable threadLocals of the thread object findThread of the upper thread based on a preset reflection mechanism; A first judging module is used to judge whether the attribute value of the thread local variable threadLocals is a null value; A first generating module, configured to generate second exception information if the attribute value of the thread local variable threadLocals is null; A calling module, configured to, if the attribute value of the thread local variable threadLocals is not null, call the attribute value of the thread local variable threadLocals based on the pre-acquired local parameter to obtain the entry class; The second judgment module is used to judge whether the attribute value of the entry class is null; A second generating module, configured to generate the second exception information if the attribute value of the entry class is null; The second acquisition module is used to acquire the thread local variable value localValue based on the preset reflection mechanism if the attribute value of the entry class is not null.
Citation Information
Patent Citations
Self-defined multi-thread management control communication method
CN105930219A
Method for preventing thread hanging in a multi-thread communication program
WO2010022635A1