Multi-threaded Printing Scheduling Method, Device, Electronic Device and Storage Medium

By using thread locks and ring queues to manage print logs in multi-threaded systems, the problem of garbled characters and resource competition in multi-threaded printing is solved, and orderly and efficient print output is achieved.

CN120066433BActive Publication Date: 2025-07-08QIJING INFORMATION TECHNOLOGY (SHANGHAI) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510550743.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-04-29
Publication Date
2025-07-08
Estimated Expiration
2045-04-29

AI Technical Summary

Technical Problem

In a multi-threaded system, when multiple threads write data to the serial port at the same time, it is easy to cause character interleaving, garbled code and resource competition problems, reducing printing efficiency and code complexity.

Method used

通过线程锁对缓存区加锁,将打印日志存入缓存区并记录到环形队列, 由独立的打印线程负责输出,确保信息完整性和效率。

Benefits of technology

The orderly processing of multi-threaded printing tasks is realized, avoiding garbled characters and resource competition, and improving printing efficiency and system stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120066433B_ABST
    Figure CN120066433B_ABST
Patent Text Reader

Abstract

An embodiment of the present application discloses a multi-threaded printing scheduling method, device, electronic device, and storage medium. In this solution, when any service thread outputs a print log, a thread lock is used to lock the buffer area, the string corresponding to the print log is allocated to the buffer area, the address information corresponding to the buffer area is recorded in a circular queue, and then the thread lock is released. The print thread accesses the address information in the circular queue to obtain the print log in the buffer area and sends the print log to the target serial port to complete the printing task. The embodiment of the present application manages the caching of printing tasks of different threads through a unified interface and hands over the actual output to an independent print thread, which not only ensures the integrity of the output information but also improves the printing efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the technical field of multi-threaded printing, and particularly to a multi-threaded printing scheduling method, apparatus, electronic device, and storage medium. Background Art

[0002] In an embedded system and a multi-threaded application environment, the output of debugging information is a key link in system development and maintenance. Usually, multiple threads need to output their respective running states or log information through a shared debugging serial port. When the system executes some high-concurrency tasks such as printing tasks, multiple threads may simultaneously attempt to write data to the serial port. Currently, the serial port printing function of most multi-threaded systems still adopts a direct output method, that is, each thread directly sends the content to be printed to the specified serial port device in its internal logic.

[0003] Although this method is simple and easy to implement, it has many defects in practical applications. For example, when a thread has not completed the current printing task, another thread may have inserted its own printing content, resulting in character interleaving or garbled characters in the finally output information, which is difficult to interpret and analyze. At the same time, multiple threads accessing the serial port hardware simultaneously are also prone to resource competition problems. In addition, since each thread needs to independently complete the entire process from data splicing to serial port writing, the repeated operations not only increase the code complexity but also reduce the overall printing efficiency. Summary of the Invention

[0004] This application provides a multi-threaded printing scheduling method, apparatus, electronic device, and storage medium, which can perform cache management on the printing tasks of different threads through a unified interface and hand them over to an independent printing thread for actual output, not only ensuring the integrity of the output information but also improving the printing efficiency.

[0005] This application provides a multi-threaded printing scheduling method, including:

[0006] When any business thread outputs a printing log, lock the buffer through a thread lock;

[0007] Allocate the string corresponding to the printing log to the buffer, record the address information corresponding to the buffer in a circular queue, and release the thread lock;

[0008] Access the address information in the circular queue through a printing thread to obtain the printing log in the buffer;

[0009] Send the printing log to the target serial port to complete the printing task.

[0010] Optionally, the step of determining that a business thread outputs a printing log includes:

[0011] When generating a log in any business thread, obtain the printing level of the log and the identifier of the associated printing module;

[0012] Judge whether the log meets the printing condition according to the printing level and the identifier;

[0013] If so, determine that the log is a printable log.

[0014] Optionally, the step of allocating the string corresponding to the printable log to the buffer includes:

[0015] Combine the original data of the printable log with a preset format template to generate a complete log text;

[0016] Allocate the string corresponding to the log text to the buffer and add printing parameters.

[0017] Optionally, the step of allocating the string corresponding to the printable log to the buffer includes:

[0018] Determine the length of the string corresponding to the printable log;

[0019] Select an idle area from multiple pre-allocated buffer pools of different specifications according to the length to cache the string to the idle area.

[0020] Optionally, the step of accessing the address information in the circular queue by the printing thread includes:

[0021] Initialize the buffer and the circular queue, and detect the status of the circular queue through the printing thread;

[0022] When it is detected that the circular queue has the address information, lock the buffer again through the thread lock, and release the thread lock after completing the printing task.

[0023] Optionally, the step of locking the buffer through the thread lock includes:

[0024] Obtain the label information of the current business thread or printing thread;

[0025] According to the label information, make the thread lock lock the buffer.

[0026] Optionally, the method further includes:

[0027] When the circular queue contains multiple address information, determine the priority information of each of the multiple address information;

[0028] Send multiple printable logs to the target serial port in sequence according to the priority information.

[0029] The present application also provides a multi-threaded printing scheduling device, including:

[0030] An output module, configured to lock the buffer through a thread lock when any service thread outputs a printing log;

[0031] An allocation module, configured to allocate the string corresponding to the printing log to the buffer, record the address information corresponding to the buffer into a circular queue, and release the thread lock;

[0032] An acquisition module, configured to access the address information in the circular queue through a printing thread to obtain the printing log in the buffer;

[0033] A sending module, configured to send the printing log to a target serial port to complete a printing task.

[0034] The present application also provides an electronic device, where the electronic device includes a memory and a processor. A computer program is stored in the memory, and the processor executes the steps in any one of the multi-threaded printing scheduling methods provided by the present application by calling the computer program stored in the memory.

[0035] The present application also provides a storage medium, where the storage medium stores a computer program, and the computer program is suitable for being loaded by a processor to execute the steps in any one of the multi-threaded printing scheduling methods provided by the present application.

[0036] For the multi-threaded printing scheduling method provided by the present application, when any service thread outputs a printing log, the buffer can be locked through a thread lock, the string corresponding to the printing log is allocated to the buffer, the address information corresponding to the buffer is recorded into a circular queue, and the thread lock is released. The printing thread accesses the address information in the circular queue to obtain the printing log in the buffer, and the printing log is sent to the target serial port to complete the printing task. In the embodiment of the present application, the printing tasks of different threads are managed in a unified interface for caching and are handed over to an independent printing thread to be responsible for actual output, which not only ensures the integrity of the output information but also improves the printing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0037] In order to more clearly illustrate the technical solutions in the present application, the following will briefly introduce the drawings required for description in the embodiments. Obviously, the following drawings are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative efforts.

[0038] Figure 1 is a schematic flowchart of a multi-threaded printing scheduling method provided by an embodiment of the present application;

[0039] Figure 2 It is another flowchart of the multi-threaded printing scheduling method provided by the embodiments of the present application;

[0040] Figure 3 It is a schematic structural diagram of the multi-threaded printing scheduling device provided by the embodiments of the present application;

[0041] Figure 4 It is a schematic structural diagram of the electronic device provided by the embodiments of the present application. Detailed implementation manners

[0042] Here, the exemplary embodiments will be described in detail, and the examples are shown in the drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The implementation manners described in the following exemplary embodiments do not represent all implementation manners consistent with the present application. On the contrary, they are merely examples of the devices and methods consistent with some aspects of the present application as detailed in the appended claims.

[0043] It should be noted that in this article, the terms "include", "comprise" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not expressly listed, or also includes elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "including one..." does not exclude the existence of another identical element in the process, method, article or device including that element. In addition, components, features, and elements with the same name in different embodiments of the present application may have the same meaning or different meanings, and their specific meanings need to be determined by their explanations in the specific embodiments or further combined with the context in the specific embodiments.

[0044] It should be understood that although the steps in the flowchart in the embodiments of the present application are shown in sequence according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless there is a clear indication in this article, the execution of these steps has no strict order limitation, and they can be executed in other orders. Moreover, at least a part of the steps in the figure may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed alternately or alternately with at least a part of other steps or sub-steps or stages of other steps.

[0045] It should be noted that in this document, step codes such as 101 and 102 are adopted. The purpose is to more clearly and briefly express the corresponding content, and it does not constitute a substantial limitation in terms of sequence. Those skilled in the art may execute 102 first and then 101 during specific implementation, etc., but these should all be within the protection scope of this application.

[0046] Referring to "embodiment" in this document means that the specific features, structures, or characteristics described in combination with the embodiment may be included in at least one embodiment of this application. The occurrence of this phrase at various positions in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment mutually exclusive with other embodiments. Those skilled in the art explicitly and implicitly understand that the embodiments described herein can be combined with other embodiments.

[0047] An embodiment of this application provides a multi-threaded printing scheduling method. The execution entity of this multi-threaded printing scheduling method can be the multi-threaded printing scheduling device provided by the embodiment of this application, or a server integrated with this multi-threaded printing scheduling device, where this multi-threaded printing scheduling device can be implemented in a hardware or software manner.

[0048] As Figure 1 shown, Figure 1 is the first process schematic diagram of the multi-threaded printing scheduling method provided by the embodiment of this application. The specific process of this multi-threaded printing scheduling method can be as follows:

[0049] 101. When any service thread outputs a print log, lock the buffer area through a thread lock.

[0050] In an environment where multiple threads execute concurrently, multiple service threads can simultaneously attempt to output print logs. Without an effective protection mechanism, simultaneous access to the buffer area by different threads will cause data competition problems. For example, if thread A and thread B write data to the buffer area simultaneously, it will result in data being overwritten and scrambled with each other, and finally the output print log will appear as garbled or error messages. In this embodiment, a thread lock is used to lock the buffer area, just like adding a "lock" to the buffer area. When a service thread obtains this "lock", other threads cannot access the buffer area until this thread releases the "lock". This can ensure that at the same moment, only one thread can operate on the buffer area, thereby ensuring the integrity and consistency of the buffer area data, laying a foundation for accurately processing print logs subsequently, effectively avoiding common conflict problems when multi-threads operate on shared resources, and being a key step in realizing ordered printing scheduling. Compared with the traditional lock-free operation method, it greatly improves the reliability and stability of the system. It should be noted that in this embodiment, only the write operation to the buffer area is locked, rather than a global lock, thereby reducing the probability of multi-thread contention.

[0051] In one embodiment, the above-mentioned thread lock can be implemented using a lightweight spin lock and a mutex. For example, if the business thread is a real-time task, a spin lock can be used to avoid task delay caused by context switching. If the business thread is a non-real-time task, a mutex can be used to reduce the waste of CPU idle resources.

[0052] In one embodiment, the steps for determining the output of a print log by a business thread may include: when any business thread generates a log, obtaining the print level of the log and the identifier of the associated print module, and determining whether the log meets the print condition based on the print level and the identifier. If so, the log is determined to be a print log. Specifically, the print levels of the above-mentioned logs may include DEBUG (debugging), INFO (information), WARN (warning), ERROR (error), FATAL (fatal), etc. Different print levels correspond to different log contents. For example, logs at the DEBUG level usually contain a large amount of debugging information for troubleshooting problems during the development stage; while logs at the ERROR level record error information that occurs in the system. In addition, the system also includes multiple different functional modules, and each module generates corresponding logs. To distinguish the logs generated by different modules, a unique identifier is assigned to each print module.

[0053] Furthermore, different print level thresholds can be set according to the system configuration or operating environment. For example, in a development environment, the print level threshold can be set to DEBUG, so that all logs at the DEBUG level and above will be printed; while in a production environment, to reduce system overhead and the amount of logs, the threshold can be set to WARN, and only logs at the WARN level and above will be printed. In addition to the print level, the printing of logs can also be controlled based on the identifier of the print module. For example, in some cases, it may only be necessary to pay attention to the logs of specific modules. At this time, it can be set to only print the logs generated by these modules. By determining whether the identifier of the print module associated with the log is in the list of allowed print modules, it is determined whether the print module identifier condition is met. If the log meets the print condition after the above judgment, then the log is determined to be a print log.

[0054] 102. Allocate the string corresponding to the print log to the buffer area, record the address information corresponding to the buffer area in the circular queue, and release the thread lock.

[0055] In a multi-threaded system, the printed log strings generated by each business thread need to be stored in a buffer first and wait for subsequent printing threads to process. The buffer is a pre-allocated memory area that can avoid the blocking problem that occurs when directly printing logs, thereby improving the concurrency performance of the system. When a business thread obtains the access permission to the buffer (successfully locks it), it will copy the printed log string into the buffer. When allocating, the size of the buffer and the length of the log string can also be considered. If the buffer is large enough, the log string can be directly copied into it; if it is not large enough, the log can be truncated or a larger buffer can be re-allocated.

[0056] A circular queue belongs to a data structure that can recycle storage space. Its function is to record the address information of the buffer, so that the printing thread can process the logs in these buffers sequentially. In this embodiment, after the log string is allocated to the buffer, the business thread will record the address information of this buffer at the tail of the circular queue. The circular queue manages the buffer addresses according to the first-in-first-out (FIFO) principle, ensuring that the printing thread can process the logs in order and preventing out-of-order printing.

[0057] In one embodiment, after completing the allocation of the log string and the recording of the buffer address information, the business thread can release the previously acquired thread lock. Releasing the thread lock means that other business threads now have the opportunity to acquire the lock and access the buffer, which can improve the concurrency processing ability of the system and ensure that the printing tasks of different threads can be queued in an orderly manner.

[0058] 103. Access the address information in the circular queue through the printing thread to obtain the printed logs in the buffer.

[0059] In one embodiment, the printing thread is a thread dedicated to outputting the printed logs in the buffer to the target serial port. It first accesses the circular queue, which records the address information of each buffer. The printing thread sequentially obtains the addresses of the buffers according to the order of the address information in the queue. Through these addresses, the printing thread can accurately find the buffer storing the printed logs. For example, the first address recorded in the circular queue points to the buffer storing the printed logs of thread A, and the printing thread will read the logs from this buffer according to this address. This method ensures that the printing thread can process the logs according to the order in which the business threads submit printing tasks, avoiding the problem of out-of-order printing. Different from the traditional multi-threaded independent printing method, this centralized printing thread processing method makes the execution of printing tasks more orderly and efficient, reduces the chaos of printing results, and improves the readability and analysis value of debugging information.

[0060] In one embodiment, the printing thread continuously executes the above steps, constantly obtaining address information from the circular queue, accessing the buffer area and obtaining the printing log until the circular queue is empty. When the circular queue is empty, the printing thread can enter the sleep state, waiting for new log address information to be added to the circular queue, or checking the queue again at regular time intervals.

[0061] 104. Send the printing log to the target serial port to complete the printing task.

[0062] In one embodiment, after the printing thread obtains the printing log from the buffer area, it will send it to the target serial port. During the sending process, the relevant protocols and specifications of serial communication need to be followed to ensure that the data can be transmitted to the serial port device accurately and without error. For example, serial communication may involve the setting of parameters such as baud rate, data bits, and stop bits. The printing thread will format the printing log according to these parameters and then send the data bit by bit. After successfully sending the printing log to the target serial port, a complete printing task is completed. In this way, the complete process from the business thread generating the printing log to the final output on the serial port device is realized. The whole process is uniformly scheduled and managed, effectively avoiding problems such as printing conflicts and garbled codes in the traditional method, improving the stability of the system and the printing efficiency, and meeting the high requirements of the multi-threaded system for printing scheduling in a complex environment.

[0063] In one embodiment, when there are multiple address information in the circular queue, the priority information of each of the multiple address information can also be determined, and then multiple printing logs are sent to the target serial port in sequence according to the priority information. Specifically, in the actual application scenario, the printing logs generated by different services may have different importance. For example, system error logs are usually more important than general debugging logs. When there are multiple address information in the circular queue, the priority of the printing log corresponding to each address needs to be determined. For example, when the business thread stores the log in the buffer area, the priority information of the log is recorded in the circular queue together with the address information, or the priority is dynamically determined according to predefined rules such as the type and source module of the printing log. Next, after sorting these address information according to the priority, the printing thread can access the buffer area in sequence according to the sorted address information order, obtain the corresponding printing log, and send it to the target serial port. This can ensure that important logs can be output in time and avoid being blocked by low-priority logs.

[0064] In summary, when any business thread outputs print logs, the multi-threaded print scheduling method proposed in the embodiments of the present application can lock the buffer area through a thread lock, allocate the string corresponding to the print logs to the buffer area, record the address information corresponding to the buffer area in a circular queue, and release the thread lock. The print thread accesses the address information in the circular queue to obtain the print logs in the buffer area and sends the print logs to the target serial port to complete the print task. The embodiments of the present application manage the print tasks of different threads through a unified interface for caching and hand them over to an independent print thread to be responsible for the actual output, which not only ensures the integrity of the output information but also improves the printing efficiency.

[0065] According to the method described in the previous embodiments, further detailed descriptions will be given below.

[0066] Please refer to Figure 2 , Figure 2 which is the second flow diagram of the multi-threaded print scheduling method provided by the embodiments of the present application. The method includes:

[0067] 201. When any business thread outputs print logs, lock the buffer area through a thread lock.

[0068] In one embodiment, in a multi-threaded environment, multiple business threads may attempt to operate on the buffer area simultaneously. To avoid data competition and inconsistency issues, a thread lock is needed to ensure that only one thread can access the buffer area at the same time. When a certain business thread wants to output print logs, it will first acquire the thread lock, so that other threads must wait for the thread to release the lock before they can access the buffer area, thus ensuring the integrity of the buffer area data.

[0069] 202. Combine the original data of the print logs with a preset format template to generate a complete log text.

[0070] Among them, the original data of the print logs contains key information. To make the logs more readable and standardized, there is usually a preset format template. For example, the format template can stipulate that the logs should contain information such as timestamps, log levels, and thread names. Combining the original data with the format template can generate a complete log text that meets specific format requirements. Such logs will be more convenient for subsequent viewing and analysis.

[0071] 203. Determine the length of the string corresponding to the print logs.

[0072] Among them, the length of the log string is crucial for subsequent buffer area allocation. Log strings of different lengths require different sizes of buffer spaces to store. By determining the length of the string, a basis can be provided for selecting an appropriate buffer area.

[0073] 204. Select an idle area from multiple pre-allocated buffer pools of different specifications according to the length, cache the string in the idle area, and add printing parameters.

[0074] In one embodiment, to improve the utilization efficiency of the buffer area, multiple buffer pools of different specifications can also be pre-allocated. Each buffer pool contains multiple buffer areas of the same size. According to the length of the log string determined in the above steps, select an idle area of an appropriate size from these buffer pools. Store the generated log string in this idle area, and at the same time add printing parameters such as printing speed and font. This can ensure that each log can be stored in a buffer area of an appropriate size, avoiding waste or insufficiency caused by using too large or too small buffer space.

[0075] 205. Record the address information corresponding to the buffer area into the circular queue and release the thread lock.

[0076] In one embodiment, after caching the log string in the idle area and adding printing parameters, the address information of the buffer area can be recorded into the circular queue. The circular queue can manage the buffer area addresses in the order in which the logs enter. Then, release the previously acquired thread lock so that other business threads can continue to access the buffer area.

[0077] 206. Access the address information in the circular queue through the printing thread to obtain the print logs in the buffer area.

[0078] In one embodiment, the step of accessing the address information in the circular queue through the printing thread may include: initializing the buffer area and the circular queue, and detecting the status of the circular queue through the printing thread. When it is detected that there is address information in the circular queue, lock the buffer area again through the thread lock, and release the thread lock after completing the printing task. Among them, initializing the buffer area means allocating a certain amount of memory space for it and setting its initial state to idle or unused. When initializing the circular queue, it is necessary to determine its maximum capacity and set the initial state of the queue to empty. After the printing thread is started, the printing thread can achieve status detection by periodically checking the length of the circular queue or whether it is empty. If the circular queue is empty, the printing thread can enter the sleep state or wait briefly to reduce the consumption of CPU resources. When the printing thread detects that there is address information in the circular queue, it indicates that a business thread has already stored the print logs in the buffer area and recorded the address information of the buffer area in the circular queue. To ensure that no other business thread modifies the buffer area simultaneously when the printing thread accesses the buffer area, it is necessary to lock the buffer area again using the thread lock, and after completing the printing task, release the previously acquired thread lock. After releasing the thread lock, other business threads can access the buffer area again and continue to store new print logs in the buffer area.

[0079] In one embodiment, the step of locking the buffer area through a thread lock may include: obtaining the tag information of the current business thread or printing thread, and locking the buffer area with the thread lock according to the tag information. Specifically, in a multi-threaded environment, there are business threads and printing threads. The business threads are responsible for generating printing logs, and the printing threads are responsible for outputting the logs in the buffer area to the target serial port. When a certain thread (business thread or printing thread) needs to operate on the buffer area, it first needs to obtain its own tag information. This tag information can be the name, ID of the thread, or other custom identifiers. For example, in an e-commerce system, the business threads may include user business threads, order business threads, etc., and the printing thread is specifically responsible for printing various types of logs. After obtaining the tag information of the thread, it will be decided how to lock the buffer area with the thread lock based on this information. Among them, different types of threads may also have different priorities or restrictions on the operation of the buffer area. Through the tag information, the thread lock can perform differential locking processing according to the thread type. For example, the printing thread needs to obtain the access right to the buffer area first because it is responsible for outputting the logs to the target serial port in a timely manner. When the printing thread requests to lock, the thread lock can respond preferentially; while for the business thread, it may need to obtain the lock when the printing thread is idle.

[0080] 207. Send the printing log and printing parameters to the target serial port to complete the printing task.

[0081] After obtaining the printing log in the buffer area, the printing thread will send the log and printing parameters to the target serial port to complete the printing task. After completing the printing task, it is necessary to release the previously obtained thread lock. After releasing the thread lock, other business threads can access the buffer area again and continue to store new printing logs in the buffer area. This can ensure the concurrency performance of the system, enabling multiple threads to alternately operate on the buffer area.

[0082] As described above, when any service thread outputs a print log, the multi-threaded print scheduling method proposed in the embodiments of the present application can lock the buffer area through a thread lock, combine the original data of the print log with a preset format template to generate a complete log text, determine the length of the string corresponding to the print log, select an idle area from multiple pre-allocated buffer pools of different specifications according to the length to cache the string in the idle area, add print parameters, record the address information corresponding to the buffer area in a circular queue, and release the thread lock. The print thread accesses the address information in the circular queue to obtain the print log in the buffer area, and sends the print log and print parameters to the target serial port to complete the printing task. The embodiments of the present application manage the print tasks of different threads through a unified interface for caching and hand them over to an independent print thread for actual output, which not only ensures the integrity of the output information but also improves the printing efficiency.

[0083] To implement the above method, the embodiments of the present application also provide a multi-threaded print scheduling device, which can be specifically integrated in terminal devices such as mobile phones, tablet computers, etc.

[0084] For example, as Figure 3 shown, it is a schematic structural diagram of a multi-threaded print scheduling device provided by the embodiments of the present application. The multi-threaded print scheduling device may include:

[0085] An output module 301, configured to lock the buffer area through a thread lock when any service thread outputs a print log;

[0086] An allocation module 302, configured to allocate the string corresponding to the print log to the buffer area, record the address information corresponding to the buffer area in a circular queue, and release the thread lock;

[0087] An acquisition module 303, configured to access the address information in the circular queue through a print thread to obtain the print log in the buffer area;

[0088] A sending module 304, configured to send the print log to the target serial port to complete the printing task.

[0089] As can be seen from the above, the multi-threaded printing scheduling device proposed in the embodiments of the present application can, when any service thread outputs a printing log, lock the buffer area through a thread lock, allocate the string corresponding to the printing log to the buffer area, record the address information corresponding to the buffer area in a circular queue, and release the thread lock. The printing thread accesses the address information in the circular queue to obtain the printing log in the buffer area and sends the printing log to the target serial port to complete the printing task. The embodiments of the present application manage the printing tasks of different threads through a unified interface and hand over the actual output to an independent printing thread, which not only ensures the integrity of the output information but also improves the printing efficiency.

[0090] Any combination of the above technical solutions can form an optional embodiment of the present application, which will not be elaborated one by one here.

[0091] Those of ordinary skill in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed through instructions or by controlling relevant hardware through instructions. The instructions can be stored in a computer-readable storage medium and loaded and executed by a processor.

[0092] Therefore, the embodiments of the present application provide a computer-readable storage medium, in which multiple computer programs are stored. The computer programs can be loaded by a processor to execute the steps in any of the multi-threaded printing scheduling methods provided by the embodiments of the present application. For example, the computer programs can execute the following steps:

[0093] When any service thread outputs a printing log, lock the buffer area through a thread lock;

[0094] Allocate the string corresponding to the printing log to the buffer area, record the address information corresponding to the buffer area in a circular queue, and release the thread lock;

[0095] The printing thread accesses the address information in the circular queue to obtain the printing log in the buffer area;

[0096] Send the printing log to the target serial port to complete the printing task.

[0097] For the specific implementation of each of the above operations, reference can be made to the previous embodiments, which will not be elaborated here.

[0098] Among them, the storage medium may include: read-only memory (ROM, Read Only Memory), random access memory (RAM, Random Access Memory), magnetic disk or optical disk, etc.

[0099] Since the computer program stored in the storage medium can execute the steps in any of the multi-threaded printing scheduling methods provided in the embodiments of the present application, the beneficial effects achievable by any of the multi-threaded printing scheduling methods provided in the embodiments of the present application can be realized. For details, please refer to the previous embodiments and will not be elaborated here.

[0100] The embodiments of the present application further provide an electronic device, including a memory and a processor. The memory is used to store a computer program, and the processor is used to call and run the computer program from the memory, so that the device installed with the chip executes the methods in the above various possible implementation manners.

[0101] For example, the above computer device may be a terminal device with corresponding functions such as a mobile phone, a tablet computer, a personal computer, a cloud computer, etc. Please refer to Figure 4 , Figure 4 which is the structural schematic diagram of the computer provided in the embodiments of the present application.

[0102] The computer device 400 may include components such as a memory 401 and a processor 402. Those skilled in the art can understand that Figure 4 the computer device structure shown in

[0103] does not constitute a limitation to the computer device, and may include more or fewer components than shown in the figure, or combine some components, or have different component arrangements.

[0104] The memory 401 can be used to store application programs and data. The application programs stored in the memory 401 contain executable codes. The application programs can form various functional modules. The processor 402 executes various functional applications and data processing by running the application programs stored in the memory 401.

[0105] In this embodiment, the processor 402 in the computer device will load the executable codes corresponding to the processes of one or more application programs into the memory 401 according to the following instructions, and the processor 402 will run the application programs stored in the memory 401 to execute:

[0106] When any service thread outputs a print log, lock the buffer area through a thread lock;

[0107] Assign the string corresponding to the printing log to the buffer area, record the address information corresponding to the buffer area into the circular queue, and release the thread lock;

[0108] Access the address information in the circular queue through the printing thread to obtain the printing log in the buffer area;

[0109] Send the printing log to the target serial port to complete the printing task.

[0110] It can be understood that the above scenarios are only examples and do not constitute a limitation on the application scenarios of the technical solutions provided in the embodiments of the present application. The technical solutions of the present application can also be applied to other scenarios. For example, as is known to those of ordinary skill in the art, with the evolution of the system architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of the present application are equally applicable to similar technical problems.

[0111] The steps in the method of the embodiments of the present application can be adjusted, combined, and deleted according to actual needs. The modules in the devices of the embodiments of the present application can be combined, divided, and deleted according to actual needs.

[0112] In the present application, for the description of the same or similar term concepts, technical solutions, and / or application scenarios, generally only a detailed description is given when it appears for the first time. When it appears repeatedly later, for the sake of brevity, it is generally not described again. When understanding the technical solutions and other contents of the present application, for the same or similar term concepts, technical solutions, and / or application scenarios that are not described in detail later, reference can be made to the relevant detailed descriptions before.

[0113] In the present application, the descriptions of the various embodiments have their own emphases. For the parts not described or recorded in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0114] The technical features of the technical solutions of the present application can be combined arbitrarily. For the sake of brevity of description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as within the scope recorded in the present application.

[0115] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions according to the embodiments of the present application are generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center by wire (such as coaxial cable, optical fiber, digital subscriber line) or wirelessly (such as infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server, data center, etc. that includes one or more integrated available media. The available medium can be a magnetic medium (e.g., floppy disk, storage disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid state disk Solid State Disk), etc.

[0116] The above has introduced in detail a multi-threaded printing scheduling method, device, electronic device, and storage medium provided by the embodiments of the present application. Specific examples are used in this article to elaborate on the principles and implementation manners of the present application. The description of the above embodiments is only used to help understand the method and its core idea of the present application; at the same time, for those skilled in the art, according to the idea of the present application, there will be changes in the specific implementation manners and application scopes. In summary, the content of this specification should not be construed as a limitation to the present invention.

Claims

1. A multi-threaded printing scheduling method, characterized in that, Including: When any business thread outputs a print log, lock the buffer area through a thread lock; Allocate the string corresponding to the print log to the buffer area, record the address information corresponding to the buffer area in the circular queue, and release the thread lock; The step of accessing the address information in the circular queue by the print thread to obtain the print log in the buffer area includes initializing the buffer area and the circular queue, and detecting the status of the circular queue by the print thread. When it is detected that the circular queue has the address information, lock the buffer area again through the thread lock, and release the thread lock after completing the printing task; Send the print log to the target serial port to complete the printing task.

2. The multi-threaded printing scheduling method according to claim 1, wherein The step of determining that a business thread outputs a print log includes: When any business thread generates a log, obtain the print level of the log and the identifier of the associated print module; Judge whether the log meets the printing conditions according to the print level and the identifier; If so, determine that the log is a print log.

3. The multi-threaded printing scheduling method according to claim 1, wherein The step of allocating the string corresponding to the print log to the buffer area includes: Combine the original data of the print log with a preset format template to generate a complete log text; Allocate the string corresponding to the log text to the buffer area and add print parameters.

4. The multi-threaded printing scheduling method according to claim 1, wherein The step of allocating the string corresponding to the print log to the buffer area includes: Determine the length of the string corresponding to the print log; Select an idle area from multiple pre-allocated buffer pools of different specifications according to the length to cache the string to the idle area.

5. The multi-threaded printing scheduling method according to any one of claims 1-4, characterized in that The step of locking the buffer area through the thread lock includes: Obtain the label information of the current business thread or print thread; Lock the buffer area by the thread lock according to the label information.

6. The multi-threaded printing scheduling method according to any one of claims 1-4, characterized in that, The method further includes: When there are multiple address information in the circular queue, determine the priority information of each of the multiple address information; Send multiple print logs to the target serial port in sequence according to the priority information.

7. A multi-threaded printing scheduling device, characterized in that, Including: An output module for locking the buffer area through a thread lock when any business thread outputs a print log; An allocation module for allocating the string corresponding to the print log to the buffer area, recording the address information corresponding to the buffer area in the circular queue, and releasing the thread lock; An acquisition module for accessing the address information in the circular queue by the print thread to obtain the print log in the buffer area. The step of accessing the address information in the circular queue by the print thread includes initializing the buffer area and the circular queue, and detecting the status of the circular queue by the print thread. When it is detected that the circular queue has the address information, lock the buffer area again through the thread lock, and release the thread lock after completing the printing task; A sending module for sending the print log to the target serial port to complete the printing task.

8. An electronic device, characterized in that, The electronic device includes a memory and a processor. A computer program is stored in the memory. The processor executes the steps in the multi-threaded printing scheduling method according to any one of claims 1-6 by calling the computer program stored in the memory.

9. A storage medium, characterized in that, The storage medium stores a computer program, and the computer program is adapted to be loaded by a processor to execute the steps in the multi-threaded printing scheduling method according to any one of claims 1-6.

Citation Information

Patent Citations

  • Data caching device and method and electronic equipment

    CN115757305A

  • Asynchronous log processing method for naked run program of relay protection device

    CN116048914A