CN113778659B - Multithreading processing method and device - Google Patents
Multithreading processing method and device Download PDFInfo
- Publication number
- CN113778659B CN113778659B CN202011165961.XA CN202011165961A CN113778659B CN 113778659 B CN113778659 B CN 113778659B CN 202011165961 A CN202011165961 A CN 202011165961A CN 113778659 B CN113778659 B CN 113778659B
- Authority
- CN
- China
- Prior art keywords
- class
- executed
- thread pool
- accessor
- multithreading
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements 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/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5027—Allocation 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/5038—Allocation 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 the following steps: 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 parameters 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 reflectably operated; 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 executing the method to be executed by using the threads in the thread pool. The implementation reduces development cost without writing multiple sets of personalized multiline Cheng Yuansheng implementation codes.
Description
Technical Field
The present invention relates to the field of computer technologies, and in particular, to a method and apparatus for multithreading.
Background
Concurrent programming technology is an indispensable existence in the internet industry today, and multithreading is the most important and commonly used technical support for concurrent programming. However, in the process of implementing the present invention, the inventor finds that the use manner of multithreading is different in the industry, and in some complex service logic code environments, most of the ways of implementing multithreading are to manually open a thread pool. If the multithreading technology is used under different code structures such as different modules, packages, classes, method paths and the like, the multithreading technology is required to be started manually, the existing method bodies are changed into asynchronous modes, a large number of codes are rewritten to realize the multithreading technology, and meanwhile, different multithreading implementation schemes are required under each class of methods, so that the code structures are extremely disordered and redundant, and research and development personnel consume a large amount of time in adapting to different code structures and method contents.
Disclosure of Invention
In view of this, the embodiments of the present invention provide a multithreading method and apparatus, which can reduce development cost, and for a scenario requiring use of a multithreading technology, only a service party needs to input a class method set and parameters, so as to implement multithreading, without writing a plurality of personalized multithreading Cheng Yuansheng implementation codes, thereby improving the readability of a program.
To achieve the above object, according to an aspect of an embodiment of the present invention, there is provided a multithreading 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 parameters 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 reflectably operated;
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 executing the method to be executed by using the threads in the thread pool.
Optionally, determining a method accessor corresponding to the class of reflectably operated comprises:
Determining whether the class of the reflectable operation exists in a cache unit, wherein the cache unit is used for storing the class of the reflectable operation and a corresponding method accessor;
if yes, acquiring a method memory corresponding to the class of the reflectable operation in the cache unit;
If not, creating a method accessor corresponding to the class of the reflectable operation, and storing the class of the reflectable operation and the created method accessor in a cache unit.
Optionally, based on the method accessor, obtaining, by a reflection mechanism, an instance of the class of reflectable operations includes: based on the method memory, reflecting, by REFLECTASM means, an index of methods of obtaining an instance of the reflectably operated class; or based on the method accessor, obtaining an instance of the reflectably operated class by JDK tool reflection.
Optionally, after receiving the multithreaded 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 using the threads in the thread pool comprises the following steps: and executing the method to be executed according to the sequence of the method queues by using the threads in the thread pool.
Optionally, after executing the method to be executed by using the threads in the thread pool, the multithreading method further includes: determining whether the thread is running abnormally; if yes, judging that the multithreading processing request fails.
Optionally, the multithreading method further includes: determining whether the thread runs overtime; if yes, judging that the multithreading processing request fails.
Optionally, the relevant parameter further includes routing information;
after deriving an instance of the reflectably operated class by a reflection mechanism based on the method accessor, the multithreaded processing method further comprises: and setting the context route of the threads in the thread pool according to the route information.
To achieve the above object, according to another aspect of an embodiment of the present invention, there is provided a multithreading apparatus including:
a receiving module, configured to receive a multithreading request, where 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, where the related parameters include parameters 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 for determining a method accessor corresponding to the class of reflectably operated;
The reflection module is used for 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 the method executing 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 class of the reflectable operation exists in a cache unit, wherein the cache unit is used for storing the class of the reflectable operation and a corresponding method accessor;
if yes, acquiring a method memory corresponding to the class of the reflectable operation in the cache unit;
If not, creating a method accessor corresponding to the class of the reflectable operation, and storing the class of the reflectable operation and the created method accessor in a cache unit.
Optionally, the reflection module is further configured to: based on the method memory, reflecting an index of methods of the instance of the reflectably operated class with REFLECTASM tools; or based on the method accessor, utilizing JDK tool reflection to obtain the example of the reflectably operated class.
Optionally, the device further comprises a queue module, configured to store the name of the method to be executed in a method queue;
the method execution module is further to: and executing the method to be executed according to the sequence of the method queues by using the threads in the thread pool.
Optionally, the apparatus further includes an exception handling module configured to determine whether the thread is running abnormally; if yes, judging that the multithreading processing request fails.
Optionally, the device further comprises a monitoring module, configured to determine whether the thread runs out of time; if yes, judging that the multithreading processing request fails.
Optionally, the relevant parameter further includes routing information;
The device also comprises a route setting module which is used for setting the context route of the thread in the thread pool according to the route information.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided an electronic device including: one or more processors; and the storage device is used for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors are enabled to realize the multi-thread processing method of the embodiment of the invention.
To achieve the above object, according to still another aspect of the embodiments of the present invention, there is provided a computer-readable medium having stored thereon a computer program which, when executed by a processor, implements a multithreading method of the embodiments of the present invention.
One embodiment of the above invention has the following advantages or benefits: decorating a class to be executed in a received multithreading processing request into a class capable of reflecting operation, and initializing a thread pool, wherein the multithreading 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 reflectably operated; 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; the thread in the thread pool is utilized to execute the method to be executed, so that the problems of manually starting the thread pool and rewriting a large amount of codes to realize the multithreading technology in the prior art can be solved. In the method of the embodiment of the invention, for the scene requiring the use of the multithreading technology, the multithreading processing can be realized only by the business side transmitting in the class method set and the parameters, and a plurality of sets of personalized multithreading Cheng Yuansheng realization codes are not required to be written, thereby reducing the development cost and improving the readability of the program.
Further effects of the above-described non-conventional alternatives are 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 of an embodiment of the invention;
FIG. 2 is a schematic diagram of the main flow of a multithreading method of an embodiment of the invention;
FIG. 3 is a schematic diagram of the major modules of a multithreaded processing apparatus in accordance with an embodiment of the invention;
FIG. 4 is a schematic diagram of the major modules of a multithreaded processing apparatus in accordance with an embodiment of the invention;
FIG. 5 is an exemplary system architecture diagram in which embodiments of the present invention may be applied;
Fig. 6 is a schematic diagram of a computer system suitable for use in implementing an embodiment of the invention.
Detailed Description
Exemplary embodiments of the present invention will now be described with reference to the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding, and are to be considered 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 the main flow of a multithreading method according to an embodiment of the invention, and as shown in FIG. 1, the multithreading method includes:
Step S101: a multi-threaded processing request is received, the multi-threaded processing request indicating one or more classes to be executed (class), names (methods) of one or more methods to be executed (class) corresponding to the one or more classes to be executed (class), and related parameters (param), wherein the related parameters include an entry for the method to be executed (class).
Step S102: and decorating the class to be executed into a class capable of reflecting operation, and initializing a thread pool.
Wherein, decorate (Decorator): allowing new functionality to be added to an existing object without changing its structure. This type of design pattern belongs to the structural model, which is a package that is an existing class. This model 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 multithreaded processing request is decorated into a class of reflectable operation so as to have 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). By way of example, a fixed-length thread pool technique may be used newFixedThreadPool to create a pool of threads, thereby controlling the maximum number of concurrent threads, with excess threads waiting on a thread task queue. In practical application, the thread pool information can be dynamically set according to application scenes, the thread pool information possibly configured by different service scenes is different, 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.
Wherein the method accessor (MethodAccess) is a method accessor in the REFLECTASM framework that functions to reflect class classes and simulate the real method of executing the class. Specifically, the step may include:
Determining whether the class of the reflectable operation exists in a cache unit, wherein the cache unit is used for storing the class of the reflectable operation and a corresponding method accessor;
if yes, acquiring a method memory corresponding to the class of the reflectable operation in the cache unit;
If not, creating a method accessor corresponding to the class of the reflectable operation, and storing the class of the reflectable operation and the created method accessor 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, wherein the instance comprises the method to be executed.
In this embodiment, not only the method to be executed but also other methods may be included in this example. By way of example, the method of obtaining an instance of the reflectably operated class may be reflected in this step by a REFLECTASM tool or JDK tool. Wherein REFLECTASM is a very small library of Java classes, providing high-performance reflection processing through code generation, automatically providing access classes for get/set fields, which use byte code operations rather than Java reflection techniques, and thus are very fast. JDK (Java Development Kit) is a Java development kit that provides the various tools and resources required to compile, run Java programs, including Java compilers, java runtime environments, and commonly used Java class libraries, among others. Furthermore, JDK techniques can be utilized to reflect the method of obtaining an instance of the reflectably operating class.
Specifically, the method comprises the following steps:
Reflecting, based on the method memory, an index (index) of a method of deriving an instance of the reflectably operated class with REFLECTASM tools; or (b)
Based on the method accessor, a method (method) of obtaining an instance of the reflectably operated class is reflected using a JDK tool.
The REFLECTASM tool is slower in creating class, and needs to be matched with a cache, but the reflection get method is quite fast, and the JDK tool is faster in creating class, and the reflection get method is relatively slow. In actual use, the business party can select one of the two reflection strategies according to application requirements.
Step S105: and executing the method to be executed by using the threads in the thread pool.
In this embodiment, the name of a method that the service side needs to use (i.e., the name of a method to be executed) may be obtained by analyzing the multithreaded processing request that it sends, all the methods (here, the logic implementation of all the methods) of the instance of the class of the reflectable operation may be obtained by reflecting the class of the reflectable operation, the specific method may be determined by the name of the method to be executed, and then the method may be executed by the thread pool.
The multithreading processing method of the embodiment of the invention decorates the class to be executed in the received multithreading processing request into the class with reflective operation and initializes the thread pool, wherein the multithreading processing request also comprises the name and related parameters of the method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectably operated; obtaining an instance of the class of reflectable operations by a reflection mechanism based on the method accessor, the instance comprising a method to be performed; the thread in the thread pool is utilized to execute the method to be executed in the multithreading processing request, so that the problems of manually starting the thread pool and rewriting a large amount of codes to realize the multithreading technology in the prior art can be solved. In the method of the embodiment of the invention, for the scene requiring the use of the multithreading technology, the multithreading processing can be realized only by the business side transmitting in the class method set and the parameters, and a plurality of sets of personalized multithreading Cheng Yuansheng realization codes are not required to be written, thereby reducing the development cost and improving the readability of the program.
FIG. 2 is a schematic diagram of the main flow of a multithreading method according to an embodiment of the invention, and as shown in FIG. 2, the multithreading method includes:
Step S201: 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 the 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 class of the reflectable operation exists in a cache unit, wherein the cache unit is used for storing the class of the reflectable operation and a corresponding method accessor;
Step S205-1: if yes, acquiring a method memory corresponding to the class of the reflectable operation in the cache unit;
Step S205-2: if the method is not available, creating a method accessor corresponding to the class of the reflectable operation, and storing the class of the reflectable operation 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, wherein the instance comprises the method to be executed;
step S207: setting the context route of the threads in the thread pool according to the route information;
Step S208: executing the method to be executed according to the sequence of the method queues by using the threads in the thread pool;
step S209: determining whether the thread is running abnormally;
Step S210-1: if yes, judging that the multithreading processing request fails;
step S210-2: if not, determining whether the thread runs overtime; if the multi-thread processing request is overtime, judging that the multi-thread processing request fails;
step S211: if the thread is not overtime, determining whether all threads are executed; if not, continuing to monitor whether all threads run overtime;
Step S212: and if all the threads are executed, judging that the multithreaded processing request is successful.
For step S202, the decorator schema (Decorator Pattern) is one way to dynamically add more functionality to an existing object. In the embodiment, the decorator mode is adopted to initialize the thread pool, so that the thread pool creation and the thread information setting can be realized, and the thread monitoring function can be realized. Specifically, the ListenableFuture of Guava, an open source-based Java library, may be used to monitor all threads in the thread pool.
In an alternative embodiment, the buffering unit may be initialized in step S202, which includes setting a buffering instance (the buffering instance is used to perform a buffering read-related operation), a buffering maximum capacity, a buffering expiration time, a buffering concurrency level, buffering statistics (information used for counting buffering related information including a hit rate of the buffering, a time of loading a new value, etc., and these metrics statistics may be used for analyzing the adjustment parameters). In practical application, the cache information can be configured according to traffic, execution frequency, data volume, memory, concurrency times and the like.
For step S203, the name of the method to be executed may be stored in the FIFO queue. Wherein the FIFO queue (First Input First Output, first-in first-out queue) is a set type based on a first-out (FIFO) policy, and when elements in the queue are accessed, 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 in the order of the method queues.
For step S209, the ListenableFuture technique may be used to monitor the execution of all threads, and FutureCallback callbacks may be used to formulate success and failure logic. When an exception occurs to a thread, the determination fails to process the request. As an example, the abnormal situation can be divided into: (1) Program exception, including memory, JVM, ERROR and other system exception (2) business exception, business side execution method in custom business logic exception. In an alternative embodiment, if there is an exception in the 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 with the decorator mode, so that whether the thread is timed out 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, other threads are blocked and are in a waiting state; and if all the threads are executed, outputting a response and ending.
The multithread processing method of the embodiment of the invention cannot be continuously executed under the abnormal condition, namely the abnormality is higher in priority, so that the method monitors the running time of the thread under the condition that the thread is not abnormal.
The multithreading processing method of the embodiment of the invention decorates the class to be executed in the received multithreading processing request into the class with reflective operation and initializes the thread pool, wherein the multithreading processing request also comprises the name and related parameters of the method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectably operated; obtaining an instance of the class of reflectable operations by a reflection mechanism based on the method accessor, the instance comprising the method to be performed; the thread in the thread pool is started to execute the method to be executed, so that the problems of manually starting the thread pool and rewriting a large amount of codes to realize the multithreading technology in the prior art can be solved. In the method of the embodiment of the invention, for the scene requiring the use of the multithreading technology, the multithreading processing can be realized only by the business side transmitting in the class method set and the parameters, and a plurality of sets of personalized multithreading Cheng Yuansheng realization codes are not required to be written, thereby reducing the development cost and improving the readability of the program.
Fig. 3 is a schematic diagram of main modules of a multithreaded processing apparatus 300 according to an embodiment of the invention, and as shown in fig. 3, the apparatus 300 includes:
A receiving module 301, configured to receive a multithreading request, where 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, where the related parameters include an entry of the methods to be executed;
A decoration module 302, configured to decorate the class to be executed into a class capable of reflecting operations, and initialize a thread pool;
an accessor determination module 303 for determining a method accessor corresponding to the class of reflectably operated;
A reflection module 304, configured to obtain, based on the method accessor, an instance of the class of reflectable operations through a reflection mechanism, where the instance includes the method to be performed;
and a method executing module 305, configured to execute the method to be executed by using threads in the thread pool.
Optionally, the accessor determination module 303 is further configured to: determining whether the class of the reflectable operation exists in a cache unit, wherein the cache unit is used for storing the class of the reflectable operation and a corresponding method accessor;
if yes, acquiring a method memory corresponding to the class of the reflectable operation in the cache unit;
If not, creating a method accessor corresponding to the class of the reflectable operation, and storing the class of the reflectable operation and the created method accessor in a cache unit.
Optionally, the reflection module 304 is further configured to: based on the method memory, reflecting an index of methods of the instance of the reflectably operated class with REFLECTASM tools; or based on the method accessor, utilizing JDK tool reflection to obtain the example of the reflectably operated class.
Optionally, the relevant parameter further includes routing information;
The device also comprises a route setting module which is used for setting the context route of the thread in the thread pool according to the route information.
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 with reflective operation, and initializes the thread pool, wherein 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 reflectably operated; 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; the thread in the thread pool is started to execute the method to be executed, so that the problems of manually starting the thread pool and rewriting a large amount of codes to realize the multithreading technology in the prior art can be solved. In the method of the embodiment of the invention, for the scene requiring the use of the multithreading technology, the multithreading processing can be realized only by the business side transmitting in the class method set and the parameters, and a plurality of sets of personalized multithreading Cheng Yuansheng realization codes are not required to be written, thereby reducing the development cost and improving the readability of the program.
Fig. 4 is a schematic diagram of the main modules of a multithreaded processing apparatus 400 according to an embodiment of the invention, and as shown in fig. 4, the apparatus 400 includes:
A receiving module 401, configured to receive a multithreading request, where 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, where the related parameters include an entry of the methods to be executed;
A decoration module 402, configured to decorate the class to be executed into a class capable of reflecting operation, and initialize a thread pool in a decorator mode;
A queue module 403, configured to store the name of the method to be executed in a method queue;
an accessor determination module 404 for determining a method accessor corresponding to the class of reflectably operated;
A reflection module 405, configured to obtain, based on the method accessor, an instance of the class of reflectable operations through a reflection mechanism, where the instance includes the method to be performed;
a method execution module 406, configured to execute the method to be executed according to the sequence of the method queue by using threads in the thread pool;
An exception handling module 407 for determining whether the thread is running abnormally; if yes, judging that the multithreading processing request fails.
A monitor module 408 for determining if the thread is running over time; if yes, 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 with reflective operation, and initializes a thread pool, wherein the multithreading processing request also comprises the name and related parameters of the method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectably operated; obtaining an instance of the reflectably operated class through a reflection mechanism based on the method accessor; the thread in the thread pool is started to execute the method to be executed in the multithreading processing request, so that the problems of manually starting the thread pool and rewriting a large amount of codes to realize the multithreading technology in the prior art can be solved. In the method of the embodiment of the invention, for the scene requiring the use of the multithreading technology, the multithreading processing can be realized only by the business side transmitting in the class method set and the parameters, and a plurality of sets of personalized multithreading Cheng Yuansheng realization codes are not required to be written, thereby reducing the development cost and improving the readability of the program.
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. Technical details not described in detail in this embodiment may be found in the methods provided in the embodiments of the present invention.
Fig. 5 illustrates an exemplary system architecture 500 for a multi-threaded processing method or multi-threaded processing apparatus in which embodiments of the present 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 is used as a medium to provide communication links between the terminal devices 501, 502, 503 and the server 505. The network 504 may include various connection types, such as wired, wireless communication links, or fiber optic cables, among others.
A user may interact with the server 505 via the network 504 using the terminal devices 501, 502, 503 to receive or send messages or the like. Various communication client applications, such as shopping class applications, web browser applications, search class applications, instant messaging tools, mailbox clients, social platform software, etc., may be installed on the terminal devices 501, 502, 503.
The terminal devices 501, 502, 503 may be a variety of electronic devices having a display screen and supporting web browsing, including but not limited to smartphones, tablets, laptop and desktop computers, and the like.
The server 505 may be a server providing various services, such as a background management server providing support for shopping-type websites browsed by the user using the terminal devices 501, 502, 503. The background management server can analyze and other processing on the received data such as the product information inquiry request and the like, and feed back processing results (such as target push information and product information) to the terminal equipment.
It should be noted that the multithreading method according to the embodiment of the present invention is generally executed by the server 505, and accordingly, the multithreading device 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, there is illustrated a schematic diagram of a computer system 600 suitable for use in implementing an embodiment of the present invention. The terminal device shown in fig. 6 is only an example, and should not impose any limitation on the functions and the scope of use of the embodiment of the present invention.
As shown in fig. 6, the computer system 600 includes a Central Processing Unit (CPU) 601, which 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 required for the operation of the system 600 are also stored. The CPU 601, ROM 602, and RAM 603 are connected to each other through 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, mouse, etc.; an output portion 607 including a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, a speaker, and the like; 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 drive 610 is also connected to the I/O interface 605 as needed. Removable media 611 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is installed as needed on drive 610 so that a computer program read therefrom is installed as needed into storage section 608.
In particular, according to embodiments of the present disclosure, the processes described above with reference to 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 shown in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network through the communication portion 609, and/or installed from the removable medium 611. The above-described functions defined in the system of the present invention are performed when the computer program is executed by a Central Processing Unit (CPU) 601.
The computer readable medium shown in the present invention may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any 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 context of this document, 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, the computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with the computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. 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 flowcharts 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 involved in the embodiments of the present invention may be implemented in software or in hardware. The described modules may also be provided in a processor, for example, 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 constitute a limitation on the unit itself in some cases, and for example, the transmitting module may also be described as "a module that transmits 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 present alone without being fitted into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to include:
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 parameters 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 reflectably operated;
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 using the threads in the thread pool.
The technical scheme 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 related parameters of the method to be executed corresponding to the class to be executed; determining a method accessor corresponding to the class of reflectably operated; obtaining an instance of the reflectably operated class through a reflection mechanism based on the method accessor; the method for starting the threads in the thread pool to execute the multithreading processing request can solve the problems of manually starting the thread pool and rewriting a large amount of codes to realize the multithreading technology in the prior art. In the method of the embodiment of the invention, for the scene requiring the use of the multithreading technology, the multithreading processing can be realized only by the business side transmitting in the class method set and the parameters, and a plurality of sets of personalized multithreading Cheng Yuansheng realization codes are not required to be written, thereby reducing the development cost and improving the readability of the program.
The above embodiments do not limit the scope of the present invention. It will be apparent to those skilled in the art that various modifications, combinations, sub-combinations and alternatives can occur depending upon design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of the present invention.
Claims (9)
1. A method of multithreading comprising:
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 parameters of the methods to be executed, a class method set and parameters transmitted by a service party;
Decorating the class to be executed into a class capable of reflecting operation, and initializing a thread pool; wherein the decoration means allows adding new functions to an existing object without changing its structure; initializing the thread pool includes: creating a thread pool and setting thread pool information, and initializing the thread pool by adopting a decorator mode;
Determining a method accessor corresponding to the class of reflectably operated, comprising: determining whether the class of the reflectable operation exists in a cache unit, wherein the cache unit is used for storing the class of the reflectable operation and a corresponding method accessor; if yes, acquiring a method memory corresponding to the class of the reflectable operation in the cache unit; if the method is not available, creating a method accessor corresponding to the class of the reflectable operation, and storing the class of the reflectable operation and the created method accessor in a cache unit;
Based on the method accessor, obtaining all methods of the instance of the reflectably operated class through a reflection mechanism, determining the method to be executed according to the name, wherein the instance comprises the method to be executed;
and executing the method to be executed by using the threads in the thread pool.
2. The method of claim 1, wherein all methods for deriving instances of the reflectably operated class by a reflection mechanism based on the method accessor comprise:
Based on the method memory, reflecting an index of methods of the instance of the reflectably operated class with REFLECTASM tools; or (b)
Based on the method accessor, a method of obtaining an instance of the reflectably operated class using JDK tool reflection.
3. The method of claim 1, wherein after receiving a multithreaded request, the multithreaded 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 using the threads in the thread pool comprises the following steps: and executing the method to be executed according to the sequence of the method queues by using the threads in the thread pool.
4. The method of claim 3, wherein initializing the 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 by using the threads in the thread pool, the multithreading method further includes:
determining whether the thread is running abnormally;
If yes, judging that the multithreading processing request fails.
5. The method of claim 4, wherein the multi-threaded processing method further comprises:
determining whether the thread runs overtime;
If yes, judging that the multithreading processing request fails.
6. The method according to any of claims 1-5, wherein the relevant parameters further comprise routing information;
after deriving an instance of the reflectably operated class by a reflection mechanism based on the method accessor, the multithreaded processing method further comprises: and setting the context route of the threads in the thread pool according to the route information.
7. A multi-threaded processing apparatus, comprising:
a receiving module, configured to receive a multithreading request, where 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, where the related parameters include parameters of the methods to be executed, and a set of class methods and parameters are input by a service party;
The decoration module is used for decorating the class to be executed into a class capable of reflecting operation and initializing a thread pool; wherein the decoration means allows adding new functions to an existing object without changing its structure; initializing the thread pool includes: creating a thread pool and setting thread pool information, and initializing the thread pool by adopting a decorator mode;
An accessor determination module for determining a method accessor corresponding to the class of reflectably operated, comprising: determining whether the class of the reflectable operation exists in a cache unit, wherein the cache unit is used for storing the class of the reflectable operation and a corresponding method accessor; if yes, acquiring a method memory corresponding to the class of the reflectable operation in the cache unit; if the method is not available, creating a method accessor corresponding to the class of the reflectable operation, and storing the class of the reflectable operation and the created method accessor in a cache unit;
The reflection module is used for obtaining all methods of the instance of the class of the reflectable operation through a reflection mechanism based on the method accessor, determining the method to be executed according to the name, wherein the instance comprises the method to be executed;
and the method executing module is used for executing the method to be executed by utilizing the threads in the thread pool.
8. An electronic device, comprising:
One or more processors;
storage means for storing one or more programs,
When executed by the one or more processors, causes the one or more processors to implement the method of any of claims 1-6.
9. A computer readable medium, on which a computer program is stored, characterized in that the program, when being executed by a processor, implements the method according to any of claims 1-6.
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 CN113778659A (en) | 2021-12-10 |
| CN113778659B true 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) |
Families Citing this family (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN115981962B (en) * | 2022-12-30 | 2024-03-12 | 北京基调网络股份有限公司 | Thread pool monitoring method and device, electronic equipment and storage medium |
Citations (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN110990136A (en) * | 2019-11-29 | 2020-04-10 | 北京奇艺世纪科技有限公司 | Task processing method and task scheduler |
Family Cites Families (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US8769505B2 (en) * | 2011-01-24 | 2014-07-01 | Hewlett-Packard Development Company, L.P. | 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 |
-
2020
- 2020-10-27 CN CN202011165961.XA patent/CN113778659B/en active Active
Patent Citations (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN110990136A (en) * | 2019-11-29 | 2020-04-10 | 北京奇艺世纪科技有限公司 | Task processing method and task scheduler |
Also Published As
| Publication number | Publication date |
|---|---|
| CN113778659A (en) | 2021-12-10 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US8321865B2 (en) | Processing of streaming data with a keyed delay | |
| US9063772B2 (en) | Automated caching and mirroring of immutable data in distributed virtual machines via native interface components | |
| US8874638B2 (en) | Interactive analytics processing | |
| US10949216B2 (en) | Support for third-party kernel modules on host operating systems | |
| US11281510B2 (en) | Intelligent scaling in microservice-based distributed systems | |
| US10282208B2 (en) | Cognitive thread management in a multi-threading application server environment | |
| CN113127057B (en) | Method and device for parallel execution of multiple tasks | |
| CN114490050A (en) | Data synchronization method and device | |
| CN111290842A (en) | Task execution method and device | |
| CN109960212B (en) | Task sending method and device | |
| CN117076096A (en) | Task flow execution method and device, computer readable medium and electronic equipment | |
| US11249760B2 (en) | Parameter management between programs | |
| US9513661B2 (en) | Calibrated timeout interval on a configuration value, shared timer value, and shared calibration factor | |
| CN110162410A (en) | A kind of message treatment method and device | |
| CN113723893A (en) | Method and apparatus for processing an order | |
| US20130304754A1 (en) | Self-Parsing XML Documents to Improve XML Processing | |
| CN113778659B (en) | Multithreading processing method and device | |
| CN115525411A (en) | Method, device, electronic equipment and computer readable medium for processing service request | |
| CN115794262A (en) | Task processing method, device, equipment, storage medium and program product | |
| US12254355B2 (en) | Method, electronic device, and computer program product for task scheduling | |
| CN113779451B (en) | A page loading method and device | |
| CN113672671A (en) | Method and device for realizing data processing | |
| CN116302549A (en) | Hardware resource acquisition method, device, equipment and medium of AI video accelerator | |
| CN113821519B (en) | Data processing method and domain drive design architecture | |
| CN115599572A (en) | Data acquisition method and device, electronic equipment and computer readable medium |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| GR01 | Patent grant | ||
| GR01 | Patent grant |