CN113778659A - Multithreading processing method and device - Google Patents

Multithreading processing method and device Download PDF

Info

Publication number
CN113778659A
CN113778659A CN202011165961.XA CN202011165961A CN113778659A CN 113778659 A CN113778659 A CN 113778659A CN 202011165961 A CN202011165961 A CN 202011165961A CN 113778659 A CN113778659 A CN 113778659A
Authority
CN
China
Prior art keywords
executed
class
accessor
multithreading
thread pool
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.)
Granted
Application number
CN202011165961.XA
Other languages
Chinese (zh)
Other versions
CN113778659B (en
Inventor
周奕儒
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Zhenshi Information Technology Co Ltd
Original Assignee
Beijing Jingdong Zhenshi Information 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 Beijing Jingdong Zhenshi Information Technology Co Ltd filed Critical Beijing Jingdong Zhenshi Information Technology Co Ltd
Priority to CN202011165961.XA priority Critical patent/CN113778659B/en
Publication of CN113778659A publication Critical patent/CN113778659A/en
Application granted granted Critical
Publication of CN113778659B publication Critical patent/CN113778659B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/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

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The invention discloses a multithreading processing method and device, and relates to the technical field of computers. One embodiment of the method comprises: receiving a multithreading request, wherein the multithreading request indicates one or more classes to be executed, names of one or more methods to be executed corresponding to the one or more classes to be executed and related parameters, and the related parameters comprise entries of the methods to be executed; decorating the class to be executed into a class capable of reflecting operation, and initializing a thread pool; determining a method accessor corresponding to the class of reflectable operations; obtaining an instance of the class of the reflectable operation through a reflection mechanism based on the method accessor, the instance comprising the method to be executed; and executing the method to be executed by utilizing the threads in the thread pool. The implementation method reduces the development cost, and does not need to write multiple sets of personalized multithread native implementation codes.

Description

Multithreading processing method and device
Technical Field
The present invention relates to the field of computer technologies, and in particular, to a multithread processing method and apparatus.
Background
Concurrent programming technology is an indispensable existence in the internet industry today, and multithreading is the most important and most common technical support for concurrent programming. However, in the process of implementing the present invention, the inventor finds that the use modes of multithreading are different in the industry, and in some complex business logic code environments, the mode of implementing multithreading is to manually open a thread pool. For example, when a multithreading technique is used under different code structures such as different modules, packages, classes, method paths and the like, multithreading needs to be manually started, an asynchronous mode is created by modifying each existing method, a large number of codes are rewritten to realize the multithreading technique, meanwhile, different multithreading implementation schemes need to be provided under each class of method, so that the code structure is extremely disordered and redundant, and research and development personnel also spend a large amount of time on adapting to different code structures and method contents.
Disclosure of Invention
In view of this, embodiments of the present invention provide a multithreading method and apparatus, which can reduce development cost, and for a scene requiring a multithreading technology, only a business side needs to transmit a class method set and parameters, so as to implement multithreading, and do not need to write multiple sets of personalized multithreading native implementation codes, thereby improving readability of a program.
To achieve the above object, according to an aspect of an embodiment of the present invention, there is provided a multithread processing method including:
receiving a multithreading request, wherein the multithreading request indicates one or more classes to be executed, names of one or more methods to be executed corresponding to the one or more classes to be executed and related parameters, and the related parameters comprise entries of the methods to be executed;
decorating the class to be executed into a class capable of reflecting operation, and initializing a thread pool;
determining a method accessor corresponding to the class of reflectable operations;
obtaining an instance of the class of the reflectable operation through a reflection mechanism based on the method accessor, the instance comprising the method to be executed;
and executing the method to be executed by utilizing the threads in the thread pool.
Optionally, determining a method accessor corresponding to the class of reflectable operations comprises:
determining whether the reflectable operation class exists in a cache unit, wherein the cache unit is used for storing the reflectable operation class and a corresponding method accessor;
if yes, acquiring a method memory corresponding to the type of the reflection operation in the cache unit;
if not, a method accessor corresponding to the reflectively-operable class is created, and the reflectively-operable class and the created method accessor are stored in a cache unit.
Optionally, based on the method accessor, obtaining the instance of the class of the reflectable operation by a reflection mechanism includes: based on the method memory, reflecting by a reflactasm tool to obtain an index of the method of the instance of the class of reflectably-operated; or a method for reflecting an instance of the reflectably operated class by JDK tools based on the method accessor.
Optionally, after receiving the multi-threaded processing request, the method further comprises: storing the name of the method to be executed into a method queue;
the method for executing the to-be-executed method by utilizing the threads in the thread pool comprises the following steps: and executing the method to be executed according to the sequence of the method queue by using the threads in the thread pool.
Optionally, after executing the to-be-executed method by using threads in the thread pool, the multithread processing method further includes: determining whether the thread runs abnormally; and if so, judging that the multithreading processing request fails.
Optionally, the multithreading method further includes: determining whether the thread runs a timeout; and if so, judging that the multithreading processing request fails.
Optionally, the related parameter further includes routing information;
after obtaining an instance of the reflectably-operable class through a reflection mechanism based on the method accessor, the multi-threaded processing method further comprises: and setting the context route of the thread in the thread pool according to the routing information.
To achieve the above object, according to another aspect of embodiments of the present invention, there is provided a multithread processing apparatus including:
the system comprises a receiving module, a processing module and a processing module, wherein the receiving module is used for receiving a multithread processing request which indicates one or more classes to be executed, names of one or more methods to be executed corresponding to the one or more classes to be executed and related parameters, and the related parameters comprise entries of the methods to be executed;
the decoration module is used for decorating the class to be executed into a class capable of reflecting operation and initializing a thread pool;
an accessor determination module to determine a method accessor corresponding to a class of the reflectable operation;
a reflection module, configured to obtain, through a reflection mechanism, an instance of the class of the reflectable operation based on the method accessor, where the instance includes the method to be executed;
and the method execution module is used for executing the method to be executed by utilizing the threads in the thread pool.
Optionally, the accessor determination module is further configured to: determining whether the reflectable operation class exists in a cache unit, wherein the cache unit is used for storing the reflectable operation class and a corresponding method accessor;
if yes, acquiring a method memory corresponding to the type of the reflection operation in the cache unit;
if not, a method accessor corresponding to the reflectively-operable class is created, and the reflectively-operable class and the created method accessor are stored in a cache unit.
Optionally, the reflection module is further configured to: based on the method memory, reflecting with a reflactasm tool to obtain an index of the method of the instance of the reflectably-operated class; or a method for reflecting an instance of the reflectably operated class using JDK tools based on the method accessor.
Optionally, the apparatus further includes a queue module, configured to store the name of the to-be-executed method in a method queue;
the method execution module is further configured to: and executing the method to be executed according to the sequence of the method queue by using the threads in the thread pool.
Optionally, the apparatus further includes an exception handling module, configured to determine whether the thread runs abnormally; and if so, judging that the multithreading processing request fails.
Optionally, the apparatus further includes a monitoring module, configured to determine whether the thread runs overtime; and if so, judging that the multithreading processing request fails.
Optionally, the related parameter further includes routing information;
the device also comprises a routing setting module used for setting the context routing of the thread in the thread pool according to the routing information.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided an electronic apparatus including: one or more processors; a storage device for storing one or more programs which, when executed by the one or more processors, cause the one or more processors to implement the multithreading method of an embodiment of the present invention.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided a computer-readable medium on which a computer program is stored, the program implementing the multithread processing method of an embodiment of the present invention when executed by a processor.
One embodiment of the above invention has the following advantages or benefits: decorating a class to be executed in a received multithread processing request into a class capable of reflecting operation, and initializing a thread pool, wherein the multithread processing request also comprises a name and related parameters of a method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectable operations; obtaining an instance of the class of the reflectable operation through a reflection mechanism based on the method accessor, the instance comprising the method to be executed; the method to be executed is executed by utilizing the threads in the thread pool, so that the problem that the multithreading technology is realized by manually opening the thread pool and rewriting a large batch of codes in the prior art can be solved. In the method of the embodiment of the invention, for the scene needing to use the multithreading technology, the multithreading processing can be realized only by transmitting the class method set and the parameters by the service party, and a plurality of sets of personalized multithreading native realization codes are not required to be written, so that the development cost is reduced, and the readability of the program is improved.
Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
FIG. 1 is a schematic diagram of the main flow of a multithreading method according to an embodiment of the present invention;
FIG. 2 is a schematic diagram of the main flow of a multithreading method according to an embodiment of the present invention;
FIG. 3 is a schematic diagram of the main blocks of a multi-threaded processing apparatus according to an embodiment of the present invention;
FIG. 4 is a schematic diagram of the main blocks of a multi-threaded processing apparatus of an embodiment of the present invention;
FIG. 5 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;
fig. 6 is a schematic block diagram of a computer system suitable for use in implementing a terminal device or server of an embodiment of the invention.
Detailed Description
Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
Fig. 1 is a schematic diagram of a main flow of a multithread processing method according to an embodiment of the present invention, and as shown in fig. 1, the multithread processing method includes:
step S101: receiving a multithreading request, wherein the multithreading request indicates one or more classes to be executed (class), one or more names of methods to be executed (methods) corresponding to the one or more classes to be executed and a parameter (param), and the parameter (param) comprises an entry of the method to be executed.
Step S102: decorating the class to be executed into a class capable of reflecting operation, and initializing a thread pool.
Wherein, decoration (Decorator): allowing new functionality to be added to an existing object without changing its structure. This type of design model is a structural model, which is a package that is an existing class. This schema creates a decoration class that wraps the original class and provides additional functionality while maintaining the integrity of the class method signature. In this step, the class to be executed in the multi-thread processing request is decorated into a reflectable class, so that the class has a reflectable function.
In this embodiment, initializing the thread pool includes: creating a thread pool and setting thread pool information. Specifically, the thread pool information includes: core thread number, maximum thread number, idle destroy time, thread task queue, reject policy, thread instance, etc. (thread instance is used to perform the actual task). As an example, a fixed-length thread pool may be created using the newFixedThreadPool thread pool technique, thereby controlling the maximum number of threads that can be concurrently running, and beyond threads that can wait in the thread task queue. In actual application, thread pool information can be dynamically set according to application scenes, thread pool information which may be configured in different service scenes is different, and the thread pool information can be configured according to service call quantity, asynchronous execution quantity, data quantity, heap memory and the like, and if the service quantity is large and large-batch asynchronous execution is needed, the thread pool can be configured to be larger.
Step S103: a method accessor corresponding to the class of reflectable operations is determined.
The method accessor (MethodAccess) is a method accessor in a Reflect name framework, and is used for reflecting class and simulating a real method for executing class. Specifically, the step may include:
determining whether the reflectable operation class exists in a cache unit, wherein the cache unit is used for storing the reflectable operation class and a corresponding method accessor;
if yes, acquiring a method memory corresponding to the type of the reflection operation in the cache unit;
if not, a method accessor corresponding to the reflectively-operable class is created, and the reflectively-operable class and the created method accessor are stored in a cache unit.
Step S104: based on the method accessor, obtaining an instance of the class of the reflectable operation through a reflection mechanism, the instance comprising the method to be executed.
In this embodiment, the example may include not only the method to be executed, but also other methods. As an example, the method of obtaining the instance of the reflectably operated class may be reflected by reflactasm tool or JDK tool in this step. Among them, reflextacsm is a very small Java class library, provides high-performance reflection processing through code generation, automatically provides an access class for get/set field, and the access class uses bytecode operation instead of Java reflection technology, thus being very fast. Jdk (Java Development kit) is a Java Development toolkit that provides various tools and resources required for compiling and running Java programs, including a Java compiler, a Java runtime environment, and commonly used Java class libraries. Further, a method of obtaining an instance of the reflectably operated class can be reflected using JDK techniques.
Specifically, the method comprises the following steps:
reflecting, based on the method memory, an index (index) of a method of obtaining an instance of the reflectably-operated class using a reflactasm tool; or
A method (method) to get an instance of the reflectably operated class is reflected using JDK tools based on the method accessor.
Reflect tools are slow in creating class and need to be used with a cache, but the reflection get method is quite fast, while JDK tools are fast in creating class and the reflection get method is relatively slow. In practical use, a service party can select one of the two reflection strategies according to application requirements.
Step S105: and executing the method to be executed by utilizing the threads in the thread pool.
In this embodiment, the name of the method that the service needs to use (i.e. the name of the method to be executed) can be obtained by parsing the multithread processing request sent by the service, all methods of the instance of the reflectable operation class can be obtained by reflecting the reflectable operation class (here, the logical implementation of all methods is obtained), the specific method can be determined by the name of the method to be executed, and then the method is executed by the thread pool.
In the multithreading processing method of the embodiment of the invention, the class to be executed in the received multithreading processing request is decorated into the class capable of reflecting operation, and the thread pool is initialized, wherein the multithreading processing request also comprises the name and relevant parameters of the method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectable operations; obtaining an example of the class of the reflective operation through a reflection mechanism based on the method accessor, wherein the example comprises a method to be executed; the method to be executed in the multithread processing request is executed by utilizing the threads in the thread pool, so that the problem that the multithread technology is realized by manually opening the thread pool and rewriting a large batch of codes in the prior art can be solved. In the method of the embodiment of the invention, for the scene needing to use the multithreading technology, the multithreading processing can be realized only by transmitting the class method set and the parameters by the service party, and a plurality of sets of personalized multithreading native realization codes are not required to be written, so that the development cost is reduced, and the readability of the program is improved.
Fig. 2 is a schematic diagram of a main flow of a multithread processing method according to an embodiment of the present invention, and as shown in fig. 2, the multithread processing method includes:
step S201: receiving a multithreading processing request, wherein the multithreading processing request indicates one or more classes to be executed, names of one or more methods to be executed corresponding to the one or more classes to be executed and related parameters, and the related parameters comprise entry and routing information of the methods to be executed;
step S202: decorating the class to be executed into a class capable of reflecting operation, and initializing a thread pool by adopting a decorator mode;
step S203: storing the name of the method to be executed into a method queue;
step S204: determining whether the reflectable operation class exists in a cache unit, wherein the cache unit is used for storing the reflectable operation class and a corresponding method accessor;
step S205-1: if yes, acquiring a method memory corresponding to the type of the reflection operation in the cache unit;
step S205-2: if the method accessor does not exist, creating a method accessor corresponding to the reflectable operation class, and storing the reflectable operation class and the created method accessor in a cache unit;
step S206: obtaining an instance of the class of the reflectable operation through a reflection mechanism based on the method accessor, the instance comprising the method to be executed;
step S207: setting context routing of the thread in the thread pool according to the routing information;
step S208: executing the method to be executed according to the sequence of the method queue by using the threads in the thread pool;
step S209: determining whether the thread runs abnormally;
step S210-1: if so, judging that the multithreading processing request fails;
step S210-2: if not, determining whether the thread runs overtime; if yes, judging that the multithreading processing request fails;
step S211: if not, determining whether all threads are executed completely; if not, continuously monitoring whether all threads run overtime or not;
step S212: and if all the threads are executed, judging that the multithreading processing request is successful.
For step S202, Decorator Pattern (Decorator Pattern) is one way to dynamically add more functionality to an existing object. In this embodiment, the thread pool is initialized in the decorator mode, which not only can implement creating the thread pool and setting thread information, but also can implement a thread monitoring function. Specifically, Guava's ListenableFUSE may be used to monitor all threads within a thread pool, where Guava is an open source based Java library.
In an alternative embodiment, in step S202, a cache unit may be further initialized, which includes setting a cache instance (the cache instance is used to perform a cache read-related operation), a maximum cache capacity, a cache expiration time, a cache concurrency level, and cache statistics (used to count cache-related information, including a cache hit rate, a time for loading a new value, and the like, which may be used to analyze an adjustment parameter). In practical application, the cache information may be configured according to traffic volume, execution frequency, data volume, memory, concurrency number, and the like.
For step S203, the name of the method to be executed may be stored in the FIFO queue. The FIFO queue (First Input First Output, FIFO queue) is a type of set based on a First-in First-out (FIFO) policy, and when accessing elements in the queue, the processing order of the elements is the order in which they are added to the queue. Therefore, in step S208, the methods to be executed need to be executed according to the sequence of the method queue.
For step S209, ListenableFUSE technology may be utilized to monitor the execution of all threads, and the FutureCallback callback may be utilized to formulate the success and failure logic. When an exception occurs in a thread, the determination is made that the multithreaded processing request failed. As an example, the abnormal situation can be divided into: (1) program exception including system exception (2) service exception such as memory, JVM, ERROR, etc., and self-defined service logic exception in the execution method of the service party. In an alternative embodiment, if there is an exception for a thread, the exception may be thrown and the stack printed to facilitate analysis of the cause of the exception.
For step S210-2, the thread pool in this embodiment is initialized in the decorator mode, and therefore, whether the thread is overtime can be determined in real time by the decorator self-contained function awaitTermination. If the thread is overtime, determining that the multithreading processing request fails; if some thread is not executed completely, other threads are blocked and are in a waiting state; and if all the threads are executed, outputting a response and finishing.
The multithread processing method of the embodiment of the invention cannot be continuously executed under the abnormal condition, namely the abnormal condition is higher in priority, so that the method monitors the running time of the thread under the condition that the thread has no abnormal condition.
In the multithreading processing method of the embodiment of the invention, the class to be executed in the received multithreading processing request is decorated into the class capable of reflecting operation, and the thread pool is initialized, wherein the multithreading processing request also comprises the name and relevant parameters of the method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectable operations; obtaining an instance of the class of the reflectable operation through a reflection mechanism based on the method accessor, wherein the instance comprises the method to be executed; and starting the thread in the thread pool to execute the method to be executed, so that the problem that the multithreading technology is realized by manually starting the thread pool and rewriting a large batch of codes in the prior art can be solved. In the method of the embodiment of the invention, for the scene needing to use the multithreading technology, the multithreading processing can be realized only by transmitting the class method set and the parameters by the service party, and a plurality of sets of personalized multithreading native realization codes are not required to be written, so that the development cost is reduced, and the readability of the program is improved.
Fig. 3 is a schematic diagram of main blocks of a multithreading apparatus 300 according to an embodiment of the present invention, as shown in fig. 3, the apparatus 300 including:
a receiving module 301, configured to receive a multithread processing request, where the multithread processing request indicates one or more classes to be executed, names of one or more methods to be executed corresponding to the one or more classes to be executed, and related parameters, where the related parameters include entries of the methods to be executed;
a decoration module 302, configured to decorate the class to be executed into a reflectable class, and initialize a thread pool;
an accessor determination module 303 to determine a method accessor corresponding to the class of reflectable operations;
a reflection module 304, configured to obtain, through a reflection mechanism, an instance of the class of the reflectable operation based on the method accessor, where the instance includes the method to be executed;
a method executing module 305, configured to execute the method to be executed by using the threads in the thread pool.
Optionally, the accessor determination module 303 is further configured to: determining whether the reflectable operation class exists in a cache unit, wherein the cache unit is used for storing the reflectable operation class and a corresponding method accessor;
if yes, acquiring a method memory corresponding to the type of the reflection operation in the cache unit;
if not, a method accessor corresponding to the reflectively-operable class is created, and the reflectively-operable class and the created method accessor are stored in a cache unit.
Optionally, the reflection module 304 is further configured to: based on the method memory, reflecting with a reflactasm tool to obtain an index of the method of the instance of the reflectably-operated class; or a method for reflecting an instance of the reflectably operated class using JDK tools based on the method accessor.
Optionally, the related parameter further includes routing information;
the device also comprises a routing setting module used for setting the context routing of the thread in the thread pool according to the routing information.
In the multithreading processing device of the embodiment of the invention, the class to be executed in the received multithreading processing request is decorated into the class capable of reflecting operation, and the thread pool is initialized, and the multithreading processing request also comprises a method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectable operations; obtaining an instance of the class of the reflectable operation through a reflection mechanism based on the method accessor, the instance comprising the method to be executed; and starting the thread in the thread pool to execute the method to be executed, so that the problem that the multithreading technology is realized by manually starting the thread pool and rewriting a large batch of codes in the prior art can be solved. In the method of the embodiment of the invention, for the scene needing to use the multithreading technology, the multithreading processing can be realized only by transmitting the class method set and the parameters by the service party, and a plurality of sets of personalized multithreading native realization codes are not required to be written, so that the development cost is reduced, and the readability of the program is improved.
Fig. 4 is a schematic diagram of main blocks of a multithreading apparatus 400 according to an embodiment of the present invention, where, as shown in fig. 4, the apparatus 400 includes:
a receiving module 401, configured to receive a multithread processing request, where the multithread processing request indicates one or more classes to be executed, names of one or more methods to be executed corresponding to the one or more classes to be executed, and related parameters, where the related parameters include entries of the methods to be executed;
a decoration module 402, configured to decorate the class to be executed as a reflectable class, and initialize a thread pool and initialize the thread pool by using a decorator mode;
a queue module 403, configured to store the name of the method to be executed into a method queue;
an accessor determination module 404 for determining a method accessor corresponding to the class of reflectable operations;
a reflection module 405, configured to obtain, through a reflection mechanism, an instance of the class of the reflectable operation based on the method accessor, where the instance includes the method to be executed;
a method executing module 406, configured to execute the to-be-executed method according to the sequence of the method queue by using the threads in the thread pool;
an exception handling module 407, configured to determine whether the thread runs abnormally; and if so, judging that the multithreading processing request fails.
A monitoring module 408, configured to determine whether the thread runs overtime; and if so, judging that the multithreading processing request fails.
The multithreading processing device of the embodiment of the invention decorates the class to be executed in the received multithreading processing request into the class capable of reflecting operation and initializes the thread pool, wherein the multithreading processing request also comprises the name and relevant parameters of the method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectable operations; obtaining an instance of the class of reflectably-operable through a reflection mechanism based on the method accessor; and starting the thread in the thread pool to execute the method to be executed in the multithread processing request, so that the problem that the multithread technology is realized by manually starting the thread pool and rewriting a large batch of codes in the prior art can be solved. In the method of the embodiment of the invention, for the scene needing to use the multithreading technology, the multithreading processing can be realized only by transmitting the class method set and the parameters by the service party, and a plurality of sets of personalized multithreading native realization codes are not required to be written, so that the development cost is reduced, and the readability of the program is improved.
The device can execute the method provided by the embodiment of the invention, and has the corresponding functional modules and beneficial effects of the execution method. For technical details that are not described in detail in this embodiment, reference may be made to the method provided by the embodiment of the present invention.
Fig. 5 illustrates an exemplary system architecture 500 to which the multi-threaded processing method or multi-threaded processing apparatus of embodiments of the invention may be applied.
As shown in fig. 5, the system architecture 500 may include terminal devices 501, 502, 503, a network 504, and a server 505. The network 504 serves to provide a medium for communication links between the terminal devices 501, 502, 503 and the server 505. Network 504 may include various connection types, such as wired, wireless communication links, or fiber optic cables, to name a few.
The user may use the terminal devices 501, 502, 503 to interact with a server 505 over a network 504 to receive or send messages or the like. The terminal devices 501, 502, 503 may have various communication client applications installed thereon, such as a shopping application, a web browser application, a search application, an instant messaging tool, a mailbox client, social platform software, and the like.
The terminal devices 501, 502, 503 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and the like.
The server 505 may be a server that provides various services, such as a background management server that supports shopping websites browsed by users using the terminal devices 501, 502, 503. The background management server may analyze and perform other processing on the received data such as the product information query request, and feed back a processing result (e.g., target push information and product information) to the terminal device.
It should be noted that the multithreading processing method provided by the embodiment of the present invention is generally executed by the server 505, and accordingly, the multithreading processing apparatus is generally disposed in the server 505.
It should be understood that the number of terminal devices, networks, and servers in fig. 5 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 6, a block diagram of a computer system 600 suitable for use with a terminal device implementing an embodiment of the invention is shown. The terminal device shown in fig. 6 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present invention.
As shown in fig. 6, the computer system 600 includes a Central Processing Unit (CPU)601 that can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM)602 or a program loaded from a storage section 608 into a Random Access Memory (RAM) 603. In the RAM 603, various programs and data necessary for the operation of the system 600 are also stored. The CPU 601, ROM 602, and RAM 603 are connected to each other via a bus 604. An input/output (I/O) interface 605 is also connected to bus 604.
The following components are connected to the I/O interface 605: an input portion 606 including a keyboard, a mouse, and the like; an output portion 607 including a display such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage section 608 including a hard disk and the like; and a communication section 609 including a network interface card such as a LAN card, a modem, or the like. The communication section 609 performs communication processing via a network such as the internet. The driver 610 is also connected to the I/O interface 605 as needed. A removable medium 611 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 610 as necessary, so that a computer program read out therefrom is mounted in the storage section 608 as necessary.
In particular, according to the embodiments of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method illustrated in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network through the communication section 609, and/or installed from the removable medium 611. The computer program performs the above-described functions defined in the system of the present invention when executed by the Central Processing Unit (CPU) 601.
It should be noted that the computer readable medium shown in the present invention can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present invention, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules described in the embodiments of the present invention may be implemented by software or hardware. The described modules may also be provided in a processor, which may be described as: a processor includes a sending module, an obtaining module, a determining module, and a first processing module. The names of these modules do not in some cases constitute a limitation on the unit itself, and for example, the sending module may also be described as a "module that sends a picture acquisition request to a connected server".
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to comprise:
receiving a multithreading request, wherein the multithreading request indicates one or more classes to be executed, names of one or more methods to be executed corresponding to the one or more classes to be executed and related parameters, and the related parameters comprise entries of the methods to be executed;
decorating the class to be executed into a class capable of reflecting operation, and initializing a thread pool;
determining a method accessor corresponding to the class of reflectable operations;
obtaining an instance of the reflectably-operated class through a reflection mechanism based on the method accessor;
and executing the method to be executed by utilizing the threads in the thread pool.
According to the technical scheme of the embodiment of the invention, the class to be executed in the received multithread processing request is decorated into the class capable of reflecting operation, and the thread pool is initialized, wherein the multithread processing request also comprises the name and relevant parameters of the method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectable operations; obtaining an instance of the class of reflectably-operable through a reflection mechanism based on the method accessor; and starting the thread in the thread pool to execute the method in the multithread processing request, so that the problem that the multithread technology is realized by manually starting the thread pool and rewriting a large batch of codes in the prior art can be solved. In the method of the embodiment of the invention, for the scene needing to use the multithreading technology, the multithreading processing can be realized only by transmitting the class method set and the parameters by the service party, and a plurality of sets of personalized multithreading native realization codes are not required to be written, so that the development cost is reduced, and the readability of the program is improved.
The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (10)

1. A method of multi-threaded processing, comprising:
receiving a multithreading request, wherein the multithreading request indicates one or more classes to be executed, names of one or more methods to be executed corresponding to the one or more classes to be executed and related parameters, and the related parameters comprise entries of the methods to be executed;
decorating the class to be executed into a class capable of reflecting operation, and initializing a thread pool;
determining a method accessor corresponding to the class of reflectable operations;
obtaining an instance of the class of the reflectable operation through a reflection mechanism based on the method accessor, the instance comprising the method to be executed;
and executing the method to be executed by utilizing the threads in the thread pool.
2. The method of claim 1, wherein determining a method accessor corresponding to the class of reflectable operations comprises:
determining whether the reflectable operation class exists in a cache unit, wherein the cache unit is used for storing the reflectable operation class and a corresponding method accessor;
if yes, acquiring a method memory corresponding to the type of the reflection operation in the cache unit;
if not, a method accessor corresponding to the reflectively-operable class is created, and the reflectively-operable class and the created method accessor are stored in a cache unit.
3. The method of claim 1, wherein deriving the instance of the class of reflectable operations by a reflection mechanism based on the method accessor comprises:
based on the method memory, reflecting with a reflactasm tool to obtain an index of the method of the instance of the reflectably-operated class; or
A method for reflecting an instance of the reflectably operated class using a JDK tool based on the method accessor.
4. The method of claim 1, wherein after receiving a multithreading request, the multithreading method further comprises: storing the name of the method to be executed into a method queue;
the method for executing the to-be-executed method by utilizing the threads in the thread pool comprises the following steps: and executing the method to be executed according to the sequence of the method queue by using the threads in the thread pool.
5. The method of claim 4, wherein initializing a thread pool comprises: initializing a thread pool by adopting a decorator mode so that the thread pool has a thread monitoring function;
after executing the method to be executed with threads in the thread pool, the multithread processing method further comprises:
determining whether the thread runs abnormally;
and if so, judging that the multithreading processing request fails.
6. The method of claim 5, wherein the multithreading method further comprises:
determining whether the thread runs a timeout;
and if so, judging that the multithreading processing request fails.
7. The method according to any of claims 1-6, wherein the relevant parameters further comprise routing information;
after obtaining an instance of the reflectably-operable class through a reflection mechanism based on the method accessor, the multi-threaded processing method further comprises: and setting the context route of the thread in the thread pool according to the routing information.
8. A multithread processing apparatus, comprising:
the system comprises a receiving module, a processing module and a processing module, wherein the receiving module is used for receiving a multithread processing request which indicates one or more classes to be executed, names of one or more methods to be executed corresponding to the one or more classes to be executed and related parameters, and the related parameters comprise entries of the methods to be executed;
the decoration module is used for decorating the class to be executed into a class capable of reflecting operation and initializing a thread pool;
an accessor determination module to determine a method accessor corresponding to a class of the reflectable operation;
a reflection module, configured to obtain, through a reflection mechanism, an instance of the class of the reflectable operation based on the method accessor, where the instance includes the method to be executed;
and the method execution module is used for executing the method to be executed by utilizing the threads in the thread pool.
9. An electronic device, comprising:
one or more processors;
a storage device for storing one or more programs,
when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-7.
10. A computer-readable medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-7.
CN202011165961.XA 2020-10-27 2020-10-27 Multithreading processing method and device Active CN113778659B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202011165961.XA CN113778659B (en) 2020-10-27 2020-10-27 Multithreading processing method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202011165961.XA CN113778659B (en) 2020-10-27 2020-10-27 Multithreading processing method and device

Publications (2)

Publication Number Publication Date
CN113778659A true CN113778659A (en) 2021-12-10
CN113778659B CN113778659B (en) 2024-07-16

Family

ID=78835146

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202011165961.XA Active CN113778659B (en) 2020-10-27 2020-10-27 Multithreading processing method and device

Country Status (1)

Country Link
CN (1) CN113778659B (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115981962A (en) * 2022-12-30 2023-04-18 北京基调网络股份有限公司 Thread pool monitoring method and device, electronic equipment and storage medium

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20120192004A1 (en) * 2011-01-24 2012-07-26 Piotr Findeisen Event information related to server request processing
CN108664315A (en) * 2017-03-30 2018-10-16 北京京东尚科信息技术有限公司 Method, electronic equipment and the readable storage medium storing program for executing of multithreading dynamic processing business
CN110990136A (en) * 2019-11-29 2020-04-10 北京奇艺世纪科技有限公司 Task processing method and task scheduler

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20120192004A1 (en) * 2011-01-24 2012-07-26 Piotr Findeisen Event information related to server request processing
CN108664315A (en) * 2017-03-30 2018-10-16 北京京东尚科信息技术有限公司 Method, electronic equipment and the readable storage medium storing program for executing of multithreading dynamic processing business
CN110990136A (en) * 2019-11-29 2020-04-10 北京奇艺世纪科技有限公司 Task processing method and task scheduler

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
ZHANG WEI-JUN等: "The design of microwave auto-measurement system based on the virtual instrumental technology", 《JOURNAL OF NATIONAL UNIVERSITY OF DEFENSE TECHNOLOGY》, 31 December 2005 (2005-12-31) *
黎颖智;史彩霞;刘世学;: "关于异步多线程快速提取CIMISS数据入库方法的研究与应用", 气象研究与应用, no. 01, 15 March 2018 (2018-03-15) *

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115981962A (en) * 2022-12-30 2023-04-18 北京基调网络股份有限公司 Thread pool monitoring method and device, electronic equipment and storage medium
CN115981962B (en) * 2022-12-30 2024-03-12 北京基调网络股份有限公司 Thread pool monitoring method and device, electronic equipment and storage medium

Also Published As

Publication number Publication date
CN113778659B (en) 2024-07-16

Similar Documents

Publication Publication Date Title
US9063772B2 (en) Automated caching and mirroring of immutable data in distributed virtual machines via native interface components
US10949216B2 (en) Support for third-party kernel modules on host operating systems
CN113934464A (en) Method and device for starting android application in Linux system and electronic equipment
US10810220B2 (en) Platform and software framework for data intensive applications in the cloud
CN110968340A (en) Method and device for realizing multi-version dependence isolation
US11281510B2 (en) Intelligent scaling in microservice-based distributed systems
CN114490050A (en) Data synchronization method and device
US9513661B2 (en) Calibrated timeout interval on a configuration value, shared timer value, and shared calibration factor
EP3872630A2 (en) Request processing method and apparatus, electronic device, and computer storage medium
CN113778659B (en) Multithreading processing method and device
CN113051055A (en) Task processing method and device
EP4203487A1 (en) Method and apparatus for processing multimedia resource
US20200326942A1 (en) Parameter management between programs
CN115630033A (en) Log information processing method and device, electronic equipment and medium
US20190227815A1 (en) Preemptive loading of code dependencies for improved performance
CN112181470B (en) Patch deployment method and device
CN113779451A (en) Page loading method and device
CN112860447B (en) Interaction method and system between different applications
CN113778910A (en) Data cache processing method and device
CN112379945A (en) Method, device, equipment and storage medium for running application
CN110262841B (en) Method and device for enhancing byte codes
US20240330021A1 (en) Contextual user interface component
CN113779078A (en) Cache data acquisition method and device
CN113961376A (en) Message middleware switching method and device
CN114741209A (en) Singleton processing method and device

Legal Events

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