Method, apparatus, server and storage medium for acquiring system time

By using the singleton pattern and thread pool instances to obtain system time in high-concurrency scenarios, the performance overhead caused by frequent thread interactions is solved, and more efficient system time acquisition is achieved.

CN116820779BActive Publication Date: 2026-07-10CHERY AUTOMOBILE CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
CHERY AUTOMOBILE CO LTD
Filing Date
2023-07-13
Publication Date
2026-07-10

AI Technical Summary

Technical Problem

In high-concurrency scenarios, multiple threads frequently need to interact with the operating system to obtain the system time, resulting in significant performance overhead.

Method used

A singleton instance of the time class is created using the singleton pattern. The system time is periodically obtained using a thread pool instance and a daemon thread, and then assigned to a global time variable for use by multiple threads in high-concurrency scenarios.

Benefits of technology

This reduces the number of frequent thread interactions, saves time, and improves the performance of system time acquisition.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116820779B_ABST
    Figure CN116820779B_ABST
Patent Text Reader

Abstract

The application relates to the field of information technology, in particular to a method and device for acquiring system time, a server and a storage medium, wherein the method comprises the following steps: creating a time class for acquiring system time; creating a target instance for uniquely acquiring system time in the time class, acquiring system time by using the target instance, and acquiring system time acquisition requests of each thread in a concurrent scene; and assigning the system time to each thread which sends the system time acquisition request. Thus, the problems that multiple threads need to frequently interact with an operating system to acquire system time in a high-concurrent scene and a large amount of time is consumed are solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of information technology, and in particular to a method, apparatus, server, and storage medium for obtaining system time. Background Technology

[0002] In high-concurrency scenarios, each process needs to acquire system time independently, requiring frequent switching between the CPU (Central Processing Unit) and memory, which consumes a significant amount of time and resources. If there are many concurrent threads, and each thread executes a short task before terminating, the frequent creation of threads will drastically reduce system efficiency and consume a large amount of time. Summary of the Invention

[0003] This application provides a method, apparatus, server, and storage medium for obtaining system time, in order to solve the problem that in high-concurrency scenarios, multiple threads need to frequently interact with the operating system to obtain system time, which consumes a lot of time.

[0004] The first aspect of this application provides a method for obtaining system time, comprising the following steps: creating a time class for obtaining system time; creating a unique target instance for obtaining system time in the time class; obtaining the system time using the target instance; and obtaining system time acquisition requests from each thread in a concurrent scenario; and assigning the system time to each thread that sent the system time acquisition request.

[0005] Optionally, in one embodiment of this application, creating a unique target instance for obtaining system time in the time class includes: privatizing the constructor of the time class; and creating a unique thread pool instance for obtaining system time based on the constructor.

[0006] Optionally, in one embodiment of this application, the step of creating a unique thread pool instance for obtaining system time according to the constructor includes: creating a scheduled task within the constructor; and creating a thread pool instance of the scheduled task within the scheduled task.

[0007] Optionally, in one embodiment of this application, a daemon thread for obtaining system time is created within the thread pool instance.

[0008] Optionally, in one embodiment of this application, while creating a unique thread pool instance for obtaining system time according to the constructor, the method further includes: creating a global time variable within the constructor.

[0009] Optionally, in one embodiment of this application, the default value of the global time variable is the current system time.

[0010] Optionally, in one embodiment of this application, obtaining the system time using the target instance includes: periodically starting the thread pool instance; using the system time obtained by the daemon thread during each startup of the thread pool instance; and assigning the system time to the global time variable.

[0011] A second aspect of this application provides an apparatus for obtaining system time, comprising: a first creation module for creating a time class for obtaining system time; a second creation module for creating a unique target instance for obtaining system time in the time class, obtaining the system time using the target instance, and obtaining system time acquisition requests from each thread in a concurrent scenario; and an assignment module for assigning the system time to each thread that sends the system time acquisition request.

[0012] Optionally, in one embodiment of this application, the second creation module is further used to privatize the constructor of the time class; and to create a unique thread pool instance for obtaining system time based on the constructor.

[0013] Optionally, in one embodiment of this application, the second creation module is further configured to create a timed task within the constructor; and create a thread pool instance of the timed task within the timed task.

[0014] Optionally, in one embodiment of this application, a daemon thread for obtaining system time is created within the thread pool instance.

[0015] Optionally, in one embodiment of this application, it further includes: a third creation module, used to create a global time variable within the constructor while creating a unique thread pool instance that obtains system time according to the constructor.

[0016] Optionally, in one embodiment of this application, the default value of the global time variable is the current system time.

[0017] Optionally, in one embodiment of this application, the second creation module is further configured to periodically start the thread pool instance; during each startup of the thread pool instance, the system time obtained by the daemon thread is used; and the system time is assigned to the global time variable.

[0018] A third aspect of this application provides a server, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to perform the method for obtaining system time as described in the above embodiments.

[0019] A fourth aspect of this application provides a computer-readable storage medium having a computer program stored thereon, which is executed by a processor to perform the method for obtaining system time as described in the above embodiments.

[0020] Therefore, this application has at least the following beneficial effects:

[0021] This application embodiment can create a unique target instance for acquiring system time within a time class for acquiring system time. A single target instance is used to acquire system time and is provided to multiple threads in high-concurrency scenarios. Therefore, each thread does not need to acquire system time independently, saving significant time, improving acquisition performance, and solving the problem that in high-concurrency scenarios, multiple threads need to frequently interact with the operating system to acquire system time, consuming a large amount of time. Additional aspects and advantages of this application will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the application. Attached Figure Description

[0022] The above and / or additional aspects and advantages of this application will become apparent and readily understood from the following description of the embodiments taken in conjunction with the accompanying drawings, wherein:

[0023] Figure 1 This is a flowchart of a method for obtaining system time according to an embodiment of this application;

[0024] Figure 2 A detailed flowchart for obtaining system time in a high-concurrency scenario according to an embodiment of this application;

[0025] Figure 3 This is an example diagram of an apparatus for obtaining system time according to an embodiment of this application;

[0026] Figure 4 This is a schematic diagram of the structure of a server provided according to an embodiment of this application. Detailed Implementation

[0027] The embodiments of this application are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and intended to explain this application, and should not be construed as limiting this application.

[0028] The following description, with reference to the accompanying drawings, outlines a method, apparatus, server, and storage medium for obtaining system time according to embodiments of this application. Addressing the problems mentioned in the background section, this application provides a method for obtaining system time. In this method, a unique target instance for obtaining system time is created within a time class. A single target instance is used to obtain system time and is provided to multiple threads in high-concurrency scenarios. This eliminates the need for each thread to obtain system time independently, saving significant time, improving performance, and solving the problem of frequent interaction with the operating system and excessive time consumption by multiple threads in high-concurrency scenarios.

[0029] Specifically, Figure 1 This is a flowchart illustrating a method for obtaining system time provided in an embodiment of this application.

[0030] like Figure 1 As shown, the method for obtaining system time includes the following steps:

[0031] In step S101, a time class for obtaining system time is created.

[0032] For certain classes in a system, having only one instance is crucial. The Singleton pattern is one of the simplest forms of design patterns, and its main purpose is to ensure that an object of a class is the only instance in the system. This embodiment of the application can create a Time class under the Singleton pattern. The Singleton pattern ensures thread safety in high-concurrency scenarios and guarantees that the Time class in this embodiment has only one instance. Furthermore, it instantiates itself and provides this instance to the entire system, thereby avoiding problems such as excessive memory consumption caused by duplicate object creation.

[0033] In step S102, a unique target instance for obtaining system time is created in the time class, the system time is obtained using the target instance, and the system time acquisition request of each thread in the concurrent scenario is obtained.

[0034] The embodiments of this application can use a single target instance to obtain the system time, eliminating the need for each thread to obtain the system time independently. This avoids the problem of frequent switching between CPU and memory, saves a lot of time, and improves the acquisition performance.

[0035] In one embodiment of this application, creating a unique target instance for obtaining system time in a time class includes: privatizing the constructor of the time class; and creating a thread pool instance for obtaining system time based on the constructor.

[0036] It is understood that, in this embodiment of the application, the constructor of the time class can be made private, and a timed task method can be created within the constructor. In the constructed timed task, a timed task thread pool instance is created, and a daemon thread for obtaining the system time is created within the thread pool instance. Subsequently, the thread pool instance is started to obtain the latest system time every preset time, where the preset time can be every millisecond.

[0037] Furthermore, while creating a unique thread pool instance to obtain the system time according to the constructor, this embodiment of the application can also initialize a global time variable in the constructor of the time class, and the default value of the variable is the current system time.

[0038] In one embodiment of this application, obtaining system time using a target instance includes: periodically starting a thread pool instance; using the system time obtained by a daemon thread during each startup of the thread pool instance; and assigning the system time to a global time variable.

[0039] To facilitate understanding, this application can be illustrated with a concrete example: a car reports its own data every second, resulting in a massive amount of data reported per unit time. When the system receives data, it needs to add a system time to identify the data. Therefore, in this concurrent scenario, providing a system time value for the data is particularly important for performance. This embodiment of the application can start a thread pool instance that executes a process every millisecond to obtain the system time and assign it to the global time variable, thus updating the current system time.

[0040] In step S103, the system time is assigned to each thread that sent the system time retrieval request.

[0041] This application embodiment can construct a static method to obtain the current time, and return the value of the global time variable as the return value, which can be used by multiple threads in high-concurrency scenarios.

[0042] The method for obtaining system time proposed in this application creates a unique target instance for obtaining system time within a time class. This single target instance is used to obtain system time and is available for use by multiple threads in high-concurrency scenarios. Therefore, each thread does not need to obtain system time independently, saving significant time, improving performance, and solving the problem that in high-concurrency scenarios, multiple threads need to frequently interact with the operating system, consuming a large amount of time.

[0043] The following is combined with Figure 2 The method for obtaining system time according to the embodiments of this application will be described in detail, including the following steps:

[0044] (1) First, create a system time class in singleton pattern;

[0045] (2) This class sets a global time variable in the constructor and constructs a timed task;

[0046] (3) In the constructed scheduled task, create a thread pool and execute it once every millisecond to obtain the system time and assign it to the global time variable;

[0047] (4) Construct a static method to provide the value of the variable for use by multiple threads in high-concurrency scenarios.

[0048] Next, with reference to the accompanying drawings, an apparatus for obtaining system time according to an embodiment of this application is described.

[0049] Figure 3 This is a block diagram of a device for obtaining system time according to an embodiment of this application.

[0050] like Figure 3 As shown, the device 10 for obtaining system time includes: a first creation module 100, a second creation module 200, and an assignment module 300.

[0051] The first creation module 100 is used to create a time class for obtaining system time; the second creation module 200 is used to create a unique target instance for obtaining system time in the time class, use the target instance to obtain system time, and obtain the system time acquisition request of each thread in the concurrent scenario; the assignment module 300 is used to assign the system time to each thread that sends the system time acquisition request.

[0052] In one embodiment of this application, the second creation module 200 is further used to privatize the constructor of the time class; and to create a unique thread pool instance for obtaining system time based on the constructor.

[0053] In one embodiment of this application, the second creation module 200 is further configured to create a timed task within the constructor; and to create a thread pool instance of the timed task within the timed task.

[0054] In one embodiment of this application, a daemon thread for obtaining system time is created within the thread pool instance.

[0055] In one embodiment of this application, the apparatus for obtaining system time further includes a third creation module.

[0056] The third creation module is used to create a global time variable within the constructor while creating a unique thread pool instance that obtains the system time according to the constructor.

[0057] In one embodiment of this application, the default value of the global time variable is the current system time.

[0058] In one embodiment of this application, the second creation module 200 is further used to periodically start the thread pool instance; during each startup of the thread pool instance, the system time obtained by the daemon thread is used; and the system time is assigned to the global time variable.

[0059] It should be noted that the foregoing explanation of the method embodiment for obtaining system time also applies to the apparatus for obtaining system time in this embodiment, and will not be repeated here.

[0060] According to the apparatus for obtaining system time proposed in the embodiments of this application, a unique target instance for obtaining system time is created in the time class for obtaining system time. A single target instance is used to obtain system time and is provided to multiple threads in high-concurrency scenarios. Therefore, each thread does not need to obtain system time independently, saving a significant amount of time, improving acquisition performance, and solving the problem that in high-concurrency scenarios, multiple threads need to frequently interact with the operating system to obtain system time, consuming a large amount of time.

[0061] Figure 4 A schematic diagram of the structure of a server provided in an embodiment of this application. The server may include:

[0062] The memory 401, the processor 402, and the computer program stored on the memory 401 and capable of running on the processor 402.

[0063] When processor 402 executes a program, it implements the method for obtaining system time provided in the above embodiments.

[0064] Furthermore, the server also includes:

[0065] Communication interface 403 is used for communication between memory 401 and processor 402.

[0066] The memory 401 is used to store computer programs that can run on the processor 402.

[0067] The memory 401 may include high-speed RAM memory, and may also include non-volatile memory, such as at least one disk storage device.

[0068] If the memory 401, processor 402, and communication interface 403 are implemented independently, then the communication interface 403, memory 401, and processor 402 can be interconnected via a bus to complete communication between them. The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be divided into address buses, data buses, control buses, etc. For ease of representation, Figure 4 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.

[0069] Optionally, in a specific implementation, if the memory 401, processor 402, and communication interface 403 are integrated on a single chip, then the memory 401, processor 402, and communication interface 403 can communicate with each other through an internal interface.

[0070] Processor 402 may be a central processing unit (CPU), an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of this application.

[0071] This embodiment also provides a computer-readable storage medium storing a computer program thereon, characterized in that the program, when executed by a processor, implements the above-described method for obtaining system time.

[0072] In the description of this specification, the references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of this application. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.

[0073] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of that feature. In the description of this application, "N" means at least two, such as two, three, etc., unless otherwise explicitly specified.

[0074] Any process or method described in the flowchart or otherwise herein can be understood as representing a module, segment, or portion of code comprising one or N executable instructions for implementing custom logic functions or processes, and the scope of the preferred embodiments of this application includes additional implementations in which functions may be performed not in the order shown or discussed, including substantially simultaneously or in reverse order depending on the functions involved, as should be understood by those skilled in the art to which embodiments of this application pertain.

[0075] It should be understood that the various parts of this application can be implemented using hardware, software, firmware, or a combination thereof. In the above embodiments, the N steps or methods can be implemented using software or firmware stored in memory and executed by a suitable instruction execution system. For example, if implemented in hardware as in another embodiment, it can be implemented using any one or a combination of the following techniques known in the art: discrete logic circuits having logic gates for implementing logical functions on data signals, application-specific integrated circuits (ASICs) having suitable combinational logic gates, programmable gate arrays (PGAs), field-programmable gate arrays (FPGAs), etc.

[0076] Those skilled in the art will understand that all or part of the steps of the methods in the above embodiments can be implemented by a program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, the program includes one or a combination of the steps of the method embodiments.

Claims

1. A method for obtaining system time, characterized in that, Includes the following steps: Create a time class to retrieve the system time; Create a unique target instance for obtaining the system time in the time class, use the target instance to obtain the system time, and obtain the system time acquisition request for each thread in the concurrent scenario; The system time is assigned to each thread that sent the system time retrieval request. Creating a unique target instance for obtaining system time in the time class includes: privatizing the constructor of the time class; creating a scheduled task within the constructor; and creating a thread pool instance for the scheduled task within the scheduled task, wherein a daemon thread for obtaining system time is created within the thread pool instance. In addition to creating a unique thread pool instance that obtains the system time according to the constructor, the method also includes: creating a global time variable within the constructor, wherein the default value of the global time variable is the current system time; The step of obtaining the system time using the target instance includes: periodically starting the thread pool instance; using the system time obtained by the daemon thread during each startup of the thread pool instance; and assigning the system time to the global time variable.

2. A device for acquiring system time, characterized in that, include: The first module is used to create a time class that retrieves the system time. The second creation module is used to create a unique target instance for obtaining system time in the time class, use the target instance to obtain the system time, and obtain the system time acquisition request of each thread in the concurrent scenario; The assignment module is used to assign the system time to each thread that sent the system time acquisition request; The second creation module is further used to: privatize the constructor of the time class; create a timed task within the constructor; and create a thread pool instance for the timed task within the timed task, wherein a daemon thread for obtaining system time is created within the thread pool instance. The third creation module is used to create a global time variable within the constructor while creating a unique thread pool instance that obtains the system time according to the constructor. The default value of the global time variable is the current system time. The second creation module is further configured to: periodically start the thread pool instance; during each startup of the thread pool instance, use the system time obtained by the daemon thread; and assign the system time to the global time variable.

3. A server, characterized in that, include: A memory, a processor, and a computer program stored in the memory and executable on the processor, the processor executing the program to implement the method for obtaining system time as described in claim 1.

4. A computer-readable storage medium having a computer program stored thereon, characterized in that, The program is executed by the processor to implement the method for obtaining system time as described in claim 1.