CN114691376A - Thread execution method and device, electronic equipment and storage medium - Google Patents

Thread execution method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN114691376A
CN114691376A CN202210374857.4A CN202210374857A CN114691376A CN 114691376 A CN114691376 A CN 114691376A CN 202210374857 A CN202210374857 A CN 202210374857A CN 114691376 A CN114691376 A CN 114691376A
Authority
CN
China
Prior art keywords
thread
synchronous lock
lock
queue
synchronous
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.)
Pending
Application number
CN202210374857.4A
Other languages
Chinese (zh)
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.)
Shenzhen TCL New Technology Co Ltd
Original Assignee
Shenzhen TCL New 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 Shenzhen TCL New Technology Co Ltd filed Critical Shenzhen TCL New Technology Co Ltd
Priority to CN202210374857.4A priority Critical patent/CN114691376A/en
Publication of CN114691376A publication Critical patent/CN114691376A/en
Pending legal-status Critical Current

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/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/526Mutual exclusion algorithms
    • 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/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/5038Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the execution order of a plurality of tasks, e.g. taking priority or time dependency constraints into consideration
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2209/00Indexing scheme relating to G06F9/00
    • G06F2209/50Indexing scheme relating to G06F9/50
    • G06F2209/5021Priority

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The embodiment of the application discloses a thread execution method, a thread execution device, electronic equipment and a storage medium; the method and the device can receive a synchronous lock acquisition instruction aiming at the current thread; detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain state information of the synchronous lock; when the state information of the synchronous lock is detected to be in a non-idle state, screening out a target queue from at least two preset queues based on a current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue; the current thread is added to the target queue. The priority of the threads can be considered under the condition of the locking mechanism through the embodiment of the application, so that the time delay of the threads with high priority to be executed is reduced, and the effectiveness of the process to be executed is improved.

Description

Thread execution method and device, electronic equipment and storage medium
Technical Field
The present application relates to the field of communications technologies, and in particular, to a method and an apparatus for executing a thread, an electronic device, and a storage medium.
Background
The thread is the smallest unit of operation scheduling that the operating system can perform, is included in the process, and is the actual operation unit in the process. Multiple threads can be concurrent in a process, each thread executing a different task in parallel. In an operating system, processes cooperating with each other may share some common storage areas that can be read and written to each other, and if threads in both processes need to rewrite the common storage area, a competition relationship occurs between the two threads.
To solve the contention problem, a lock mechanism is proposed in the prior art, that is, when a thread enters a synchronous code block, the usage right of the lock object is obtained. If other threads execute the lock, the lock object is found to be occupied and can only be in a waiting state, and when the threads execute synchronous codes or an exception occurs, the lock can be automatically released. However, some threads may be executed with higher priority, and the existing lock mechanism cannot consider the urgency of the execution of the threads, which may affect the time delay of the execution of the threads, and further affect the effectiveness of the execution of the threads.
Disclosure of Invention
The embodiment of the application provides a thread execution method and device, an electronic device and a storage medium, which can give consideration to the priority of a thread under the condition of a locking mechanism, thereby reducing the time delay of the execution of the thread with high priority and improving the effectiveness of the execution of a process.
The embodiment of the application provides a thread execution method, which comprises the following steps:
receiving a synchronous lock acquisition instruction for a current thread;
detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock;
when the state information of the synchronous lock is detected to be in a non-idle state, screening out a target queue from at least two preset queues based on the current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue;
adding the current thread to the target queue.
Correspondingly, an embodiment of the present application further provides a thread execution apparatus, including:
a receiving unit, configured to receive a synchronization lock acquisition instruction for a current thread;
the detection unit is used for detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock;
the screening unit is used for screening a target queue from at least two preset queues based on the current thread when the state information of the synchronous lock is detected to be in a non-idle state, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue;
and the adding unit is used for adding the current thread into the target queue.
In an embodiment, the screening unit may include:
the identification subunit is used for identifying the priority information of the current thread when the state information of the synchronous lock is detected to be in a non-idle state;
and the screening subunit is used for screening the target queue from at least two preset queues based on the priority information of the current thread.
In an embodiment, the screening subunit may include:
the matching module is used for matching the priority information of the current thread with a preset priority condition;
the first determining module is used for determining the key thread synchronous lock waiting queue as the target queue when the priority information of the current thread meets the preset priority condition;
and the second determining module is used for determining the non-key thread synchronous lock waiting queue as the target queue when the priority information of the current thread does not accord with the preset priority condition.
In an embodiment, the detection unit may include:
a synchronous lock acquisition subunit, configured to acquire the synchronous lock for the current thread based on the synchronous lock acquisition instruction, and start monitoring of the synchronous lock to obtain a monitoring result;
and the information generating subunit is used for generating the state information of the synchronous lock based on the monitoring result.
In an embodiment, the thread execution apparatus may further include:
the queue detection unit is used for detecting the key thread synchronous lock waiting queue when detecting that the state information of the synchronous lock is in an idle state, so as to obtain the number of threads in the key thread synchronous lock waiting queue;
the selecting unit is used for selecting the thread to be executed from the key thread synchronous lock waiting queue through the awakening module based on the number of the threads;
and the lock adding unit is used for adding the synchronous lock to the thread to be executed.
In an embodiment, the selecting unit may include:
the comparison subunit is used for comparing the thread quantity with a preset thread threshold value;
and the first selection subunit is used for selecting the thread to be executed from the non-key thread synchronous lock waiting queue through the awakening module when the number of the threads is greater than the preset thread threshold value.
In an embodiment, the selecting unit may further include:
and the second selection subunit is used for selecting the thread to be executed from the key thread synchronous lock waiting queue through the awakening module when the number of the threads is less than or equal to the preset thread threshold value.
Correspondingly, the embodiment of the application also provides an electronic device, which comprises a memory and a processor; the memory stores a computer program, and the processor is configured to execute the computer program in the memory to perform the thread execution method provided in any of the embodiments of the present application.
Correspondingly, an embodiment of the present application further provides a storage medium, where the storage medium stores a computer program, and the computer program, when executed by a processor, implements the thread execution method provided in any embodiment of the present application.
The method and the device can receive a synchronous lock acquisition instruction aiming at the current thread; detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain state information of the synchronous lock; when the state information of the synchronous lock is detected to be in a non-idle state, screening out a target queue from at least two preset queues based on a current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue; the current thread is added to the target queue. The priority of the threads can be considered under the condition of the locking mechanism, so that the time delay of the threads with high priority to be executed is reduced, and the effectiveness of the process to be executed is improved.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts.
Fig. 1 is a schematic view of a scenario of a thread execution method according to an embodiment of the present application;
FIG. 2 is a flowchart illustrating a thread execution method according to an embodiment of the present disclosure;
FIG. 3 is a schematic flow chart illustrating a thread execution method according to an embodiment of the present application;
FIG. 4 is a schematic diagram of another scenario of a thread execution method according to an embodiment of the present application;
FIG. 5 is a schematic flowchart of a thread execution method according to an embodiment of the present application;
FIG. 6 is a schematic structural diagram of a thread execution apparatus according to an embodiment of the present disclosure;
fig. 7 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, however, the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments of the present application. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
The embodiment of the application provides a thread execution method, which can be executed by a thread execution device, and the thread execution device can be integrated in an electronic device. The electronic device may include at least one of a terminal and a server. That is, the thread execution method may be executed by the terminal or the server.
Wherein, this terminal can include personal computer, panel computer, smart television, smart mobile phone, intelligent house, wearable electronic equipment, VR/AR equipment, on-vehicle computer etc..
The server may be an interworking server or a background server among a plurality of heterogeneous systems, an independent physical server, a server cluster or a distributed system formed by a plurality of physical servers, a cloud server providing basic cloud computing services such as cloud service, a cloud database, cloud computing, a cloud function, cloud storage, network service, cloud communication, middleware service, domain name service, security service, big data, an artificial intelligence platform and the like, and the like.
In an embodiment, as shown in fig. 1, the thread executing apparatus may be integrated on an electronic device such as a terminal or a server, so as to implement the thread executing method provided in the embodiment of the present application. Specifically, the electronic device may receive a synchronization lock acquisition instruction for a current thread; detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock; when the state information of the synchronous lock is detected to be in a non-idle state, screening out a target queue from at least two preset queues based on a current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue; the current thread is added to the target queue.
The following are detailed below, and it should be noted that the order of description of the following examples is not intended to limit the preferred order of the examples.
In the embodiments of the present application, the thread execution method provided in the embodiments of the present application will be described from the perspective of integrating a thread execution device into an electronic device.
As shown in fig. 2, a method for executing a thread is provided, and the specific process includes:
101. a synchronization lock acquisition instruction for a current thread is received.
The thread is the smallest unit that the operating system can perform operation scheduling. It is included in the process and is the actual unit of operation in the process. Multiple threads can be concurrent in a process, each thread executing a different task in parallel.
Wherein the current thread may comprise a thread currently generated most recently in the thread execution device.
In one embodiment, a process is a basic unit representing resource allocation and is a basic unit for scheduling operations. For example, a user runs his or her own program through a thread execution device, and the operating system of the thread execution device may create a process and allocate resources for it, such as various tables, memory space, disk space, I/O devices, and the like. The process may then be placed in the process's ready queue. Next, if the process scheduler chooses the thread, it will allocate a Central Processing Unit (CPU) and other related resources to the thread, and the process will only run. A process is therefore the unit of concurrent execution in the system.
In one embodiment, a thread is the smallest unit in a process that performs operations, i.e., the basic unit for performing processor scheduling. If a process is understood to logically operate a task that a system completes, then the thread represents one of many possible subtasks to complete the task. For example, assuming a user launches a database application in a window, the operating system represents a call to the database as a process. Suppose that a user wants to generate a payroll report from a database and transfer the payroll report to a file, which is a subtask; in the process of generating payroll reports, the user can input a database query request, which is a subtask. Thus, the operating system represents each request, payroll report and newly entered data query, as a separate thread in the database process. Threads may be scheduled for execution on the processors independently, thus allowing several threads to each be executing on separate processors in a multi-processor environment. The operating system provides threads to facilitate and efficiently achieve this concurrency.
In one embodiment, in order to improve the efficiency of task execution, multiple threads may be concurrently issued in one thread, and each thread executes different subtasks in parallel, thereby achieving the purpose of task. However, when a plurality of threads complete a task at the same time, in order to ensure correctness, a resource is accessed and operated by the same thread at the same time, and the function of the synchronous lock is to limit the resource and ensure the safety of the thread.
The synchronization lock may be a tag lock, which ensures that each thread can normally execute atomic non-changeable operations, and synchronously listens to objects/synchronization locks/synchronization listeners/muters.
In one embodiment, to ensure the security of thread execution, a synchronization lock may be assigned to a thread based on the resource during thread execution, thereby avoiding resource contention among threads. Thus, the thread execution device may also receive a synchronization lock acquisition instruction for the current thread while the current thread is generating.
For example, assuming a user launches a database application in a window, the operating system represents a call to the database as a process. If the user wants to generate a payroll report from the database and transmit the payroll report to a file, the thread executing device generates a thread for generating a payroll report for the user and receives a synchronous lock acquisition instruction for the current thread.
102. And detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock.
The state information of the synchronization lock may indicate whether the synchronization lock is currently occupied by other threads. For example, when the state information of the synchronization lock is in an idle state, it indicates that the synchronization lock is not currently occupied by other threads. For another example, when the state information of the synchronization lock is in a non-idle state, it indicates that the synchronization lock is currently occupied by other threads.
In one embodiment, the thread execution device may assign a synchronization lock to the current thread based on the synchronization lock acquisition instruction. However, when the resources are all occupied, the synchronization lock is also occupied, and at this time, the current thread cannot be allocated to the synchronization lock immediately and cannot be executed immediately, and needs to enter a waiting queue. Therefore, the synchronous lock can be detected based on the synchronous lock acquisition instruction, and the state information of the synchronous lock is obtained.
For example, when the state information of the synchronization lock is in an idle state, then the synchronization lock may be assigned to the current thread to ensure that the current thread may be executed safely.
For another example, when the state information of the synchronization lock is in a non-idle state, the target queue may be screened out from at least two preset queues based on the current thread, where the at least two preset queues include at least one key thread synchronization lock waiting queue and at least one non-key thread synchronization lock waiting queue.
103. And when the state information of the synchronous lock is detected to be in a non-idle state, screening out a target queue from at least two preset queues based on the current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue.
In one embodiment, when the state information of the synchronization lock is detected to be in a non-idle state, it indicates that the current thread cannot be executed immediately, and the current thread needs to be added into the wait queue for waiting to be executed.
The preset queue may include a preset waiting queue.
The target object may include, among other things, a queue that stores the current thread.
In one embodiment, different threads may have different priorities. To increase the effectiveness of thread execution, a higher priority thread is typically executed before a lower priority thread is executed. In the existing lock mechanism, the execution urgency of the thread is not considered. Therefore, in the embodiment of the present application, at least two preset queues are set up in the existing locking mechanism. The at least two preset queues may include at least one critical thread synchronization lock waiting queue and at least one non-critical thread synchronization lock waiting queue.
The critical thread synchronization lock waiting queue can be used for storing the threads with higher execution urgency. While the non-critical thread synchronization lock wait queue may be used to store threads that are less urgent to execute.
When the thread execution device detects that a synchronization lock is in an idle state, the thread execution device may first screen out a thread to be executed from the key thread synchronization lock waiting queue, and allocate the idle synchronization lock to the thread to be executed. When all threads in the critical thread synchronization lock waiting queue are executed, the thread executing device selects the threads from the non-critical thread synchronization lock waiting queue for continuous execution.
In one embodiment, the urgency with which a thread is executed may be reflected by setting a priority for the thread. For example, a higher priority may be set for a thread when the urgency with which the thread is executed is higher. And a lower priority may be set for a thread when the urgency of the thread being executed is low.
Among these, threads that are executed with a higher urgency may be referred to as critical threads. Threads that are less urgent to be executed may be referred to as normal threads.
Therefore, when the target queue is screened out from the at least two preset queues based on the current thread, the target queue can be screened out from the at least two preset queues based on the priority information of the current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue.
For example, two preset queues are set in the thread execution device, wherein one preset queue is a key thread synchronization lock waiting queue, and the other preset queue is a non-key thread synchronization lock waiting queue.
When the priority of the current thread is higher, the key thread synchronization lock waiting queue can be determined as the target queue of the current thread.
And when the priority of the current thread is lower, the non-critical thread synchronization lock waiting queue can be determined as the target queue of the current thread.
104. The current thread is added to the target queue.
In one embodiment, after determining the target queue for the current thread, the current thread may be added to the target queue so that the current thread may wait in the corresponding queue to be executed.
For example, the current thread may be added to the critical thread synchronization lock wait queue when the target queue of the current thread is determined to be the critical thread synchronization lock wait queue.
For another example, when it is determined that the target queue of the current thread is a non-critical thread synchronization lock wait queue, the current thread may be added to the non-critical thread synchronization lock wait queue.
When the thread execution device detects that a synchronization lock is in an idle state, the thread execution device may first screen out a thread to be executed from the key thread synchronization lock waiting queue, and allocate the idle synchronization lock to the thread to be executed. When all threads in the critical thread synchronization lock waiting queue are executed, the thread executing device selects the threads from the non-critical thread synchronization lock waiting queue for continuous execution.
The embodiment of the application provides a thread execution method, which comprises the following steps: receiving a synchronous lock acquisition instruction for a current thread; detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock; when the state information of the synchronous lock is detected to be in a non-idle state, screening out a target queue from at least two preset queues based on a current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue; the current thread is added to the target queue. In the method provided in the embodiment of the present application, in order to solve the problem that the existing lock mechanism cannot consider the urgency of execution of the threads, at least two preset queues are set in the existing lock mechanism for caching the threads that cannot be executed temporarily. Wherein the at least two preset queues include at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue. The critical thread synchronization lock waiting queue can be used for storing threads with higher execution urgency, and the non-critical thread synchronization lock waiting queue can be used for storing threads with lower execution urgency. By the key thread synchronous lock waiting queue and the non-key thread synchronous lock waiting queue, when the thread execution device distributes synchronous locks for the threads, the execution priority of the threads can be considered, so that the threads with higher execution priority can be executed faster, the time delay of the threads with higher priority to be executed is reduced, and the effectiveness of the process to be executed is improved.
The method described in the above examples is further illustrated in detail below by way of example.
The method of the embodiment of the present application will be described by taking an example in which a thread execution method is integrated on a server. Specifically, as shown in fig. 3, a flow of the thread execution method provided in the embodiment of the present application may include:
201. the server receives a synchronization lock acquisition instruction for the current thread.
Wherein the current thread may comprise a thread currently newly generated in the server.
The synchronization lock acquiring instruction may include an instruction for the server to allocate a synchronization lock for the current thread.
For example, assuming that a user launches a database application in a window, the operating system of the server represents a call to the database as a process. Assuming that a user wants to generate a payroll report from a database and transmit the payroll report to a file, the server generates a thread for generating a payroll report for the user and receives a synchronous lock acquisition instruction for the current thread.
202. And the server detects the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock.
The state information of the synchronization lock may indicate whether the synchronization lock is currently occupied by other threads. For example, when the state information of the synchronization lock is in an idle state, it indicates that the synchronization lock is not currently occupied by other threads. For another example, when the state information of the synchronization lock is in a non-idle state, it indicates that the synchronization lock is currently occupied by other threads.
In one embodiment, there are various ways to detect the synchronization lock and obtain the status information of the synchronization lock.
For example, the server may acquire a synchronization lock for the current thread based on the synchronization lock acquisition instruction and monitor whether the current thread successfully acquires the synchronization lock. And if the current thread successfully acquires the synchronous lock, indicating that the state information of the synchronous lock is in an idle state. And if the current thread does not successfully acquire the synchronous lock, indicating that the state information of the synchronous lock is in a non-idle state. Specifically, the step of detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock may include:
acquiring a synchronous lock for the current thread based on the synchronous lock acquisition instruction, and starting monitoring on the synchronous lock to obtain a monitoring result;
and generating the state information of the synchronous lock based on the monitoring result.
For example, the server may acquire a synchronization lock for the current thread based on the synchronization lock acquisition instruction. When the server acquires the synchronous lock for the current thread, the current thread can start monitoring the synchronous lock, and whether the current thread successfully acquires the synchronous lock is monitored.
When the current thread successfully acquires the synchronous lock, the state information of the synchronous lock is in an idle state. And when the current thread does not successfully acquire the synchronous lock, the state information of the synchronous lock is in a non-idle state.
In addition, the synchronous lock can be identified to obtain a locking identifier of the synchronous lock, wherein the locking identifier is used for indicating whether the synchronous lock is occupied or not. The status information of the synchronization lock may then be determined based on the spoken identification of the synchronization lock.
For example, when the lock identification of the synchronization lock is that the synchronization lock is occupied, the status information of the synchronization lock is in a non-idle state. For another example, when the locking flag of the synchronization lock is that the synchronization lock is unoccupied, it indicates that the state information of the synchronization lock is in an idle state.
203. When the server detects that the state information of the synchronous lock is in a non-idle state, screening out a target queue from at least two preset queues based on the current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue.
In an embodiment, when the server detects that the state information of the synchronization lock is in a non-idle state, the server may screen out a target queue from at least two preset queues based on the priority information of the current thread. Specifically, the step of screening out a target queue from at least two preset queues based on a current thread when it is detected that the state information of the synchronization lock is in a non-idle state may include:
when the state information of the synchronous lock is detected to be in a non-idle state, identifying the priority information of the current thread;
and screening out a target queue from at least two preset queues based on the priority information of the current thread.
The priority information may reflect how urgent the thread is to be executed. For example, a higher priority of a thread may indicate a higher urgency of the thread being executed. For example, the lower the priority of a thread, the lower the urgency of execution of the thread.
Among these, threads that are executed with a higher urgency may be referred to as critical threads. Threads that are less urgent to be executed may be referred to as normal threads.
The critical threads may include tasks that are perceptible to the user. For example, a high priority thread may include a task that may show a change in the screen, and so on. For example, when a user opens a file, a change in the screen of the display screen may be caused, and at this time, a thread corresponding to the user opening the file may be a thread with a higher priority, and so on.
In an embodiment, priorities may be set in advance for both tasks and subtasks that can be generated by the server, so that threads corresponding to the tasks have corresponding priority information. In addition, a preset priority condition can be preset, and the priority level of each thread can be judged according to the preset priority condition. Specifically, the step "screening out a target queue from at least two preset queues based on the priority information of the current thread" may include:
matching the priority information of the current thread with a preset priority condition;
when the priority information of the current thread meets the preset priority condition, determining a key thread synchronous lock waiting queue as a target queue;
and when the priority information of the current thread does not meet the preset priority condition, determining the non-key thread synchronous lock waiting queue as a target queue.
For example, priority information of a thread may be represented by data. For example, the priority information of a thread may be divided into 4 levels, 1, 2, 3, and 4, respectively. Where the level 4 priority may be made highest and the level 1 priority may be made lowest. Further, the pre-set priority condition may be set as a priority threshold. For example, the priority threshold may be set to 3. When the priority information of the current thread is greater than or equal to 3, the thread is a thread with higher priority, and the key thread synchronization lock waiting queue is determined to be the target queue of the current thread. And when the priority of the current thread is less than 3, the thread is a thread with lower priority, and the non-critical thread synchronization lock waiting queue is determined to be the target queue of the current thread.
204. The server adds the current thread to the target queue.
In one embodiment, after the server determines the target queue for the current thread, the current thread may be added to the target queue.
When the server detects that a synchronization lock is in an idle state, the server may first screen out a thread to be executed from the key thread synchronization lock waiting queue, and allocate the idle synchronization lock to the thread to be executed. When all threads in the critical thread synchronization lock waiting queue are executed, the server selects threads from the non-critical thread synchronization lock waiting queue for continuous execution. Specifically, after the step "add current thread to target queue", the method may include:
when the state information of the synchronous lock is detected to be an idle state, detecting a key thread synchronous lock waiting queue to obtain the number of threads in the key thread synchronous lock waiting queue;
based on the number of threads, a thread to be executed is selected from the key thread synchronous lock waiting queue through a wake-up module;
and adding the synchronous lock to the thread to be executed.
In one embodiment, the state information of the synchronization lock may continue to be detected after the current thread is added to the target queue. When the state information of the synchronous lock is detected to be in an idle state, the synchronous lock can be allocated to the thread with higher priority, so that the thread with higher priority can be executed first. The threads with higher priority are all in the key thread synchronous lock waiting queue, so that the key thread synchronous lock waiting queue can be detected to obtain the number of the threads in the key thread synchronous lock waiting queue, namely, whether the threads exist in the key thread synchronous lock waiting queue or not is judged. Specifically, the step "based on the number of threads, selecting a thread to be executed from the waiting queue of the key thread synchronization lock through the wakeup module" may include:
comparing the number of threads with a preset thread threshold;
and when the number of the threads is larger than a preset thread threshold value, selecting the threads to be executed from the non-key thread synchronous lock waiting queue through the awakening module.
Wherein, the preset thread threshold value can be set to 0. For example, when the number of threads in the critical thread synchronization lock waiting queue is greater than 0, it indicates that there are threads in the critical thread synchronization lock, so that the thread to be executed can be selected from the non-critical thread synchronization lock waiting queue by the wake-up module, and an idle synchronization lock is added to the thread to be executed, so that the thread to be executed can be executed safely.
For another example, when the number of threads in the critical thread synchronization lock waiting queue is less than 0, it indicates that there is no thread in the critical thread synchronization lock. At this point, the server may execute the threads in the non-critical thread synchronization lock wait queue. Specifically, the method provided by the embodiment of the present application may further include:
and when the number of the threads is less than or equal to a preset thread threshold value, selecting the thread to be executed from the key thread synchronous lock waiting queue through the awakening module.
In an embodiment, as shown in fig. 4, a system scenario diagram of the method proposed in the embodiment of the present application is shown. The key thread identification module can set the priority of the thread and put the thread into the corresponding queue according to the priority of the thread. For example, when the current thread is a critical thread, the current thread may be placed into a critical thread synchronization lock wait queue. As another example, when the current thread is a normal thread, the current thread may be placed into a non-critical thread synchronization lock wait queue.
When the synchronous lock is released, the wake-up module can preferentially select the thread in the synchronous lock waiting queue to obtain the synchronous lock. If no thread exists in the key thread synchronous lock waiting queue, the awakening module takes out the thread according to the first-in first-out sequence in the non-key thread synchronous lock waiting queue.
In an embodiment, as shown in fig. 5, the method is a complete flow chart of the method provided in the embodiment of the present application.
For example, thread A has acquired a synchronization lock. When thread B (corresponding to the current thread) wants to acquire the synchronization lock, it can be determined whether thread B is a critical thread because the state information of the synchronization lock is a non-idle state. If the thread B is a key thread, the thread B is put into a key thread synchronous lock waiting queue, otherwise, the thread B is put into a non-key thread synchronous lock waiting queue.
After thread A finishes running and releases the synchronization lock, it can be determined whether there is a thread in the critical thread synchronization lock wait queue. If a thread is in the key thread synchronization lock waiting queue, the wake-up module may select the thread to acquire the lock from the key thread synchronization lock waiting queue according to a first-in-first-out logic. For example, the critical thread synchronization lock wait queue includes thread C, thread D, and thread A. Where thread C is the first to enter the critical thread synchronization lock wait queue, and thus, a synchronization lock may be assigned to thread C. If no thread exists in the critical thread synchronization lock waiting queue, the wake-up module can acquire the synchronization lock from the non-critical thread synchronization lock waiting queue according to the first-in first-out logic.
The embodiment of the application provides a thread execution method, which comprises the following steps: the server receives a synchronous lock acquisition instruction aiming at the current thread; the server detects the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock; when the server detects that the state information of the synchronous lock is in a non-idle state, the server screens out a target queue from at least two preset queues based on a current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue; the current thread is added to the target queue. By setting the assigned thread priority, the embodiment of the application can solve the problem that the time delay is influenced because the high-priority thread cannot be awakened in time in the existing locking mechanism, thereby reducing the time delay of the executed thread with high priority and improving the effectiveness of the executed process.
In order to better implement the thread execution method provided by the embodiment of the present application, in an embodiment, a thread execution device is further provided, and the thread execution device may be integrated in an electronic device. The terms are the same as those in the above thread execution method, and details of implementation may refer to the description in the method embodiment.
In an embodiment, a thread execution apparatus is provided, and the thread execution apparatus may be specifically integrated in an electronic device, as shown in fig. 6, and the thread execution apparatus includes: the receiving unit 301, the detecting unit 302, the screening unit 303 and the adding unit 304 are specifically as follows:
a receiving unit 301, configured to receive a synchronization lock acquisition instruction for a current thread;
a detecting unit 302, configured to detect a synchronous lock based on the synchronous lock acquisition instruction, to obtain state information of the synchronous lock;
a screening unit 303, configured to screen out a target queue from at least two preset queues based on the current thread when it is detected that the state information of the synchronous lock is in a non-idle state, where the at least two preset queues include at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue;
an adding unit 304, configured to add the current thread to the target queue.
In an embodiment, the screening unit 303 may include:
the identification subunit is used for identifying the priority information of the current thread when the state information of the synchronous lock is detected to be in a non-idle state;
and the screening subunit is used for screening the target queue from at least two preset queues based on the priority information of the current thread.
In an embodiment, the screening subunit may include:
the matching module is used for matching the priority information of the current thread with a preset priority condition;
the first determining module is used for determining the key thread synchronous lock waiting queue as the target queue when the priority information of the current thread meets the preset priority condition;
and the second determining module is used for determining the non-key thread synchronous lock waiting queue as the target queue when the priority information of the current thread does not accord with the preset priority condition.
In an embodiment, the detecting unit 302 may include:
a synchronous lock acquisition subunit, configured to acquire the synchronous lock for the current thread based on the synchronous lock acquisition instruction, and start monitoring of the synchronous lock to obtain a monitoring result;
and the information generation subunit is used for generating the state information of the synchronous lock based on the monitoring result.
In an embodiment, the thread execution apparatus may further include:
the queue detection unit is used for detecting the key thread synchronous lock waiting queue when detecting that the state information of the synchronous lock is in an idle state, so as to obtain the number of threads in the key thread synchronous lock waiting queue;
the selecting unit is used for selecting the thread to be executed from the key thread synchronous lock waiting queue through the awakening module based on the number of the threads;
and the lock adding unit is used for adding the synchronous lock to the thread to be executed.
In an embodiment, the selecting unit may include:
the comparison subunit is used for comparing the thread quantity with a preset thread threshold value;
and the first selection subunit is used for selecting the thread to be executed from the non-key thread synchronous lock waiting queue through the awakening module when the number of the threads is greater than the preset thread threshold value.
In an embodiment, the selecting unit may further include:
and the second selecting subunit is used for selecting the thread to be executed from the key thread synchronous lock waiting queue through the awakening module when the number of the threads is less than or equal to the preset thread threshold value.
In a specific implementation, the above units may be implemented as independent entities, or may be combined arbitrarily to be implemented as the same or several entities, and the specific implementation of the above units may refer to the foregoing method embodiments, which are not described herein again.
The thread executing device gives consideration to the priority of the thread under the condition of the locking mechanism, thereby reducing the time delay of the thread with high priority to be executed and improving the effectiveness of the process to be executed.
The embodiment of the application also provides an electronic device, which can comprise a terminal or a server; for example, the electronic device may be a server, such as a thread execution function or the like. As shown in fig. 7, it shows a schematic structural diagram of a terminal according to an embodiment of the present application, specifically:
the electronic device may include components such as a processor 401 of one or more processing cores, memory 402 of one or more computer-readable storage media, a power supply 403, and an input unit 404. Those skilled in the art will appreciate that the electronic device configuration shown in fig. 7 does not constitute a limitation of the electronic device and may include more or fewer components than shown, or some components may be combined, or a different arrangement of components. Wherein:
the processor 401 is a control center of the electronic device, connects various parts of the whole electronic device by using various interfaces and lines, and performs various services of the electronic device and processes data by running or executing software programs and/or modules stored in the memory 402 and calling data stored in the memory 402, thereby performing overall monitoring of the electronic device. Optionally, processor 401 may include one or more processing cores; preferably, the processor 401 may integrate an application processor and a modem processor, wherein the application processor mainly handles operating systems, user pages, application programs, and the like, and the modem processor mainly handles wireless communications. It will be appreciated that the modem processor described above may not be integrated into the processor 401.
The memory 402 may be used to store software programs and modules, and the processor 401 executes various service applications and data processing by operating the software programs and modules stored in the memory 402. The memory 402 may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required by at least one service (such as a sound playing service, an image playing service, etc.), and the like; the storage data area may store data created according to use of the computer device, and the like. Further, the memory 402 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. Accordingly, the memory 402 may also include a memory controller to provide the processor 401 access to the memory 402.
The electronic device further comprises a power supply 403 for supplying power to the various components, and preferably, the power supply 403 is logically connected to the processor 401 through a power management system, so that services such as charging, discharging, and power consumption management are managed through the power management system. The power supply 403 may also include any component of one or more dc or ac power sources, recharging systems, power failure detection circuitry, power converters or inverters, power status indicators, and the like.
The electronic device may further include an input unit 404, and the input unit 404 may be used to receive input numeric or character information and generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and service control.
Although not shown, the electronic device may further include a display unit and the like, which are not described in detail herein. Specifically, in this embodiment, the processor 401 in the electronic device loads the executable file corresponding to the process of one or more application programs into the memory 402 according to the following instructions, and the processor 401 runs the application programs stored in the memory 402, thereby implementing various services, as follows:
receiving a synchronous lock acquisition instruction for a current thread;
detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock;
when the state information of the synchronous lock is detected to be in a non-idle state, screening out a target queue from at least two preset queues based on the current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue;
adding the current thread to the target queue.
The above operations can be implemented in the foregoing embodiments, and are not described in detail herein.
According to an aspect of the application, there is provided a computer program application or computer program comprising computer instructions stored in a computer readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions to cause the computer device to perform the method provided in the various alternative implementations of the above embodiments.
It will be understood by those skilled in the art that all or part of the steps of the methods of the above embodiments may be performed by a computer program, which may be stored in a computer-readable storage medium and loaded and executed by a processor, or by related hardware controlled by the computer program.
To this end, the present application further provides a storage medium, in which a computer program is stored, where the computer program can be loaded by a processor to execute the steps in any one of the thread execution methods provided in the present application. For example, the computer program may perform the steps of:
receiving a synchronous lock acquisition instruction for a current thread;
detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock;
when the state information of the synchronous lock is detected to be in a non-idle state, screening out a target queue from at least two preset queues based on the current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue;
adding the current thread to the target queue.
The above operations can be implemented in the foregoing embodiments, and are not described in detail herein.
Since the computer program stored in the storage medium can execute the steps in any of the thread execution methods provided in the embodiments of the present application, beneficial effects that can be achieved by any of the thread execution methods provided in the embodiments of the present application can be achieved, which are detailed in the foregoing embodiments and will not be described again here.
The above detailed description is provided for a thread execution method, a thread execution apparatus, an electronic device, and a storage medium according to embodiments of the present application, and a specific example is applied in the present application to explain the principles and implementations of the present application, and the description of the above embodiments is only used to help understanding the method and the core idea of the present application; meanwhile, for those skilled in the art, according to the idea of the present application, there may be variations in the specific embodiments and the application scope, and in summary, the content of the present specification should not be construed as a limitation to the present application.

Claims (10)

1. A method of thread execution, comprising:
receiving a synchronous lock acquisition instruction for a current thread;
detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock;
when the state information of the synchronous lock is detected to be in a non-idle state, screening a target queue from at least two preset queues based on the current thread, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue;
adding the current thread to the target queue.
2. The method of claim 1, wherein the screening out a target queue from at least two preset queues based on the current thread when detecting that the status information of the synchronization lock is in a non-idle status comprises:
when the state information of the synchronous lock is detected to be in a non-idle state, identifying the priority information of the current thread;
and screening out a target queue from at least two preset queues based on the priority information of the current thread.
3. The method of claim 2, wherein the screening out a target queue from at least two preset queues based on the priority information of the current thread comprises:
matching the priority information of the current thread with a preset priority condition;
when the priority information of the current thread meets the preset priority condition, determining the key thread synchronous lock waiting queue as the target queue;
and when the priority information of the current thread does not meet the preset priority condition, determining the non-key thread synchronous lock waiting queue as the target queue.
4. The method of claim 1, wherein detecting a synchronization lock based on the synchronization lock acquisition instruction to obtain the status information of the synchronization lock comprises:
acquiring the synchronous lock for the current thread based on the synchronous lock acquisition instruction, and starting monitoring on the synchronous lock to obtain a monitoring result;
and generating the state information of the synchronous lock based on the monitoring result.
5. The method of claim 1, wherein after the adding the current thread to the target queue, further comprising:
when the state information of the synchronous lock is detected to be an idle state, detecting the key thread synchronous lock waiting queue to obtain the number of threads in the key thread synchronous lock waiting queue;
based on the number of the threads, a thread to be executed is selected from the key thread synchronous lock waiting queue through a wake-up module;
and adding the synchronous lock to the thread to be executed.
6. The method of claim 5, wherein the retrieving, by a wake-up module, a thread to be executed from the critical thread synchronization lock wait queue based on the number of threads comprises:
comparing the thread quantity with a preset thread threshold value;
and when the number of the threads is larger than the preset thread threshold value, selecting the threads to be executed from the non-key thread synchronous lock waiting queue through a wakeup module.
7. The method of claim 6, further comprising:
and when the number of the threads is less than or equal to the preset thread threshold value, selecting the thread to be executed from the key thread synchronous lock waiting queue through a wake-up module.
8. A thread execution apparatus, comprising:
a receiving unit, configured to receive a synchronization lock acquisition instruction for a current thread;
the detection unit is used for detecting the synchronous lock based on the synchronous lock acquisition instruction to obtain the state information of the synchronous lock;
the screening unit is used for screening a target queue from at least two preset queues based on the current thread when the state information of the synchronous lock is detected to be in a non-idle state, wherein the at least two preset queues comprise at least one key thread synchronous lock waiting queue and at least one non-key thread synchronous lock waiting queue;
and the adding unit is used for adding the current thread into the target queue.
9. An electronic device comprising a memory and a processor; the memory stores a computer program, and the processor is configured to execute the computer program in the memory to perform the thread execution method according to any one of claims 1 to 7.
10. A storage medium storing a plurality of computer programs adapted to be loaded by a processor to perform the thread execution method of any one of claims 1 to 7.
CN202210374857.4A 2022-04-11 2022-04-11 Thread execution method and device, electronic equipment and storage medium Pending CN114691376A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210374857.4A CN114691376A (en) 2022-04-11 2022-04-11 Thread execution method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210374857.4A CN114691376A (en) 2022-04-11 2022-04-11 Thread execution method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN114691376A true CN114691376A (en) 2022-07-01

Family

ID=82143248

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210374857.4A Pending CN114691376A (en) 2022-04-11 2022-04-11 Thread execution method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN114691376A (en)

Similar Documents

Publication Publication Date Title
US6272517B1 (en) Method and apparatus for sharing a time quantum
US20040181791A1 (en) Task switching apparatus, method and program
JP2013506179A (en) Execution management system combining instruction threads and management method
JPH05204675A (en) Scheduling system
CN109815007A (en) Thread control method, device, electronic equipment and storage medium based on cloud monitoring
CN111459622B (en) Method, device, computer equipment and storage medium for scheduling virtual CPU
CN111897637B (en) Job scheduling method, device, host and storage medium
CN113495780A (en) Task scheduling method and device, storage medium and electronic equipment
CN112860387A (en) Distributed task scheduling method and device, computer equipment and storage medium
CN114637536A (en) Task processing method, computing coprocessor, chip and computer equipment
CN113886069A (en) Resource allocation method and device, electronic equipment and storage medium
CN112148480A (en) Task processing method, device and equipment based on multithreading and storage medium
CN111597044A (en) Task scheduling method and device, storage medium and electronic equipment
CN114461365A (en) Process scheduling processing method, device, equipment and storage medium
EP0913770A2 (en) Method and apparatus for sharing a time quantum
CN113495787A (en) Resource allocation method, device, storage medium and electronic equipment
JP2001117786A (en) Process scheduling device and process scheduling method
CN111831436A (en) Scheduling method and device of IO (input/output) request, storage medium and electronic equipment
CN114691376A (en) Thread execution method and device, electronic equipment and storage medium
US6766508B1 (en) Object-oriented system having anonymous scheduler design pattern
CN115629854A (en) Distributed task scheduling method, system, electronic device and storage medium
CN114035926A (en) Application thread scheduling method and device, storage medium and electronic equipment
JP2008225641A (en) Computer system, interrupt control method and program
CN112269658B (en) Composite resource application method and system, electronic equipment and storage medium
CN115220908A (en) Resource scheduling method, device, electronic equipment and storage medium

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