CN109213577B - Thread sleep method and device and computer equipment - Google Patents

Thread sleep method and device and computer equipment Download PDF

Info

Publication number
CN109213577B
CN109213577B CN201710523203.2A CN201710523203A CN109213577B CN 109213577 B CN109213577 B CN 109213577B CN 201710523203 A CN201710523203 A CN 201710523203A CN 109213577 B CN109213577 B CN 109213577B
Authority
CN
China
Prior art keywords
time
sleep
microsecond
thread
current thread
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201710523203.2A
Other languages
Chinese (zh)
Other versions
CN109213577A (en
Inventor
杨亮
张文明
陈少杰
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shanghai Langyu Health Technology Group Co ltd
Original Assignee
Wuhan Douyu Network Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Wuhan Douyu Network Technology Co Ltd filed Critical Wuhan Douyu Network Technology Co Ltd
Priority to CN201710523203.2A priority Critical patent/CN109213577B/en
Publication of CN109213577A publication Critical patent/CN109213577A/en
Application granted granted Critical
Publication of CN109213577B publication Critical patent/CN109213577B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/466Transaction processing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • G06F9/4843Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Telephone Function (AREA)
  • Measurement Of The Respiration, Hearing Ability, Form, And Blood Characteristics Of Living Organisms (AREA)

Abstract

The invention provides a method, a device and computer equipment for thread sleep, wherein the method comprises the following steps: storing the acquired microsecond time of the historical sleep time of each thread into a pre-established array; acquiring an identification ID of a current thread; based on the ID, searching whether a time item corresponding to the ID exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID; determining the total microsecond time of the current thread needing sleeping by utilizing the preset second microsecond time and the preset first microsecond time of the time length of the current thread needing sleeping; determining millisecond time M of the total microsecond time relative to unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable a current thread to sleep for M milliseconds if the millisecond time M is the positive number; therefore, the called sleep function can be accurate to microsecond when executing the sleep time, the thread can be ensured to accurately wake up after sleeping, and the execution efficiency of the thread is improved.

Description

Thread sleep method and device and computer equipment
Technical Field
The invention belongs to the technical field of communication, and particularly relates to a method and a device for thread sleep and computer equipment.
Background
When a Windows system is developed, in order to reduce consumption of a Central Processing Unit (CPU), an Application program function (API) Sleep provided by the system is usually called to Sleep a program for a certain time.
However, the precision of the function is not enough, and in some cases, when the function is called to make the program sleep for one millisecond, the function does not wake up as expected and further influences the execution logic of the thread, and further influences the running efficiency of the thread.
Based on this, the invention provides a method, an apparatus and a computer device for thread sleep, so as to solve the above problems in the prior art.
Disclosure of Invention
Aiming at the problems in the prior art, the embodiments of the present invention provide a method, an apparatus, and a computer device for thread sleep, which are used to solve the technical problems in the prior art that the execution efficiency of a thread is reduced because the accuracy of a sleep function is not high and the thread cannot wake up accurately because the sleep function is called to enable the thread to sleep for a certain duration so as to reduce the power consumption of a CPU.
The invention provides a method for thread sleep, which comprises the following steps:
storing the acquired microsecond time of the historical sleep time of each thread into each time item of an array, wherein the array is established in advance;
acquiring the Identification (ID) of the current thread;
based on the ID, searching whether a time item corresponding to the ID exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID;
determining the total microsecond time of the current thread needing to sleep by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the first microsecond time;
and determining the millisecond time M of the total microsecond time relative to unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M millisecond if the millisecond time M is the positive number.
In the above solution, if the time entry does not exist in the array, the method further includes:
and adding a time item corresponding to the current ID in the array, and setting microsecond time of the added time item as a preset numerical value.
In the foregoing solution, the determining, by using the preset second microsecond time of the sleep duration required by the current thread at the current time and the preset first microsecond time, the total microsecond time that the current thread needs to sleep includes:
determining a total microsecond time T1 of the current thread needing to sleep by using a formula T1 ═ (timeArray [ id ] + T) × delta, wherein the timeArray [ id ] is the first microsecond time, and the T is the second microsecond time; the delta is the thread response error.
In the scheme, the thread response error delta is determined according to a formula delta T2/(Tend-Tstart); the T2 is the third microsecond time when the current thread actually sleeps, Tstart is the time point when the execution of the sleep function starts, and Tend is the time point when the execution of the sleep function ends.
In the above scheme, when the delta is greater than 1, the total microsecond time for which the current thread needs to sleep is determined by using a formula T3 ═ timeArray [ id ] + T + (timeArray [ id ] + T) (. delta-1); wherein the (timeArray [ id ] + T) (. delta-1) is a time difference compensation value.
In the above scheme, when the delta is less than 1, the total microsecond time for which the current thread needs to sleep is determined by using a formula T4 ═ timeArray [ id ] + T- (timeArray [ id ] + T) × (delta-1); wherein the (timeArray [ id ] + T) (. delta-1) is a time difference compensation value.
In the foregoing scheme, after determining that the total microsecond time is relative to a millisecond time M of one millisecond, the method further includes:
determining the residual microsecond time U of the total microsecond time by using a formula U-T1-M1000, and storing the residual microsecond time U into a time item corresponding to the ID; wherein the T1 is the total microsecond time for which the current thread needs to sleep.
The invention also provides a thread sleep device, which comprises:
the storage unit is used for storing the acquired microsecond time of the historical sleep time of each thread into each time item of an array, and the array is established in advance;
the acquiring unit is used for acquiring the identification ID of the current thread; based on the ID, searching whether a time item corresponding to the ID exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID;
the determining unit is used for determining the total microsecond time of the current thread needing sleeping by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the preset first microsecond time;
and the calling unit is used for determining the millisecond time M of the total microsecond time relative to a unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M milliseconds if the millisecond time M is the positive number.
The present invention also provides a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, performs the steps of:
storing the acquired microsecond time of the historical sleep time of each thread into each time item of an array, wherein the array is established in advance;
acquiring an identification ID of a current thread;
based on the ID, searching whether a time item corresponding to the ID exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID;
determining the total microsecond time of the current thread needing to sleep by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the first microsecond time;
and determining the millisecond time M of the total microsecond time relative to unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M millisecond if the millisecond time M is the positive number.
The invention also provides a computer device for thread sleep, which comprises:
at least one processor; and
at least one memory communicatively coupled to the processor, wherein,
the memory stores program instructions executable by the processor, the processor being capable of performing the method as described in any one of the above.
The invention provides a method, a device and computer equipment for thread sleep, wherein the method comprises the following steps: storing the acquired microsecond time of the historical sleep time of each thread into each time item of an array, wherein the array is established in advance; acquiring an identification ID of a current thread; based on the ID, searching whether a time item corresponding to the ID exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID; determining the total microsecond time of the current thread needing to sleep by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the first microsecond time; determining millisecond time M of the total microsecond time relative to unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable a current thread to sleep for M millisecond if the millisecond time M is the positive number; therefore, when the total microsecond time that the current thread needs to sleep is determined, the microsecond level is taken as a unit, the called sleep function can be accurate to the microsecond when the sleep time is executed, the precision of the sleep function is further improved, the thread can be ensured to be accurately awakened after the sleep, and the execution efficiency of the thread is improved.
Drawings
Fig. 1 is a flowchart illustrating a thread sleep method according to an embodiment of the present invention;
fig. 2 is a schematic overall structure diagram of a thread sleep apparatus according to a second embodiment of the present invention;
fig. 3 is a schematic diagram of an overall structure of a thread sleep computer device according to a third embodiment of the present invention.
Detailed Description
The invention provides a method, a device and computer equipment for thread sleep, which are used for solving the technical problems that in the prior art, a thread sleeps for a certain time by calling a sleep function provided by a system so as to reduce the CPU power consumption, and the execution efficiency of the thread is reduced because the thread cannot accurately wake up due to low precision of the sleep function, and the method comprises the following steps: storing the acquired microsecond time of the historical sleep time of each thread into each time item of an array, wherein the array is established in advance; acquiring an identification ID of a current thread; based on the ID, searching whether a time item corresponding to the ID exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID; determining the total microsecond time of the current thread needing to sleep by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the first microsecond time; and determining the millisecond time M of the total microsecond time relative to the unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M milliseconds if the millisecond time M is the positive number.
The technical solution of the present invention is further described in detail by the accompanying drawings and the specific embodiments.
Example one
The embodiment provides a method for thread sleep, as shown in fig. 1, the method comprises
S101, storing the acquired microsecond time of the historical sleep time of each thread into each time item of the array;
in this step, an array timeArray is pre-established, and each time item of the array is used for storing microsecond time of historical sleep time of each thread. It should be noted that the historical sleep duration of each thread refers to a sum of the remaining microseconds (i.e., the microsecond time less than one millisecond) after each thread executes the sleep function.
S102, acquiring an identification ID of a current thread; based on the ID, searching whether a time item corresponding to the ID exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID;
in this step, if a sleep function Usleep is called to execute the sleep time of the current thread, a GetCurrentThreadId function is used to obtain an identifier ID of the current thread, and based on the ID, whether a time item corresponding to the ID exists is searched in the array timeArray; and if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID.
If the time item does not exist in the array, adding the time item corresponding to the current ID in the array, and setting microsecond time in the added time item as a preset numerical value; generally, the predetermined value is 0.
S103, determining the total microsecond time of the current thread needing to sleep by utilizing the preset second microsecond time of the current thread needing to sleep and the first microsecond time;
in this step, after the first microsecond time of the historical sleep duration corresponding to the ID is obtained, the total microsecond time that the current thread needs to sleep is determined by using the preset second microsecond time that the current thread needs to sleep at the current time and the first microsecond time; and the preset second microsecond time of the sleep time needed by the current thread at the current time is the assumed microsecond time needed by the current thread at the current time.
Specifically, determining the total microsecond time T1 for the current thread to sleep by using formula (1);
T1=(timeArray[id]+T)*delta (1)
wherein, in formula (1), the timeArray [ id ] is the first microsecond time, and the first microsecond time substantially refers to the microsecond time less than one millisecond stored in the array before the current thread executes the sleep function; the T is a second microsecond time; the delta is the thread response error. The thread response error is used for measuring the accuracy of the system executing the sleep function Usleep, namely, when the sleep function Usleep is called, so that the actual sleep time of the current thread is the third microsecond time T2, the time ratio consumed by the time when the current thread finishes sleeping and continues to execute is obtained. Here, a time point at which the execution of the sleep function is started is denoted as Tstart, and a time point at which the execution of the sleep function is ended is denoted as Tend.
Then, the thread response error delta can be calculated using equation (2):
delta=T2/(Tend–Tstart) (2)
it should be noted that, when the delta is equal to 1, a second microsecond time T indicating a preset sleep-required time of the current thread at the current time is exactly equal to a third microsecond time T2 when the current thread actually sleeps; then the second microsecond time T is very accurate.
When the delta is greater than 1, the second microsecond time T of the preset sleep duration of the current thread at the current time is greater than the third microsecond time T2 of the actual sleep of the current thread; therefore, the actual total microsecond time T3 that the current thread needs to sleep is calculated by equation (3):
T3=timeArray[id]+T+(timeArray[id]+T)*(delta–1) (3)
wherein the (timeArray [ id ] + T) (. delta-1) is a time difference compensation value.
When the delta is less than 1, the second microsecond time T of the preset sleep duration of the current thread at the current time is less than the third microsecond time T2 of the actual sleep of the current thread; therefore, the actual total microsecond time T4 that the current thread needs to sleep is calculated by equation (4):
T4=timeArray[id]+T-(timeArray[id]+T)*(delta–1) (4)
wherein the (timeArray [ id ] + T) (. delta-1) is a time difference compensation value.
And when the error between delta and 1 is smaller, the accuracy of the sleep function USleep is larger, and the CPU consumption is smaller, and when the error between delta and 1 is larger, the accuracy of the sleep function USleep is smaller, and the CPU consumption is larger.
And S104, determining the millisecond time M of the total microsecond time relative to the unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M milliseconds if the millisecond time M is the positive number.
In this step, after the total microsecond time T1 is obtained, the millisecond time M of the total microsecond time relative to a unit millisecond is determined by using the formula (5), so that the precision of the sleep function is positioned to the microsecond level, and the precision of the sleep function is improved:
M=floor(T1,1000) (5)
wherein floor is an integer function
Then, the remaining microsecond time U of the total microsecond time is determined using formula (6), and the remaining microsecond time U is saved into the historical sleep time corresponding to the ID using formula (7).
U=T1–M*1000 (6)
timeArray[id]=U (7)
After acquiring the millisecond time M, judging whether the millisecond time M is a positive number, namely whether the millisecond time M is greater than 0; and if the millisecond time M is a positive number, calling a sleep function to enable the current thread to sleep for M milliseconds. If the millisecond time M is 0, the sleep function is not called.
Example two
Corresponding to the first embodiment, this embodiment further provides a thread sleep device, as shown in fig. 2, the device includes: a storage unit 21, an acquisition unit 22, a determination unit 23, and a calling unit 24; wherein the content of the first and second substances,
the storage unit 21 is configured to store the acquired time items of the microsecond time of the historical sleep duration of each thread into each time item of an array timeArray, where the array timeArray is pre-established. It should be noted that the historical sleep duration of each thread refers to a sum of the remaining microseconds (i.e., the microsecond time less than one millisecond) after each thread executes the sleep function.
If the sleep function Usleep is called to execute the sleep time of the current thread, the obtaining unit 22 is configured to obtain the identifier ID of the current thread by using the GetCurrentThreadId function; based on the ID, searching whether a time item corresponding to the ID exists in the array timeArray; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID;
if the time item does not exist in the array, the obtaining unit 22 further needs to add the time item corresponding to the current ID in the array timeArray, and set the microsecond time in the added time item to a preset value; generally, the predetermined value is 0.
After the obtaining unit 22 obtains the first microsecond of the historical sleep time corresponding to the ID, the determining unit 23 is configured to determine the total microsecond time that the current thread needs to sleep by using a preset second microsecond time of the sleep time that the current thread needs to sleep at the current time and the first microsecond time. And the preset second microsecond time of the sleep time needed by the current thread at the current time is the assumed microsecond time needed by the current thread at the current time.
Specifically, the determining unit 23 determines the total microsecond time T1 for the current thread to sleep by using formula (1);
T1=(timeArray[id]+T)*delta (1)
wherein, in formula (1), the timeArray [ id ] is the first microsecond time, and the first microsecond time substantially refers to the microsecond time less than one millisecond stored in the array before the current thread executes the sleep function; the T is a second microsecond time; the delta is the thread response error. The thread response error is used for measuring the accuracy of the system executing the sleep function Usleep, namely, when the sleep function Usleep is called, so that the actual sleep time of the current thread is the third microsecond time T2, the time ratio consumed by the time when the current thread finishes sleeping and continues to execute is obtained. Here, a time point at which the execution of the sleep function is started is denoted as Tstart, and a time point at which the execution of the sleep function is ended is denoted as Tend.
Then, the determining unit 23 may also calculate the thread response error delta using equation (2):
delta=T2/(Tend–Tstart) (2)
it should be noted that, when the delta is equal to 1, a second microsecond time T indicating a preset sleep-required time of the current thread at the current time is exactly equal to a third microsecond time T2 when the current thread actually sleeps; then the second microsecond time T is very accurate.
When the delta is greater than 1, the second microsecond time T of the preset sleep duration of the current thread at the current time is greater than the third microsecond time T2 of the actual sleep of the current thread; the determining unit 23 calculates the actual total microsecond time T3 for the current thread to sleep by using equation (3):
T3=timeArray[id]+T+(timeArray[id]+T)*(delta–1) (3)
wherein the (timeArray [ id ] + T) (. delta-1) is a time difference compensation value.
When the delta is less than 1, the second microsecond time T of the preset sleep duration of the current thread at the current time is less than the third microsecond time T2 of the actual sleep of the current thread; therefore, the determining unit 23 calculates the actual total microsecond time T4 for the current thread to sleep by using equation (4):
T4=timeArray[id]+T-(timeArray[id]+T)*(delta–1) (4)
wherein the (timeArray [ id ] + T) (. delta-1) is a time difference compensation value.
And when the error between delta and 1 is smaller, the accuracy of the sleep function USleep is larger, and the CPU consumption is smaller, and when the error between delta and 1 is larger, the accuracy of the sleep function USleep is smaller, and the CPU consumption is larger.
When the total microsecond time T1 is determined, the calling unit 24 is configured to determine the total microsecond time T1 relative to the millisecond time M of the unit millisecond using equation (5), so as to position the accuracy of the sleep function to the microsecond level, thereby improving the accuracy of the sleep function:
M=floor(T1,1000) (5)
wherein floor is an integer function
Then, the calling unit 24 determines the remaining microsecond time U of the total microsecond time by using formula (6), and saves the remaining microsecond time U to the historical sleep time corresponding to the ID by using formula (7).
U=T1–M*1000 (6)
timeArray[id]=U (7)
After acquiring the millisecond time M, the calling unit 24 determines whether the millisecond time M is a positive number, that is, whether the millisecond time M is greater than 0; and if the millisecond time M is a positive number, calling a sleep function to enable the current thread to sleep for M milliseconds. If the millisecond time M is 0, the sleep function is not called.
EXAMPLE III
The embodiment further provides a computer device for thread sleep, as shown in fig. 3, the computer device includes: radio Frequency (RF) circuitry 310, memory 320, input unit 330, display unit 340, audio circuitry 350, WiFi module 360, processor 370, and power supply 380. Those skilled in the art will appreciate that the computer device configuration illustrated in FIG. 3 does not constitute a limitation of computer devices, and may include more or fewer components than those illustrated, or some components may be combined, or a different arrangement of components.
The following describes the components of the computer device in detail with reference to fig. 3:
RF circuitry 310 may be used for receiving and transmitting signals, and in particular, for receiving downlink information from base stations and processing the received information to processor 350. In general, the RF circuit 310 includes, but is not limited to, at least one Amplifier, transceiver, coupler, Low Noise Amplifier (LNA), duplexer, and the like.
The memory 320 may be used to store software programs and modules, and the processor 350 executes various functional applications and data processing of the computer device by operating the software programs and modules stored in the memory 320. The memory 320 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application program required for at least one function, and the like; the storage data area may store data created according to use of the computer device, and the like. Further, the memory 320 may include high speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device.
The input unit 330 may be used to receive input numeric or character information and generate key signal inputs related to user settings and function control of the computer apparatus. Specifically, the input unit 330 may include a keyboard 331 and other input devices 332. The keyboard 331 can collect the input operation of the user thereon and drive the corresponding connection device according to a preset program. The keyboard 331 collects the output information and sends it to the processor 350. The input unit 330 may include other input devices 332 in addition to the keyboard 331. In particular, other input devices 332 may include, but are not limited to, one or more of a touch panel, function keys (such as volume control keys, switch keys, etc.), a trackball, a mouse, a joystick, and the like.
The display unit 340 may be used to display information input by a user or information provided to the user and various menus of the computer device. The Display unit 340 may include a Display panel 341, and optionally, the Display panel 341 may be configured in the form of a Liquid Crystal Display (LCD), an Organic Light-Emitting Diode (OLED), or the like. Further, the keyboard 331 may cover the display panel 341, and when the keyboard 331 detects a touch operation thereon or nearby, the keyboard 331 is transmitted to the processor 350 to determine the type of the touch event, and then the processor 350 provides a corresponding visual output on the display panel 341 according to the type of the input event. Although the keyboard 331 and the display panel 341 are shown in fig. 3 as two separate components to implement input and output functions of the computer device, in some embodiments, the keyboard 331 and the display panel 341 may be integrated to implement input and output functions of the computer device.
Audio circuitry 350, speaker 351, microphone 352 may provide an audio interface between a user and a computer device. The audio circuit 350 may transmit the electrical signal converted from the received audio data to the speaker 351, and the electrical signal is converted into a sound signal by the speaker 351 and output;
WiFi belongs to short-distance wireless transmission technology, and computer equipment can help a user to receive and send e-mails, browse webpages, access streaming media and the like through the WiFi module 360, and provides wireless broadband internet access for the user. Although fig. 3 shows the WiFi module 360, it is understood that it does not belong to the essential constitution of the computer device, and may be omitted entirely as needed within the scope not changing the essence of the invention.
The processor 370 is a control center of the computer device, connects various parts of the entire computer device using various interfaces and lines, performs various functions of the computer device and processes data by operating or executing software programs and/or modules stored in the memory 320 and calling data stored in the memory 320, thereby monitoring the computer device as a whole. Alternatively, processor 370 may include one or more processing units; preferably, the processor 370 may be integrated with an application processor, wherein the application processor primarily handles operating systems, user interfaces, application programs, and the like.
The computer device also includes a power supply 380 (such as a power adapter) for powering the various components, which may preferably be logically connected to the processor 370 through a power management system.
The thread sleep method, the thread sleep device and the computer equipment provided by the invention have the beneficial effects that at least:
the invention provides a method, a device and computer equipment for thread sleep, wherein the method comprises the following steps: storing the acquired microsecond time of the historical sleep time of each thread into each time item in an array, wherein the array is established in advance; acquiring an identification ID of a current thread; based on the ID, searching whether a time item corresponding to the ID exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID; determining the total microsecond time of the current thread needing to sleep by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the first microsecond time; determining millisecond time M of the total microsecond time relative to unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M millisecond if the millisecond time M is the positive number; therefore, when the total microsecond time that the current thread needs to sleep is determined, the microsecond level is taken as a unit, the called sleep function can be accurate to the microsecond when the sleep time is executed, the precision of the sleep function is further improved, the thread can be ensured to be accurately awakened after the sleep, and the execution efficiency of the thread is improved.
The algorithms and displays presented herein are not inherently related to any particular computer, virtual machine, or other apparatus. Various general purpose systems may also be used with the teachings herein. The required structure for constructing such a system will be apparent from the description above. Moreover, the present invention is not directed to any particular programming language. It is appreciated that a variety of programming languages may be used to implement the teachings of the present invention as described herein, and any descriptions of specific languages are provided above to disclose the best mode of the invention.
In the description provided herein, numerous specific details are set forth. It is understood, however, that embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures and techniques have not been shown in detail in order not to obscure an understanding of this description.
Similarly, it should be appreciated that in the foregoing description of exemplary embodiments of the invention, various features of the invention are sometimes grouped together in a single embodiment, figure, or description thereof for the purpose of streamlining the disclosure and aiding in the understanding of one or more of the various inventive aspects. However, the disclosed method should not be interpreted as reflecting an intention that: that the invention as claimed requires more features than are expressly recited in each claim. Rather, as the following claims reflect, inventive aspects lie in less than all features of a single foregoing disclosed embodiment. Thus, the claims following the detailed description are hereby expressly incorporated into this detailed description, with each claim standing on its own as a separate embodiment of this invention.
Those skilled in the art will appreciate that the modules in the device in an embodiment may be adaptively changed and disposed in one or more devices different from the embodiment. The modules or units or components of the embodiments may be combined into one module or unit or component, and furthermore they may be divided into a plurality of sub-modules or sub-units or sub-components. All of the features disclosed in this specification (including any accompanying claims, abstract and drawings), and all of the processes or elements of any method or apparatus so disclosed, may be combined in any combination, except combinations where at least some of such features and/or processes or elements are mutually exclusive. Each feature disclosed in this specification (including any accompanying claims, abstract and drawings) may be replaced by alternative features serving the same, equivalent or similar purpose, unless expressly stated otherwise.
Furthermore, those skilled in the art will appreciate that while some embodiments herein include some features included in other embodiments, rather than other features, combinations of features of different embodiments are meant to be within the scope of the invention and form different embodiments. For example, in the following claims, any of the claimed embodiments may be used in any combination.
The various component embodiments of the invention may be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. Those skilled in the art will appreciate that a microprocessor or Digital Signal Processor (DSP) may be used in practice to implement some or all of the functionality of some or all of the components of a gateway, proxy server, system according to embodiments of the present invention. The present invention may also be embodied as apparatus or device programs (e.g., computer programs and computer program products) for performing a portion or all of the methods described herein. Such programs implementing the present invention may be stored on a computer-readable storage medium or may be in the form of one or more signals. Such a signal may be downloaded from an internet website, or provided on a carrier signal, or provided in any other form; the program when executed by a processor implements the steps of: storing the acquired microsecond time of the historical sleep time of each thread into each time item in an array, wherein the array is established in advance; acquiring an identification ID of a current thread; based on the ID, searching whether a time item corresponding to the ID exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the ID; determining the total microsecond time of the current thread needing to sleep by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the first microsecond time; and determining the millisecond time M of the total microsecond time relative to unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M millisecond if the millisecond time M is the positive number.
It should be noted that the above-mentioned embodiments illustrate rather than limit the invention, and that those skilled in the art will be able to design alternative embodiments without departing from the scope of the appended claims. 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 invention may 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.
The above description is only exemplary of the present invention and should not be taken as limiting the scope of the present invention, and any modifications, equivalents, improvements, etc. that are within the spirit and principle of the present invention should be included in the present invention.

Claims (10)

1. A method of thread sleep, the method comprising:
storing the acquired microsecond time of the historical sleep time of each thread into each time item of an array, wherein the array is established in advance;
acquiring an identification ID of a current thread;
based on the identification ID of the current thread, searching whether a time item corresponding to the identification ID of the current thread exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the identification ID of the current thread;
determining the total microsecond time of the current thread needing to sleep by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the first microsecond time;
determining the total microsecond time of the current thread needing to sleep relative to the millisecond time M of a unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M milliseconds if the millisecond time M is the positive number.
2. The method of claim 1, wherein if the time entry does not exist in the array, further comprising:
and adding a time item corresponding to the identification ID of the current thread in the array, and setting microsecond time of the added time item as a preset numerical value.
3. The method of claim 1, wherein the determining the total microsecond time for the current thread to sleep by using the preset second microsecond time for the sleep duration of the current thread and the first microsecond time comprises:
determining a total microsecond time T1 of the current thread needing to sleep by using a formula T1 ═ (timeArray [ id ] + T) × delta, wherein the timeArray [ id ] is the first microsecond time, and the T is the second microsecond time; the delta is the thread response error.
4. The method of claim 3 wherein the thread response error delta is determined according to the formula delta T2/(Tend-Tstart); the T2 is the third microsecond time when the current thread actually sleeps, Tstart is the time point when the execution of the sleep function starts, and Tend is the time point when the execution of the sleep function ends.
5. The method of claim 3, wherein when the delta is greater than 1, the total microseconds time that the current thread needs to sleep is determined using the formula T3 ═ timeArray [ id ] + T + (timeArray [ id ] + T) (. delta-1); wherein the (timeArray [ id ] + T) (. delta-1) is a time difference compensation value.
6. The method of claim 3, wherein when the delta is less than 1, the total microseconds time that the current thread needs to sleep is determined using the formula T4 ═ timeArray [ id ] + T- (timeArray [ id ] + T) (. delta-1); wherein the (timeArray [ id ] + T) (. delta-1) is a time difference compensation value.
7. The method of claim 1, wherein determining that the total microsecond time that the current thread needs to sleep relative to millisecond time M in units of milliseconds further comprises:
determining the residual microsecond time U of the total microsecond time required by the current thread to sleep by using a formula U-T1-M1000, and storing the residual microsecond time U into a time item corresponding to the identification ID of the current thread; wherein the T1 is the total microsecond time for which the current thread needs to sleep.
8. An apparatus for thread sleep, the apparatus comprising:
the storage unit is used for storing the acquired microsecond time of the historical sleep time of each thread into each time item of an array, and the array is established in advance;
the acquiring unit is used for acquiring the identification ID of the current thread; based on the identification ID of the current thread, searching whether a time item corresponding to the identification ID of the current thread exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the identification ID of the current thread;
the determining unit is used for determining the total microsecond time of the current thread needing sleeping by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the preset first microsecond time;
and the calling unit is used for determining the millisecond time M of the total microsecond time relative to a unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M milliseconds if the millisecond time M is the positive number.
9. A computer-readable storage medium, on which a computer program is stored, which program, when executed by a processor, carries out the steps of:
storing the acquired microsecond time of the historical sleep time of each thread into each time item of an array, wherein the array is established in advance;
acquiring an identification ID of a current thread;
based on the identification ID of the current thread, searching whether a time item corresponding to the identification ID of the current thread exists in the array; if the time item exists, acquiring the first microsecond time of the historical sleep time corresponding to the identification ID of the current thread;
determining the total microsecond time of the current thread needing to sleep by utilizing the preset second microsecond time of the sleep time of the current thread at the current time and the first microsecond time;
and determining the millisecond time M of the total microsecond time relative to unit millisecond, judging whether the millisecond time M is a positive number, and calling a sleep function to enable the current thread to sleep for M millisecond if the millisecond time M is the positive number.
10. A computer device for thread sleep, comprising:
at least one processor; and
at least one memory communicatively coupled to the processor, wherein,
the memory stores program instructions executable by the processor, the processor invoking the program instructions to perform the method of any of claims 1 to 7.
CN201710523203.2A 2017-06-30 2017-06-30 Thread sleep method and device and computer equipment Active CN109213577B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201710523203.2A CN109213577B (en) 2017-06-30 2017-06-30 Thread sleep method and device and computer equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201710523203.2A CN109213577B (en) 2017-06-30 2017-06-30 Thread sleep method and device and computer equipment

Publications (2)

Publication Number Publication Date
CN109213577A CN109213577A (en) 2019-01-15
CN109213577B true CN109213577B (en) 2020-09-08

Family

ID=64976435

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201710523203.2A Active CN109213577B (en) 2017-06-30 2017-06-30 Thread sleep method and device and computer equipment

Country Status (1)

Country Link
CN (1) CN109213577B (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2023004662A1 (en) * 2021-07-29 2023-02-02 华为技术有限公司 Delay generation method and related device

Family Cites Families (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN100511155C (en) * 2006-12-18 2009-07-08 上海未来宽带技术及应用工程研究中心有限公司 Method for implementing non accumulation inaccuracy and self-adaption timer
US8136113B2 (en) * 2006-12-20 2012-03-13 International Business Machines Corporation Method and apparatus for adjusting sleep time of fixed high-priority threads
CN100588280C (en) * 2007-07-24 2010-02-03 北京天碁科技有限公司 Method and device for calibrating sleep clock of TD-SCDMA terminal
CN100489788C (en) * 2007-09-30 2009-05-20 金蝶软件(中国)有限公司 Thread wakening control systems and method
CN106815065B (en) * 2015-11-27 2020-02-11 腾讯科技(深圳)有限公司 Background application awakening method and device and electronic equipment
CN106775620B (en) * 2016-11-14 2020-05-12 武汉斗鱼网络科技有限公司 Timing method and device

Also Published As

Publication number Publication date
CN109213577A (en) 2019-01-15

Similar Documents

Publication Publication Date Title
CN107277029B (en) Remote procedure call method and device and computer equipment
KR101722042B1 (en) Power efficient location notification
EP3214544B1 (en) User equipment power management method and power management device
CN106874077B (en) Process running method and device
CN106775965B (en) Processing method and device for timing task
EP2882234B1 (en) Method, apparatus, equipment and system for controlling turn-on and turn-off of wireless network
US9179341B2 (en) Method and system for simplifying WiFi setup for best performance
CN104778055B (en) Method and device for displaying progress bar
CN107506251B (en) Method and device for resetting intelligent equipment
CN107479985B (en) Remote procedure call method and device and computer equipment
CN109213577B (en) Thread sleep method and device and computer equipment
CN104809055A (en) Application program test method and device based on cloud platform
CN107122036B (en) Method and device for adjusting frequency of central processing unit
CN103197944A (en) Method for determining performance of process of application program and computer system
CN108234663B (en) Method and device for acquiring data and computer equipment
CN108549512B (en) Display method and device and computer equipment
CN111177612A (en) Method and related device for authenticating page login
CN106658556B (en) A kind of method and mobile terminal receiving data
CN108874381B (en) Method and device for outputting page view controller and electronic equipment
CN108028851B (en) Equipment group management method, device and system
CN106339230B (en) Graphical interface display method and device
US10506378B2 (en) Apparatus and method for notifying the apparatus in both first state and second state based on detecting conditions
CN108616552B (en) Webpage access method, device and system
CN111007916B (en) Terminal device time setting method and system, storage medium and terminal device
US8819274B2 (en) Selecting a network connection for data communications with a networked device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant
TR01 Transfer of patent right

Effective date of registration: 20230724

Address after: 200000 building 10, No. 860, Xinyang Road, Lingang New District, China (Shanghai) pilot Free Trade Zone, Fengxian District, Shanghai

Patentee after: Shanghai Langyu Health Technology (Group) Co.,Ltd.

Address before: 430000 East Lake Development Zone, Wuhan City, Hubei Province, No. 1 Software Park East Road 4.1 Phase B1 Building 11 Building

Patentee before: WUHAN DOUYU NETWORK TECHNOLOGY Co.,Ltd.

TR01 Transfer of patent right