Disclosure of Invention
The embodiment of the application provides a method, a device and a system for using a global variable and a computer readable storage medium, and aims to solve the technical problems that when the global variable is read in multiple threads and the consistency of thread execution results is kept through a thread lock, unlocking operation is frequently performed and thread execution efficiency is reduced.
To achieve the above object, an aspect of the present application provides a method for using a global variable, including:
acquiring a timestamp of a first global variable, wherein the first global variable is a global variable of a program to be executed;
determining whether the first global variable is changed according to the timestamp;
and when the first global variable is determined not to be changed, acquiring a second global variable backed up by the read thread processing function so as to perform functional operation by using the second global variable.
Optionally, the step of determining whether the first global variable is changed according to the timestamp includes:
comparing the timestamp of the first global variable with the timestamp of the second global variable stored in the read thread;
and when the comparison is consistent, confirming that the first global variable is not changed.
Optionally, after the step of determining whether the first global variable is changed according to the timestamp, the method includes:
and when the first global variable is determined to be changed, assigning the first global variable to the read thread processing function so as to use the first global variable to perform the functional operation.
Optionally, before the step of assigning the first global variable to the read thread processing function to perform the functional operation by using the first global variable, the method includes:
acquiring the state of a thread lock;
when the state of the thread lock is an unlocking state, acquiring the first global variable in the thread lock;
and assigning the first global variable to the read thread processing function.
Optionally, after the step of obtaining the state of the thread lock, the method includes:
and if the state of the thread lock is an occupied state, applying for writing the lock so as to read the first global variable in the thread lock.
Optionally, after the step of assigning the first global variable to the read thread processing function, the method includes:
and modifying the timestamp of the first global variable into the timestamp of the first global variable backed up by the read thread processing function, and releasing the thread lock.
Optionally, after the step of obtaining a second global variable backed up by the read thread processing function to perform a functional operation using the second global variable, the method further includes:
and displaying the result of the functional operation to a display interface.
In addition, to achieve the above object, another aspect of the present application further provides an apparatus for using a global variable, including:
the system comprises a first acquisition module, a second acquisition module and a control module, wherein the first acquisition module is used for acquiring a timestamp of a first global variable, and the first global variable is a global variable of a program to be executed;
a determining module, configured to determine whether the first global variable is changed according to the timestamp;
and the second acquisition module is used for acquiring a second global variable backed up by the read thread processing function when the first global variable is determined not to be changed so as to perform functional operation by using the second global variable.
In addition, in order to achieve the above object, in another aspect, the present application further provides a system for using a global variable, where the system for using a global variable includes a processor, a memory, and a program for using a global variable stored in the memory and executable on the processor, and the program for using a global variable, when executed by the processor, implements the steps of the method for using a global variable as described above.
In addition, to achieve the above object, another aspect of the present application further provides a computer-readable storage medium, on which a global variable using program is stored, and when the global variable using program is executed by a processor, the method for using the global variable as described above is implemented.
In this embodiment, when a thread acquires a first global variable of a program to be executed, a timestamp of the first global variable is acquired first, and when it is determined that the timestamp of the first global variable does not change, it is determined that the first global variable does not change, a second global variable stored in a thread-reading processing function is directly acquired, so as to perform functional operation using the second global variable. The unlocking operation does not need to be frequently executed under the condition that the global variable is not changed, and the running efficiency of the thread is improved.
Detailed Description
It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the present application.
The main solution of the embodiment of the application is as follows: acquiring a timestamp of a first global variable, wherein the first global variable is a global variable of a program to be executed; determining whether the first global variable is changed according to the timestamp; and when the first global variable is determined not to be changed, acquiring a second global variable backed up by the read thread processing function so as to perform functional operation by using the second global variable.
In the prior art, in the process of using global variables by multiple threads, it often happens that the global variables are changed due to the end of execution of one thread, but another thread continues to use the unchanged global variables, so that the final result is wrong. In order to solve the problem, a thread lock is usually added to the global variable to ensure that only one thread can perform read-write operation on the global variable at the same time, and only after the current thread finishes using the global variable, the thread lock is released and the next thread can continue to use the global variable. Namely, the synchronization of different threads for acquiring the global variable is ensured in a thread lock mode. But the way of using thread locks may have the problem that each time a thread needs to acquire a global variable by unlocking. If there are multiple threads in a program, each thread needs to perform wait and unlock operations one by one, which will undoubtedly slow down the execution efficiency of the threads. In the method, the timestamp is added to the global variable, when the timestamp is not changed, the representative global variable is not changed, the thread directly reads the backup global variable in the thread reading processing function, operations such as unlocking and lock releasing are not needed to be executed, the step of acquiring the global variable by the thread is simplified, and the overall execution efficiency of the thread is improved.
As shown in fig. 1, fig. 1 is a schematic terminal structure diagram of a hardware operating environment according to an embodiment of the present application.
As shown in fig. 1, the terminal may include: a processor 1001, such as a CPU, a network interface 1004, a user interface 1003, a memory 1005, a communication bus 1002. Wherein a communication bus 1002 is used to enable connective communication between these components. The user interface 1003 may include a Display screen (Display), an input unit such as a Keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface, a wireless interface. The network interface 1004 may optionally include a standard wired interface, a wireless interface (e.g., WI-FI interface). The memory 1005 may be a high-speed RAM memory or a non-volatile memory (e.g., a magnetic disk memory). The memory 1005 may alternatively be a storage device separate from the processor 1001.
Optionally, the terminal may further include a camera, a Radio Frequency (RF) circuit, a sensor, a remote controller, an audio circuit, a WiFi module, a detector, and the like. Of course, the terminal may also be configured with other sensors such as a gyroscope, a barometer, a hygrometer and a temperature sensor, which are not described herein again.
Those skilled in the art will appreciate that the terminal structure shown in fig. 1 does not constitute a limitation of the terminal device and may include more or fewer components than those shown, or some components may be combined, or a different arrangement of components.
As shown in fig. 1, a memory 1005, which is a kind of computer-readable storage medium, may include an operating system, a network communication module, a user interface module, and a control program of the home appliance.
In the terminal shown in fig. 1, the network interface 1004 is mainly used for connecting to a backend server and performing data communication with the backend server; the user interface 1003 is mainly used for connecting a client (user side) and performing data communication with the client; and the processor 1001 may be configured to call the control program of the home appliance stored in the memory 1005 and perform the following operations:
acquiring a timestamp of a first global variable, wherein the first global variable is a global variable of a program to be executed;
determining whether the first global variable is changed according to the timestamp;
and when the first global variable is determined not to be changed, acquiring a second global variable backed up by the read thread processing function so as to perform functional operation by using the second global variable.
Referring to fig. 2, fig. 2 is a schematic flowchart of an embodiment of a method for using global variables according to the present application.
While the embodiments of the present application provide an embodiment of a method for using global variables, it should be noted that, although a logical order is shown in the flowcharts, in some cases, the steps shown or described may be performed in an order different from that here.
The use method of the global variable comprises the following steps:
step S10, acquiring a timestamp of a first global variable, wherein the first global variable is a global variable of a program to be executed;
the timestamp is the time when the global variable changes, the read thread records the time when the global variable changes, and the timestamp can be marked by using a system tick or a universal coordinated time (UTC) method, so that the repetition is avoided, and the thread is prevented from using an incorrect global variable to perform functional operation. In this embodiment, a system tick is taken as an example for explanation, a system tick execution statement is added in the programming process, and when a global variable changes, a timestamp of the change of the global variable is acquired through the system tick execution statement. Wherein the timestamp may be converted to a date format, which may be 2020-04-0404: 04:04, or 2020/04/04/04/04/04. In this embodiment, the acquired time stamp is converted into a date format by adding the time stamp to the thread, so that the change of the time stamp can be more intuitively understood.
When the thread reads a first global variable of a program to be executed, the thread firstly acquires a time stamp of the first global variable. The method for acquiring the timestamp of the first global variable may be acquired by adding a corresponding method to the thread. In this embodiment, there are four methods for obtaining the timestamp of the first global variable, which are respectively: by Date, by currentTimeMillis method in the System class, by Calendar class, and by Date class.
In the application, the global variable is an array-class variable, and the array-class global variable cannot be released by the thread, so that the situation of calculation errors caused by using historical data when the thread processing function is read to back up the global variable is avoided. When the callback processing function uses the global variable, whether the obtained global variable is historical data or not can be identified through handles or other information, if yes, the currently used global variable is discarded, the operation of obtaining the global variable for assignment is executed, and errors in logic functions caused by using the historical data are avoided.
Step S20, determining whether the first global variable is changed according to the timestamp;
in the present application, the thread determines whether the first global variable has been modified by the timestamp of the first global variable. For example, when the thread reads that the date format converted from the timestamp of the first global variable is 2020-04-0404: 06:07, the thread B is obtained to modify the first global variable in the program to be executed every 2S, the current system time is obtained, the current system time is compared with the time of the obtained first global variable, if the current system time is 2020-04-0404: 06:10, the current system time is calculated to perform difference operation with the time corresponding to the date format of the obtained timestamp, the difference value is 3S and is greater than the period 2S in which the thread B has modified the global variable, and the change of the global variable in the program to be executed is determined. It is understood that there are other methods for determining the change of the global variable by the timestamp, and the method is not limited to the scheme listed in the embodiment.
And step S30, when it is determined that the first global variable has not changed, acquiring a second global variable backed up by the read thread processing function, so as to perform functional operation using the second global variable.
The second global variable is stored in the read thread, and when the timestamp of the second global variable is equal to that of the first global variable, the second global variable is equal to the first global variable; when its timestamp is not equal to that of the first global variable, then the second global variable is not equal to the first global variable.
The functional operation is an operation performed by substituting the first global variable into the execution method after the thread acquires the first global variable. For example, addition, subtraction, multiplication, and division operations.
After the step of obtaining the second global variable backed up by the read thread processing function to perform the functional operation by using the second global variable, the method further includes:
and displaying the result of the functional operation to a display interface.
After the functional operation is executed, in the application, the result of the functional operation is displayed to a display interface, so that a user can know the result of the functional operation performed after the thread assigns the global variable.
And when the thread determines that the global variable changes, executing a step of reading a first global variable in the running program, assigning the read first global variable to a read thread processing function, realizing functional operation by using the first global variable, and updating a second global variable stored in the read thread processing function.
It is understood that multiple threads performing different operations may be started during the running of the program, and the threads may be executed simultaneously or separately (thread a is executed before thread B is executed). When multiple threads are running simultaneously, there may be a delay. For example, when the thread a writes a first global variable of a program to be executed, the thread B reads the first global variable, the period required for the thread a to write the global variable is long, there is a change in the value of the first global variable caused in the process of modifying the first global variable by the thread a, at this time, the system tick in the thread records the modification time of the first global variable, because the modification of the global variable by the thread a takes a long period, if the thread B reads the first global variable in the process of modifying the global variable by the thread a, and performs the functional operation using the first global variable, since the thread a has modified the first global variable, but the thread B still uses the first global variable before the thread a does not perform the modification operation to perform the operation, the operation result obtained when the second global variable backed up in the read thread is assigned to the thread is an error. The problem is usually solved by adopting a thread lock mode, namely in the process that the thread A acquires the first global variable for assignment, the first global variable is locked and can only be assigned by the thread A, other threads enter a waiting state until the thread A finishes assignment of the first global variable, and the thread lock is released. The synchronization of the calculation results can be ensured by locking the global variables. However, when there are a plurality of threads in the program, the waiting and unlocking operations need to be executed continuously, and the running speed of the CPU is reduced.
In this embodiment, when a thread acquires a first global variable in a program to be executed, a timestamp of the first global variable is acquired first, and when it is determined that the timestamp of the first global variable does not change, it is determined that the first global variable does not change, a second global variable stored in a thread-reading processing function is directly acquired, so as to perform functional operation using the second global variable. The unlocking operation is not required to be frequently executed under the condition that the first global variable in the program to be executed is not changed, and the processing efficiency of the thread is improved.
Further, referring to fig. 3, a method for using global variables of the present application is provided.
The present embodiment is different from the previous embodiment in that the step of determining whether the global variable is changed according to the timestamp includes:
step S21, comparing the timestamp of the first global variable with the timestamp of the second global variable stored in the read thread;
and step S22, when the comparison is consistent, confirming that the first global variable is not changed.
In this embodiment, the thread may compare the obtained timestamp of the first global variable with the timestamp of the second global variable backed up in the read thread processing function, and when the comparison result is consistent, it is determined that the first global variable is not changed; and when the comparison result is inconsistent, confirming that the first global variable is changed.
Specifically, the following examples are given in this embodiment: when the thread reads the first global variable, the timestamp of the acquired global first variable is 1603846961000, and when the timestamp stored in the read thread is 1603846961000, the timestamp is determined not to be changed, so that the first global variable is confirmed not to be changed; if it is obtained that the timestamp stored in the read thread is 1603846961001, it is determined that the timestamp has changed, and it is confirmed that the first global variable has changed. Further, in this embodiment, whether the global variable has changed or not can also be intuitively determined by converting the timestamp into a date format, and when the timestamp of the global variable is obtained as 1603846961000, the method is substituted to sequentially obtain the year, month, day, minute, second and millisecond for converting the timestamp into the date format, and finally the converted beijing time is 2020-10-2809: 02:41, so that the change of the timestamp can be more intuitively understood by a time comparison method.
As follows:
in this embodiment, the thread may determine whether the timestamp is changed by comparing the read timestamp with the timestamp stored in the read thread, and when the timestamps are not consistent, it is determined that the timestamp of the first global variable has changed; when the timestamps are consistent, it represents that the first global variable has not changed. Whether the time stamp is changed or not is intuitively judged by using a time stamp for comparison.
Based on the previous embodiment, the present application provides another embodiment, which is different from the previous embodiment in that: after the step of determining whether the first global variable has changed according to the timestamp, the method includes:
and step S40, when it is determined that the first global variable has changed, assigning the first global variable to the read thread processing function, so as to perform the functional operation using the first global variable.
In this embodiment, when a thread acquires a first global variable, a timestamp of the first global variable is acquired first, the timestamp of the first global variable is compared with a timestamp stored in a read thread processing function, and when the acquired timestamp of the first global variable is different from a timestamp of a second global variable stored in the read thread processing function, it is determined that the first global variable has changed, and a step of reading the first global variable of a function to be processed needs to be performed to acquire the first global variable, and the first global variable is assigned to the read thread processing function to perform function operation using the first global variable.
In this embodiment, when a thread acquires a first global variable, first, by acquiring a timestamp of the first global variable, whether the first global variable changes is determined by the timestamp, the first global variable of a program to be executed is read only when the first global variable changes, and a second global variable stored in the thread is directly acquired when the timestamp of the first global variable does not change, so that the acquisition of the first global variable of the program to be executed is not required to be uniformly executed, the acquisition step of the first global variable is simplified, and the operating efficiency of the thread is improved.
Before the step of assigning the global variable to the read thread processing function to perform the functional operation by using the global variable, the method includes:
step S01, acquiring the state of the thread lock;
step S02, when the state of the thread lock is an unlocked state, acquiring the first global variable in the thread lock;
in step S03, the first global variable is assigned to the read thread processing function.
When the first global variable is changed, the thread acquires the state of a thread lock of the first global variable of the program to be executed, wherein the state of the thread lock comprises an occupied state and an unlocked state, when the thread lock is in the occupied state, the thread lock represents that other threads currently use the first global variable, and it can be understood that when the thread lock is in the occupied state, the first global variable is already added into the thread lock, and when the thread uses the first global variable, other threads cannot acquire the first global variable through the thread lock at the same time, so that the correctness of an operation result after the thread acquires the first global variable for assignment is ensured; when the thread lock is in the unlocked state, the thread represents that no other thread uses the first global variable currently, and the thread can acquire the first global variable through the unlocking operation. After the thread executes the unlocking operation to obtain the first global variable, the first global variable is assigned to the read thread processing function so that the read thread processing function can obtain the updated first global variable, record the updated first global variable as the second global variable, store the timestamp of the updated first global variable, and use the updated first global variable to perform functional operation.
For example, when the thread a reads the global variable, a timestamp of a first global variable to be read is obtained first, the obtained timestamp is compared with a timestamp of a second global variable read by the thread backup, and when the comparison is inconsistent, it is determined that the thread a needs to perform an unlocking operation to obtain the first global variable. That is, in this embodiment, a timestamp is added before the global variable is determined, so that when the thread acquires the global variable, whether the first global variable with the execution program changes is determined according to a change in the timestamp, and when the thread reads that the first global variable has changed, the thread needs to assign a value to the first global variable in a manner of reading the first global variable, thereby ensuring accuracy of acquiring the first global variable.
After the step of obtaining the state of the thread lock, the method comprises the following steps:
step S04, if the state of the thread lock is an occupied state, applying for a write lock to read a global variable in the thread lock.
When the thread reads that the state of the thread lock is an occupied state, the thread represents that other threads (thread B) are using the first global variable of the program to be executed currently, the state of the thread lock needs to be acquired, when the state of the thread lock is the occupied state, the thread firstly executes the step of applying for unlocking, so that the assignment of the first global variable is completed by the thread B, and the unlocking can be timely carried out to acquire the first global variable when the lock is released.
After the step of assigning the global variable to the read thread processing function, the method includes:
and step S05, modifying the time stamp of the global variable into the time stamp of the read thread processing function backup, and releasing the thread lock.
And after the thread completes the assignment of the global variable, the timestamp of the global variable needs to be modified, the timestamp of the global variable is modified into the timestamp stored in the thread reading processing function, and the thread lock is released for the next thread to obtain.
In this embodiment, after the assignment of the global variable is completed, the timestamp of the global variable is changed to change the timestamp of the global variable into a timestamp in the read thread processing function, so that the timestamp of the global variable is ensured to be consistent with the timestamp in the read thread processing function.
In addition, referring to fig. 3, fig. 3 is a schematic flow chart of the present application.
In the embodiment, the thread 1 performs modification operation on the global variable of the program to be executed, and time stamps on the modification of the global variable. When the thread 2 reads a first global variable of a program to be executed, the acquired timestamp is changed, the step of acquiring the lock is executed, the first global variable is acquired, the global variable is assigned to a backup global variable in the read thread processing function to serve as a second global variable, and when the timestamp of the first global variable is not changed, the second global variable backed up in the read thread is directly acquired, and functional operation is executed. The step of reading the global variable is not required to be uniformly executed by the thread, so that the execution efficiency of the thread is improved.
In addition, referring to fig. 4, fig. 4 is a functional module schematic diagram of the present application. In addition, to achieve the above object, another aspect of the present application further provides an apparatus for using a global variable, including:
a first obtaining module 10, configured to obtain a timestamp of a first global variable, where the first global variable is a global variable of a program to be executed;
a determining module 20, configured to determine whether the first global variable is changed according to the timestamp;
a second obtaining module 30, configured to obtain a second global variable backed up by the read thread processing function when it is determined that the first global variable is not changed, so as to perform functional operation using the second global variable.
In addition, in order to achieve the above object, in another aspect, the present application further provides a system for using a global variable, where the system for using a global variable includes a processor, a memory, and a program for using a global variable stored in the memory and executable on the processor, and the program for using a global variable, when executed by the processor, implements the steps of the method for using a global variable as described above.
In addition, to achieve the above object, another aspect of the present application further provides a computer-readable storage medium, on which a global variable using program is stored, and when the global variable using program is executed by a processor, the method for using the global variable as described above is implemented.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the application. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
It should be noted that in the claims, any reference signs placed between parentheses shall not be construed as limiting the claim. The word "comprising" does not exclude the presence of elements or steps not listed in a claim. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The application can be implemented by means of hardware comprising several distinct elements, and by means of a suitably programmed computer. In the unit claims enumerating several means, several of these means may be embodied by one and the same item of hardware. The usage of the words first, second and third, etcetera do not indicate any ordering. These words may be interpreted as names.
While alternative embodiments of the present application have been described, additional variations and modifications of these embodiments may occur to those skilled in the art once they learn of the basic inventive concepts. It is therefore intended that the following appended claims be interpreted as including alternative embodiments and all such alterations and modifications as fall within the scope of the application.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present application without departing from the spirit and scope of the application. Thus, if such modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is intended to include such modifications and variations as well.