WO2018121696A1 - 用于多线程处理的方法及装置、服务器 - Google Patents
用于多线程处理的方法及装置、服务器 Download PDFInfo
- Publication number
- WO2018121696A1 WO2018121696A1 PCT/CN2017/119561 CN2017119561W WO2018121696A1 WO 2018121696 A1 WO2018121696 A1 WO 2018121696A1 CN 2017119561 W CN2017119561 W CN 2017119561W WO 2018121696 A1 WO2018121696 A1 WO 2018121696A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- storage space
- thread
- sub
- unit
- threads
- 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.)
- Ceased
Links
Images
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/48—Program initiating; Program switching, e.g. by interrupt
- G06F9/4806—Task transfer initiation or dispatching
- G06F9/4843—Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
- G06F9/4881—Scheduling strategies for dispatcher, e.g. round robin, multi-level priority queues
-
- 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/5011—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
- G06F9/5016—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
-
- 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/5011—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
- G06F9/5022—Mechanisms to release resources
-
- 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
Definitions
- the present invention relates to the field of data processing technologies, and in particular, to a method and apparatus for multi-thread processing and a server.
- the two programs on the network exchange data through a two-way communication connection.
- One end of the connection is called a socket.
- Socket communication is widely used in various fields of software systems, and usually its communication data needs to be customized data format.
- the current communication method mostly adopts a one-to-one manner in which multiple threads are processed by multiple threads. When there are a large number (for example, 10,000) of connection requests, it may take 10,000 threads to process, the resources are extremely expensive, and blocking occurs. Once the blocking occurs, a large number of threads frequently switch contexts, which makes the time consumption too large.
- Embodiments of the present invention provide a method for multi-thread processing, which solves the problem caused by multi-thread processing of a one-to-one connection of multiple connections.
- a brief summary is given below. This generalization is not a general comment, nor is it intended to identify key/critical constituent elements or to describe the scope of protection of these embodiments. Its sole purpose is to present some concepts in a simplified form as a prelude to the following detailed description.
- the method for multi-threaded processing includes: creating a thread pool comprising a plurality of child threads; establishing one or more remote connections; after the one or more remotely connected task queues arrive, The plurality of sub-threads execute tasks in parallel.
- the plurality of sub-threads execute tasks in parallel, including: each sub-thread separately extracts mutually different tasks from the task queue, and respectively executes the fetched tasks.
- data received by the plurality of sub-threads when the task is executed is cached in the same storage space, the storage space being composed of one or more unit storage spaces.
- part or all of the storage space is released when a release condition is satisfied, the release condition including one or more of the following: a) within the storage space, free storage space The length reaches the first threshold; b) in the storage space, there is a free storage space in which the idle time reaches the second threshold; c) the length of the free storage space in which the idle time reaches the second threshold reaches the third threshold in the storage space. .
- part or all of the storage space is released when a release condition is satisfied, the release condition including one or more of the following: a) within the storage space, free storage space The length reaches the first threshold; b) in the storage space, there is a free storage space in which the idle time reaches the second threshold; c) the length of the free storage space in which the idle time reaches the second threshold reaches the third threshold in the storage space.
- the first threshold and the third threshold are integer multiples of the unit storage space length.
- the release condition when the release condition is satisfied, all free storage space is released, or the free storage space whose idle time reaches the fourth threshold is released, or the set length of free storage space is released;
- the length of free storage space is an integer multiple of the unit storage space length.
- the method for multi-thread processing further includes deleting or adding a sub-thread according to the number of tasks, including: deleting one or more sub-threads when the number of communication tasks is less than a first threshold When the number of communication tasks is greater than the second threshold, one or more new child threads are created.
- Another object of embodiments of the present invention is to provide an apparatus for multi-threaded processing.
- the apparatus for multi-thread processing includes: a first unit for creating a thread pool including a plurality of child threads; a second unit for establishing one or more remote connections; And a unit, configured to manage the multiple sub-threads to perform tasks in parallel after the one or more remotely connected task queues arrive.
- the third unit includes: a task retrieving unit, configured to schedule each sub-thread to respectively extract different tasks from the task queue; and, a task execution unit, configured to schedule each The child thread executes the extracted task separately.
- the apparatus for multi-thread processing further includes a storage unit that provides a storage space for buffering data received when the plurality of sub-threads perform a task, the storage space being one or Multiple unit storage space components.
- part or all of the storage space is released when a release condition is satisfied, the release condition including one or more of the following: a) within the storage space, free storage space The length reaches the first threshold; b) in the storage space, there is a free storage space in which the idle time reaches the second threshold; c) the length of the free storage space in which the idle time reaches the second threshold reaches the third threshold in the storage space. .
- part or all of the storage space is released when a release condition is satisfied, the release condition including one or more of the following: a) within the storage space, free storage space The length reaches the first threshold; b) in the storage space, there is a free storage space in which the idle time reaches the second threshold; c) the length of the free storage space in which the idle time reaches the second threshold reaches the third threshold in the storage space.
- the first threshold and the third threshold are integer multiples of the unit storage space length.
- the release condition when the release condition is satisfied, all free storage space is released, or the free storage space whose idle time reaches the fourth threshold is released, or the set length of free storage space is released;
- the length of free storage space is an integer multiple of the unit storage space length.
- the first unit is further configured to delete or add a sub-thread according to the number of tasks, including: deleting one or more sub-threads when the number of communication tasks is less than a first threshold; When the number is greater than the second threshold, one or more new child threads are created.
- the processing method of the thread pool adopts the thread centralized parallel processing instead of the one-to-one mode, so that the problem of executing multiple threads in the processor unit can be solved, and the processor can be significantly reduced.
- the idle time of the unit increases the throughput of the processor unit.
- Figure 1 is a schematic flow chart for multi-thread processing
- Figure 3 is a schematic flow chart of a sub-thread processing
- FIG. 4 is a schematic diagram of an apparatus for multi-thread processing.
- relational terms such as first and second are used merely to distinguish one entity or operation from another entity or operation, and do not require or imply any actual relationship between the entities or operations or order.
- the terms “comprises” or “comprising” or “comprising” or any other variations are intended to encompass a non-exclusive inclusion, such that a process, method, or device that includes a plurality of elements includes not only those elements but also other items not specifically listed. Elements, or elements that are inherent to such a process, method, or device. An element that is defined by the phrase “comprising a " does not exclude the presence of additional equivalent elements in the process, method, or device that comprises the element.
- Figure 1 shows an illustrative flow embodiment for multi-threaded processing.
- the method for multi-thread processing includes: creating a thread pool including a plurality of child threads (step S11), the child thread waiting for a task (step S12), establishing one or more remote connections (step S13), After the arrival of one or more remotely connected task queues, the plurality of child threads execute the tasks in parallel (step S14).
- the thread pool handles the task in a thread-centric parallel processing manner instead of the one-to-one mode. Therefore, it can solve the problem of multiple thread execution in the processor unit, which can significantly reduce the idle time of the processor unit and increase the processor unit.
- the throughput capacity is a thread-centric parallel processing manner instead of the one-to-one mode.
- each sub-thread separately takes different tasks from the task queue and executes the fetched tasks separately. More specifically, each sub-thread simultaneously takes a task from the task queue, and the tasks fetched by each sub-thread are different from each other, and then each sub-thread executes its fetched task separately.
- the method for multi-threaded processing includes: creating a thread pool comprising a plurality of child threads; establishing one or more remote connections; after one or more remotely connected task queues arrive, A plurality of child threads execute tasks in parallel; data received by the plurality of child threads when executed is cached in the same storage space.
- the method for multi-threaded processing includes: creating a thread pool comprising a plurality of child threads; establishing one or more remote connections; after one or more remotely connected task queues arrive, Multiple child threads execute tasks in parallel.
- the data received by the plurality of sub-threads when the task is executed is cached in the same storage space, and part or all of the storage space is released when the release condition is satisfied.
- the memory space is released when the release condition is satisfied, so that it is not necessary to repeatedly apply for and release the memory space, thereby effectively improving the system operation efficiency.
- the dynamic control of the memory space length can be realized, taking into consideration both the efficiency and the ease of use, reducing the number of memory fragments that cannot be used by the system, and improving the number of memory fragments that are not available to the system. System operation efficiency.
- the release condition includes one or more of the following:
- the method for multi-threaded processing includes: creating a thread pool comprising a plurality of child threads; establishing one or more remote connections; after one or more remotely connected task queues arrive, The plurality of sub-threads execute the tasks in parallel; the data received by the plurality of sub-threads when the task is executed is cached to be in the same storage space, part or all of the storage space is released when the release condition is satisfied; when the release condition is satisfied, all idle The storage space is released, or the free storage space whose idle time reaches the fourth threshold is released, or the set length of free storage space is released.
- the set length of the free storage space is released, including: releasing the free storage space by a preset fixed length, or obtaining a length value according to a preset rule and releasing the idle storage according to the length value. space.
- release mode of the free storage space needs to be flexibly set according to system design requirements or application scenario requirements, and cannot be implemented by common common sense or conventional technical means.
- the storage space is composed of one or more unit storage spaces.
- the thread pool uses memory pool technology for memory application and management, thereby being able to allocate a plurality of unit storage spaces of the same size, greatly speeding up the memory allocation/release process, and effectively improving system operation efficiency.
- the optional release condition includes one or more of the following cases: a) in the storage space, the length of the free storage space reaches a first threshold; b In the storage space, there is a free storage space in which the idle time reaches the second threshold; c) in the storage space, the length of the idle storage space whose idle time reaches the second threshold reaches a third threshold.
- the first threshold and/or the third threshold are integer multiples of the unit storage space length.
- the storage space is composed of one or more unit storage spaces
- the release condition when the release condition is satisfied, all the free storage spaces are released, or the free storage space whose idle time reaches the fourth threshold is released, or the set length is Free storage is released.
- the length of the free storage space that is released is an integer multiple of the length of the storage space.
- the storage space can be expanded by merging the adjacent free storage spaces in the system.
- An alternative is to merge two adjacent free storage spaces in the system to extend the storage space for the thread pool.
- the length of the expandable memory space is 2 times or more, thereby realizing dynamic control of the length of the memory space, effectively reducing the amount of system memory fragmentation.
- Combining the adjacent free memory space can not only realize the dynamic control of the memory block length, but also the requirements of ease of use and merge efficiency, and improve the system operation efficiency.
- the method may further include: deleting or adding the child thread according to the number of tasks, including: deleting one or more children when the number of tasks is less than a fourth threshold Thread; when the number of tasks is greater than the fifth threshold, one or more new child threads are created.
- FIG. 2 illustrates another illustrative flow embodiment for multi-threading processing.
- the main thread will start a socket service (step S21), listen to the designated port (step S22) and receive a new connection request from the client (S23). If a new connection request is received, a new connection is established and a new connection is added to the thread pool (S24), and data communication with the client is handled by the thread pool (S25). If the new connection request is not received, the port continues to be listened to.
- Figure 3 illustrates an illustrative flow embodiment of a sub-threaded process.
- Child threads (quantity configurable) will be run in the thread pool, and data transmission and reception tasks for all client connections will be performed in parallel.
- the child thread waits to receive data (S31), and when it receives the data, it caches the data (S32) and determines whether the cached data is a complete data packet (S33). If the full data packet is cached, the data packet is taken out and parsed ( S34), then processing the data (S35); otherwise, continuing to receive the data. If no data is received, the child thread continues to wait to receive data.
- Figure 4 shows an illustrative device embodiment for multi-threading processing.
- the apparatus for multi-thread processing includes a first unit S01, a second unit S02, and a third unit S03.
- the first unit S01 is configured to create a thread pool including a plurality of child threads
- the second unit S02 is configured to establish one or more remote connections
- the third unit S03 is configured to: after the one or more remotely connected task queues arrive, The plurality of child threads are managed to execute tasks in parallel.
- the plurality of sub-threads simultaneously execute one task from the task queue and execute in parallel.
- the apparatus for multi-thread processing further includes a storage unit that provides a storage space for buffering data received by the plurality of child threads when performing a task.
- the storage space is composed of one or more unit storage spaces.
- some or all of the storage space is released when a release condition is satisfied, the release condition including one or more of the following:
- the length of the free storage space reaches a first threshold; b) in the storage space, there is a free storage space in which the idle time reaches a second threshold; c) the idle time reaches the first in the storage space The length of the two threshold free storage space reaches a third threshold.
- the storage space is composed of one or more unit storage spaces
- part or all of the storage space is released when a release condition is satisfied, the release condition including one of the following cases Or several:
- the length of the free storage space reaches a first threshold; b) in the storage space, there is a free storage space in which the idle time reaches a second threshold; c) the idle time reaches the first in the storage space
- the length of the two threshold free storage space reaches a third threshold.
- the first threshold and the third threshold are integer multiples of the unit storage space length.
- the release condition when the release condition is satisfied, all of the free storage space is released, or the free storage space whose idle time reaches the fourth threshold is released, or the set length of free storage space is released.
- the set length of the free storage space is released, including: releasing the free storage space by a preset fixed length, or obtaining a length value according to a preset rule and releasing the idle storage according to the length value. space.
- release mode of the free storage space needs to be flexibly set according to system design requirements or application scenario requirements, and cannot be implemented by common common sense or conventional technical means.
- the storage space is composed of one or more unit storage spaces
- the release condition when the release condition is satisfied, all the free storage spaces are released, or the free storage space whose idle time reaches the fourth threshold is released.
- the set length of free storage space is released.
- the length of the free storage space that is released is an integer multiple of the length of the storage space.
- the first unit S01 is further configured to delete or add a sub-thread according to the number of tasks, including: deleting one or more sub-threads when the number of communication tasks is less than a first threshold; when the communication task When the number is greater than the second threshold, one or more new child threads are created.
- a server comprising the apparatus for multi-threading processing as described in any of the above embodiments.
- non-transitory computer readable storage medium comprising instructions, such as a memory comprising instructions executable by a processor to perform the methods described above.
- the above non-transitory computer readable storage medium may be a read only memory (ROM), a random access memory (RAM), a magnetic tape, an optical storage device, or the like.
- the disclosed methods, products may be implemented in other manners.
- the device embodiments described above are merely illustrative.
- the division of the unit is only a logical function division.
- there may be another division manner for example, multiple units or components may be combined or Can be integrated into another system, or some features can be ignored or not executed.
- the mutual coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection through some interface, device or unit, and may be in an electrical, mechanical or other form.
- each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
- Debugging And Monitoring (AREA)
Abstract
一种用于多线程处理的方法,包括:创建包括多个子线程的线程池(S11);建立一个或多个远程连接(S13);所述一个或多个远程连接的任务队列到达后,所述多个子线程并行执行任务(S14)。线程池对于任务的处理方式采用线程集中并行处理的方式,而不是一对一模式,因此能够解决处理器单元内多个线程执行的问题,可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力。还公开了一种用于多线程处理的装置及一种服务器。
Description
本发明涉及数据处理技术领域,特别涉及一种用于多线程处理的方法及装置和一种服务器。
网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个套接字(socket)。socket通信广泛应用于软件系统的各个领域,通常其通信数据需自定义数据格式。目前的通讯方式多采用多线程处理多个连接的一对一方式。当有大量(例如10000个)连接请求时,可能就需要有10000个线程来处理,资源消耗极大,且会产生阻塞。而一旦阻塞产生就会带来大量的线程频繁地进行上下文切换,从而使得时间消耗过大。
发明内容
本发明实施例提供了一种用于多线程处理的方法,解决多线程处理多个连接的一对一方式所带来的问题。为了对披露的实施例的一些方面有一个基本的理解,下面给出了简单的概括。该概括部分不是泛泛评述,也不是要确定关键/重要组成元素或描绘这些实施例的保护范围。其唯一目的是用简单的形式呈现一些概念,以此作为后面的详细说明的序言。
本发明实施例的一个目的是提供了一种用于多线程处理的方法。
在一些示意性实施例中,所述用于多线程处理的方法包括:创建包括多个子线程的线程池;建立一个或多个远程连接;所述一个或多个远程连接的任务队列到达后,所述多个子线程并行执行任务。
在一些示意性实施例中,所述多个子线程并行执行任务,包括:各子线程分别从所述任务队列中取出互不相同的任务,并分别执行所取出的任务。
在一些示意性实施例中,所述多个子线程执行任务时所接收的数据被缓存到在同一存储空间,所述存储空间由一个或多个单位存储空间组成。
在一些示意性实施例中,所述存储空间的部分或全部在满足释放条件时被释放,所述释放条件包括以下情况的一种或几种:a)所述存储空间内,空闲存储空间的长度达到第一阈值;b)所述存储空间内,存在空闲时间达到第二阈值的空闲存储空间;c)所述存储空间内,空闲时间达到第二阈值的空闲存储空间的长度达到第三阈值。
在一些示意性实施例中,所述存储空间的部分或全部在满足释放条件时被释放,所述释放条件包括以下情况的一种或几种:a)所述存储空间内,空闲存储空间的长度达到第一阈值;b)所述存储空间内,存在空闲时间达到第二阈值的空闲存储空间;c)所述存储空间内,空闲时间达到第二阈值的空闲存储空间的长度达到第三阈值;其中,第一阈值和第三阈值为单位存储空间长度的整数倍。
在一些示意性实施例中,满足释放条件时,全部空闲存储空间被释放,或,空闲时间达到第四阈值的空闲存储空间被释放,或,设定长度的空闲存储空间被释放;其中,被释放空闲存储空间的长度为单位存储空间长度的整数倍。
在另一些示意性实施例中,所述用于多线程处理的方法还包括:根据任务的数量删除或增加子线程,包括:当通信任务的数量小于第一阈值时,删除一个或多个子线程;当通信任务的数量大于第二阈值时,创建一个或多个新的子线程。
本发明实施例的另一个目的是提供一种用于多线程处理的装置。
在一些示意性实施例中,所述用于多线程处理的装置包括:第一单元,用于创建包括多个子线程的线程池;第二单元,用于建立一个或多个远程连接;第三单元,用于在所述一个或多个远程连接的任务队列到达后,管理所述多个子线程并行执行任务。
在一些示意性实施例中,所述第三单元包括:任务调取单元,用于调度各子线程分别从所述任务队列中取出互不相同的任务;和,任务执行单元,用于调度各子线程分别执行所取出的任务。
在另一些示意性实施例中,所述用于多线程处理的装置还包括提供存 储空间的存储单元,用于缓存所述多个子线程执行任务时所接收的数据,所述存储空间由一个或多个单位存储空间组成。
在一些示意性实施例中,所述存储空间的部分或全部在满足释放条件时被释放,所述释放条件包括以下情况的一种或几种:a)所述存储空间内,空闲存储空间的长度达到第一阈值;b)所述存储空间内,存在空闲时间达到第二阈值的空闲存储空间;c)所述存储空间内,空闲时间达到第二阈值的空闲存储空间的长度达到第三阈值。
在一些示意性实施例中,所述存储空间的部分或全部在满足释放条件时被释放,所述释放条件包括以下情况的一种或几种:a)所述存储空间内,空闲存储空间的长度达到第一阈值;b)所述存储空间内,存在空闲时间达到第二阈值的空闲存储空间;c)所述存储空间内,空闲时间达到第二阈值的空闲存储空间的长度达到第三阈值;其中,第一阈值和第三阈值为单位存储空间长度的整数倍。
在一些示意性实施例中,满足释放条件时,全部空闲存储空间被释放,或,空闲时间达到第四阈值的空闲存储空间被释放,或,设定长度的空闲存储空间被释放;其中,被释放空闲存储空间的长度为单位存储空间长度的整数倍。
在一些示意性实施例中,所述第一单元还用于根据任务的数量删除或增加子线程,包括:当通信任务的数量小于第一阈值时,删除一个或多个子线程;当通信任务的数量大于第二阈值时,创建一个或多个新的子线程。
本发明实施例的再一个目的是提供一种服务器,该服务器包括上述任一项实施例所述的用于多线程处理的装置。
本发明实施例提供的技术方案,线程池对于任务的处理方式采用线程集中并行处理的方式,而不是一对一模式,因此能够解决处理器单元内多个线程执行的问题,可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力。
应当理解的是,以上的一般描述和后文的细节描述仅是示例性和解释性的,并不能限制本发明。
此处的附图被并入说明书中并构成本说明书的一部分,示出了符合本发明的实施例,并与说明书一起用于解释本发明的原理。
图1是一个用于多线程处理的示意性流程图;
图2是另一个用于多线程处理的示意性流程图;
图3是一个子线程处理的示意性流程图;
图4是一个用于多线程处理的装置示意图。
以下描述和附图充分地示出本发明的具体实施方案,以使本领域的技术人员能够实践它们。其他实施方案可以包括结构的、逻辑的、电气的、过程的以及其他的改变。实施例仅代表可能的变化。除非明确要求,否则单独的部件和功能是可选的,并且操作的顺序可以变化。一些实施方案的部分和特征可以被包括在或替换其他实施方案的部分和特征。本发明的实施方案的范围包括权利要求书的整个范围,以及权利要求书的所有可获得的等同物。在本文中,各实施方案可以被单独地或总地用术语“发明”来表示,这仅仅是为了方便,并且如果事实上公开了超过一个的发明,不是要自动地限制该应用的范围为任何单个发明或发明构思。本文中,诸如第一和第二等之类的关系术语仅仅用于将一个实体或者操作与另一个实体或操作区分开来,而不要求或者暗示这些实体或操作之间存在任何实际的关系或者顺序。而且,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法或者设备所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括所述要素的过程、方法或者设备中还存在另外的相同要素。本文中各个实施例采用递进的方式描述,每个实施例重点说明的都是与其他实施例的不同之处,各个实施例之间相同相似部分互相参见即可。对于实施例公开的方法、产品等而言,由于其与实施例公开的方法部分相对应,所以描述的比较简单,相关之处参见方法部分说明即可。
图1示出一个用于多线程处理的示意性流程实施例。
该实施例中,所述用于多线程处理的方法包括:创建包括多个子线程的线程池(步骤S11),子线程等待任务(步骤S12),建立一个或多个远程连接(步骤S13),在一个或多个远程连接的任务队列到达后,多个子线程并行执行任务(步骤S14)。
线程池对于任务的处理方式采用线程集中并行处理的方式,而不是一对一模式,因此能够解决处理器单元内多个线程执行的问题,可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力。
多个子线程并行执行任务的方式有很多,一种可选的实施方式是,各子线程分别从任务队列中取出互不相同的任务,并分别执行所取出的任务。更具体地说,每个子线程同时从任务队列中分别取出一个任务,各子线程所取出的任务互不相同,然后每个子线程分别执行其取出的任务。
在另一个示意性实施例中,所述用于多线程处理的方法包括:创建包括多个子线程的线程池;建立一个或多个远程连接;在一个或多个远程连接的任务队列到达后,多个子线程并行执行任务;所述多个子线程执行任务时所接收的数据被缓存到在同一存储空间。
在另一个示意性实施例中,所述用于多线程处理的方法包括:创建包括多个子线程的线程池;建立一个或多个远程连接;在一个或多个远程连接的任务队列到达后,多个子线程并行执行任务。其中,所述多个子线程执行任务时所接收的数据被缓存到在同一存储空间,所述存储空间的部分或全部在满足释放条件时被释放。
本实施例中,在满足释放条件的情况下内存空间才会被释放,因此无需反复申请和释放内存空间,从而有效提高系统运行效率。此外,在满足释放条件的情况下释放部分或全部内存空间,能够实现内存空间长度的动态控制,兼顾了效率和易用性两方面的需求,降低了系统无法使用的内存碎片的数量,提高了系统运行效率。
在一些可选实施例中,所述释放条件包括以下情况的一种或几种:
a)所述存储空间内,空闲存储空间的长度达到第一阈值;
b)所述存储空间内,存在空闲时间达到第二阈值的空闲存储空间;
c)所述存储空间内,空闲时间达到第二阈值的空闲存储空间的长度达到第三阈值。
上述情况为示例性说明,在实际应用时,以上这几种条件情况可以择一使用,也可以组合使用。可以看出,释放条件的选取需要根据系统设计需求或应用场景需求而灵活设定,无法采用公知常识或惯用技术手段实现。
在另一个示意性实施例中,所述用于多线程处理的方法包括:创建包括多个子线程的线程池;建立一个或多个远程连接;在一个或多个远程连接的任务队列到达后,多个子线程并行执行任务;所述多个子线程执行任务时所接收的数据被缓存到在同一存储空间,所述存储空间的部分或全部在满足释放条件时被释放;满足释放条件时,全部空闲存储空间被释放,或,空闲时间达到第四阈值的空闲存储空间被释放,或,设定长度的空闲存储空间被释放。
采用本实施提供的方法,能够对内存空间的长度实施更加精准的动态控制,从而进一步降低系统内存碎片的数量,提高系统运行效率。
在一些示意性实施例中,所述设定长度的空闲存储空间被释放,包括:按预设固定长度释放空闲存储空间,或者,根据预设规则获得长度值并按所述长度值释放空闲存储空间。
可以看出,空闲存储空间的释放方式需要根据系统设计需求或应用场景需求而灵活设定,无法采用公知常识或惯用技术手段实现。
需要说明的是,在上述所有实施例中,存储空间的实现方式有很多,一种可选的实现方式是,存储空间由一个或多个单位存储空间组成。该实施方式中,线程池对内存的申请和管理采用内存池技术,从而能够分配多个大小相同的单位存储空间,极大加快内存分配/释放过程,有效提高系统运行效率。
在存储空间由一个或多个单位存储空间组成的情况下,可选的释放条件包括以下情况的一种或几种:a)所述存储空间内,空闲存储空间的长度达到第一阈值;b)所述存储空间内,存在空闲时间达到第二阈值的空闲存储空间;c)所述存储空间内,空闲时间达到第二阈值的空闲存储空间的长度达到第三阈值。可选地,第一阈值和/或第三阈值为单位存储空间长度的整数倍。
在存储空间由一个或多个单位存储空间组成的情况下,当满足释放条件时,全部空闲存储空间被释放,或,空闲时间达到第四阈值的空闲存储 空间被释放,或,设定长度的空闲存储空间被释放。可选地,被释放空闲存储空间的长度为单位存储空间长度的整数倍。
当数据量增加,现有存储空间无法满足任务处理需要的情况下,可以通过合并系统中相邻的空闲存储空间扩展存储空间。一种可选的方式是,将系统中相邻的两个空闲存储空间合并,以扩展用于线程池的存储空间。
当内存空间不够使用时,可扩展内存空间长度到2倍或更多,从而实现内存空间长度的动态控制,有效降低系统内存碎片的数量。合并相邻的空闲内存空间,既能实现内存块长度的动态控制,又兼顾易用性和合并效率这两方面的需求,提高系统运行效率。
在上述所有的实施例中,当任务队列中的任务数量发生变化时,还可以包括:根据任务的数量删除或增加子线程,包括:当任务的数量小于第四阈值时,删除一个或多个子线程;当任务的数量大于第五阈值时,创建一个或多个新的子线程。从而,实现对子线程数量的动态控制,提高系统运行效率。
图2示出另一个用于多线程处理的示意性流程实施例。
在该示例性实施例中,程序启动后,主线程将启动套接字(Socket)服务(步骤S21),监听指定端口(步骤S22)并接收客户端新的连接请求(S23)。如果收到新的连接请求,则建立新连接并将新连接添加到线程池(S24),并由线程池处理与客户端的数据通信(S25)。未收到新的连接请求,则继续监听端口。
图3示出一个子线程处理的示意性流程实施例。
线程池中将运行若干个子线程(数量可配置),将并行执行所有客户端连接的数据发送和接收任务。子线程启动后等待接收数据(S31),收到数据则缓存数据(S32)并判断缓存的数据是否是一个完整数据包(S33),如果缓存的是完整数据包,则取出数据包并解析(S34),然后处理数据(S35);否则继续接收数据。如果未接收到数据,则子线程继续等待接收数据。
图4示出一个用于多线程处理的示意性装置实施例。
该示意性实施例中,所述用于多线程处理的装置包括第一单元S01、第二单元S02和第三单元S03。
第一单元S01用于创建包括多个子线程的线程池,第二单元S02用于 建立一个或多个远程连接,第三单元S03用于在所述一个或多个远程连接的任务队列到达后,管理所述多个子线程并行执行任务。
在一些示意性实施例中,所述多个子线程同时分别从所述任务队列中取出一个任务并行执行。
在一些示意性实施例中,所述于多线程处理的装置还包括提供存储空间的存储单元,用于缓存所述多个子线程执行任务时所接收的数据。可选地,所述存储空间由一个或多个单位存储空间组成。
在一些示例性实施例中,所述存储空间的部分或全部在满足释放条件时被释放,所述释放条件包括以下情况的一种或几种:
a)所述存储空间内,空闲存储空间的长度达到第一阈值;b)所述存储空间内,存在空闲时间达到第二阈值的空闲存储空间;c)所述存储空间内,空闲时间达到第二阈值的空闲存储空间的长度达到第三阈值。
在一些示例性实施例中,在存储空间由一个或多个单位存储空间组成的情况下,所述存储空间的部分或全部在满足释放条件时被释放,所述释放条件包括以下情况的一种或几种:
a)所述存储空间内,空闲存储空间的长度达到第一阈值;b)所述存储空间内,存在空闲时间达到第二阈值的空闲存储空间;c)所述存储空间内,空闲时间达到第二阈值的空闲存储空间的长度达到第三阈值。可选地,第一阈值和第三阈值为单位存储空间长度的整数倍。
在一些示例性实施例中,满足释放条件时,全部空闲存储空间被释放,或,空闲时间达到第四阈值的空闲存储空间被释放,或,设定长度的空闲存储空间被释放。
在一些示意性实施例中,所述设定长度的空闲存储空间被释放,包括:按预设固定长度释放空闲存储空间,或者,根据预设规则获得长度值并按所述长度值释放空闲存储空间。
可以看出,空闲存储空间的释放方式需要根据系统设计需求或应用场景需求而灵活设定,无法采用公知常识或惯用技术手段实现。
在一些示例性实施例中,在存储空间由一个或多个单位存储空间组成的情况下,满足释放条件时,全部空闲存储空间被释放,或,空闲时间达到第四阈值的空闲存储空间被释放,或,设定长度的空闲存储空间被释放。 可选地,被释放空闲存储空间的长度为单位存储空间长度的整数倍。
在一些示例性实施例中,所述第一单元S01还用于根据任务的数量删除或增加子线程,包括:当通信任务的数量小于第一阈值时,删除一个或多个子线程;当通信任务的数量大于第二阈值时,创建一个或多个新的子线程。
在一些示例性实施例中,还公开一种服务器,该服务器包括如上述任一实施例所述的用于多线程处理的装置。
在示例性实施例中,还提供了一种包括指令的非临时性计算机可读存储介质,例如包括指令的存储器,上述指令可由处理器执行以完成前文所述的方法。上述非临时性计算机可读存储介质可以是只读存储器(Read Only Memory,ROM)、随机存取存储器(Random Access Memory,RAM)、磁带和光存储设备等。
本领域普通技术人员可以意识到,结合本文中所公开的实施例描述的各示例的单元及算法步骤,能够以电子硬件、或者计算机软件和电子硬件的结合来实现。这些功能究竟以硬件还是软件方式来执行,取决于技术方案的特定应用和设计约束条件。所属技术人员可以对每个特定的应用来使用不同方法来实现所描述的功能,但是这种实现不应认为超出本发明的范围。所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,上述描述的系统、装置和单元的具体工作过程,可以参考前述方法实施例中的对应过程,在此不再赘述。
本文所披露的实施例中,应该理解到,所揭露的方法、产品(包括但不限于装置、设备等),可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,所述单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,例如多个单元或组件可以结合或者可以集成到另一个系统,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其它的形式。所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分 或者全部单元来实现本实施例方案的目的。另外,在本发明各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。
应当理解的是,本发明并不局限于上面已经描述并在附图中示出的流程及结构,并且可以在不脱离其范围进行各种修改和改变。本发明的范围仅由所附的权利要求来限制。
Claims (13)
- 一种用于多线程处理的方法,其特征在于,包括:创建包括多个子线程的线程池;建立一个或多个远程连接;所述一个或多个远程连接的任务队列到达后,所述多个子线程并行执行任务。
- 如权利要求1所述的方法,其特征在于,所述多个子线程并行执行任务,包括:各子线程分别从所述任务队列中取出互不相同的任务,并分别执行所取出的任务。
- 如权利要求1或2所述的方法,其特征在于,所述多个子线程执行任务时所接收的数据被缓存到在同一存储空间。
- 如权利要求3所述的方法,其特征在于,所述存储空间的部分或全部在满足释放条件时被释放。
- 如权利要4所述的方法,其特征在于,满足释放条件时,全部空闲存储空间被释放,或,空闲时间达到第四阈值的空闲存储空间被释放,或,设定长度的空闲存储空间被释放。
- 如权利要求1至5任一项所述的方法,其特征在于,还包括:根据任务的数量删除或增加子线程。
- 一种用于多线程处理的装置,其特征在于,包括:第一单元,用于创建包括多个子线程的线程池;第二单元,用于建立一个或多个远程连接;第三单元,用于在所述一个或多个远程连接的任务队列到达后,管理所述多个子线程并行执行任务。
- 如权利要求7所述的装置,其特征在于,所述第三单元包括:任务调取单元,用于调度各子线程分别从所述任务队列中取出互不相同的任务;和,任务执行单元,用于调度各子线程分别执行所取出的任务。
- 如权利要求7或8所述的装置,其特征在于,还包括提供存储空间的存储单元,用于缓存所述多个子线程执行任务时所接收的数据。
- 如权利要求9所述的装置,其特征在于,所述存储空间的部分或全部在满足释放条件时被释放。
- 如权利要10所述的装置,其特征在于,满足释放条件时,全部空闲存储空间被释放,或,空闲时间达到第四阈值的空闲存储空间被释放,或,设定长度的空闲存储空间被释放。
- 如权利要求7至11任一项所述的装置,其特征在于,所述第一单元还用于根据任务的数量删除或增加子线程。
- 一种服务器,其特征在于,包括如权利要求7至12任一项所述的用于多线程处理的装置。
Applications Claiming Priority (2)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201611242037.0A CN106648872A (zh) | 2016-12-29 | 2016-12-29 | 用于多线程处理的方法及装置、服务器 |
| CN201611242037.0 | 2016-12-29 |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| WO2018121696A1 true WO2018121696A1 (zh) | 2018-07-05 |
Family
ID=58836273
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| PCT/CN2017/119561 Ceased WO2018121696A1 (zh) | 2016-12-29 | 2017-12-28 | 用于多线程处理的方法及装置、服务器 |
Country Status (2)
| Country | Link |
|---|---|
| CN (1) | CN106648872A (zh) |
| WO (1) | WO2018121696A1 (zh) |
Cited By (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN113626176A (zh) * | 2020-05-08 | 2021-11-09 | 北京沃东天骏信息技术有限公司 | 一种业务请求处理方法及装置 |
| CN117472593A (zh) * | 2023-12-27 | 2024-01-30 | 中诚华隆计算机技术有限公司 | 一种多线程间资源分配方法及系统 |
Families Citing this family (12)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN106648872A (zh) * | 2016-12-29 | 2017-05-10 | 深圳市优必选科技有限公司 | 用于多线程处理的方法及装置、服务器 |
| CN107220033A (zh) * | 2017-07-05 | 2017-09-29 | 百度在线网络技术(北京)有限公司 | 用于控制线程池中线程数量的方法和装置 |
| CN109660569B (zh) * | 2017-10-10 | 2021-10-15 | 武汉斗鱼网络科技有限公司 | 一种多任务并发执行方法、存储介质、设备及系统 |
| CN109766131B (zh) * | 2017-11-06 | 2022-04-01 | 上海宝信软件股份有限公司 | 基于多线程技术实现软件智能化自动升级的系统及方法 |
| CN108492211A (zh) * | 2018-04-04 | 2018-09-04 | 北京科东电力控制系统有限责任公司 | 应用于电力市场业务平台的计算方法及装置 |
| CN108510333B (zh) * | 2018-04-27 | 2022-11-15 | 厦门南讯股份有限公司 | 一种多客户积分快速过期的处理方法及装置 |
| CN109582455B (zh) * | 2018-12-03 | 2021-06-18 | 恒生电子股份有限公司 | 多线程任务处理方法、装置及存储介质 |
| CN112578259B (zh) * | 2019-09-29 | 2022-06-10 | 北京君正集成电路股份有限公司 | 一种具有数据空间设置的线程调度方法 |
| CN112817771A (zh) * | 2021-04-15 | 2021-05-18 | 成都四方伟业软件股份有限公司 | 一种共享多线程服务管理方法及装置 |
| CN113360266B (zh) * | 2021-06-23 | 2022-09-13 | 北京百度网讯科技有限公司 | 任务处理方法和装置 |
| CN113391926B (zh) * | 2021-06-28 | 2024-08-02 | 京东方科技集团股份有限公司 | 一种播放数据的方法、装置及电子设备 |
| CN115223541A (zh) * | 2022-06-21 | 2022-10-21 | 深圳市优必选科技股份有限公司 | 文本转语音的处理方法、装置、设备及存储介质 |
Citations (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20050022173A1 (en) * | 2003-05-30 | 2005-01-27 | Codito Technologies Private Limited | Method and system for allocation of special purpose computing resources in a multiprocessor system |
| CN101287166A (zh) * | 2008-02-22 | 2008-10-15 | 北京航空航天大学 | 一种用于电子会务辅助系统的短信息发布系统与发布方法 |
| CN101957863A (zh) * | 2010-10-14 | 2011-01-26 | 广州从兴电子开发有限公司 | 数据并行处理方法、装置及系统 |
| CN102681889A (zh) * | 2012-04-27 | 2012-09-19 | 电子科技大学 | 一种云计算开放平台的调度方法 |
| CN106648872A (zh) * | 2016-12-29 | 2017-05-10 | 深圳市优必选科技有限公司 | 用于多线程处理的方法及装置、服务器 |
-
2016
- 2016-12-29 CN CN201611242037.0A patent/CN106648872A/zh active Pending
-
2017
- 2017-12-28 WO PCT/CN2017/119561 patent/WO2018121696A1/zh not_active Ceased
Patent Citations (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20050022173A1 (en) * | 2003-05-30 | 2005-01-27 | Codito Technologies Private Limited | Method and system for allocation of special purpose computing resources in a multiprocessor system |
| CN101287166A (zh) * | 2008-02-22 | 2008-10-15 | 北京航空航天大学 | 一种用于电子会务辅助系统的短信息发布系统与发布方法 |
| CN101957863A (zh) * | 2010-10-14 | 2011-01-26 | 广州从兴电子开发有限公司 | 数据并行处理方法、装置及系统 |
| CN102681889A (zh) * | 2012-04-27 | 2012-09-19 | 电子科技大学 | 一种云计算开放平台的调度方法 |
| CN106648872A (zh) * | 2016-12-29 | 2017-05-10 | 深圳市优必选科技有限公司 | 用于多线程处理的方法及装置、服务器 |
Cited By (3)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN113626176A (zh) * | 2020-05-08 | 2021-11-09 | 北京沃东天骏信息技术有限公司 | 一种业务请求处理方法及装置 |
| CN117472593A (zh) * | 2023-12-27 | 2024-01-30 | 中诚华隆计算机技术有限公司 | 一种多线程间资源分配方法及系统 |
| CN117472593B (zh) * | 2023-12-27 | 2024-03-22 | 中诚华隆计算机技术有限公司 | 一种多线程间资源分配方法及系统 |
Also Published As
| Publication number | Publication date |
|---|---|
| CN106648872A (zh) | 2017-05-10 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| WO2018121696A1 (zh) | 用于多线程处理的方法及装置、服务器 | |
| CN108023908B (zh) | 数据更新方法、装置及系统 | |
| CN106850829B (zh) | 一种基于非阻塞通信的微服务系统设计方法 | |
| US10380058B2 (en) | Processor core to coprocessor interface with FIFO semantics | |
| CN102520936B (zh) | 一种在Android上实现Socket通讯服务共享的方法 | |
| US20160162330A1 (en) | Epoll optimisations | |
| CN107526645A (zh) | 一种通信优化方法及系统 | |
| CN108595282A (zh) | 一种高并发消息队列的实现方法 | |
| CN113760400A (zh) | 一种请求处理方法和装置 | |
| CN106933664B (zh) | 一种Hadoop集群的资源调度方法及装置 | |
| WO2016074472A1 (zh) | 分布式事务资源执行的方法、装置和系统 | |
| CN107203560A (zh) | 数据库、多数据库操作事务一致性保证方法及系统 | |
| CN103532876A (zh) | 数据流的处理方法与系统 | |
| WO2017028399A1 (zh) | 通信数据传输方法及系统 | |
| CN104683472A (zh) | 一种支持大数据量的数据传输方法 | |
| CN106155803A (zh) | 一种基于信号量的线程池共享方法及系统 | |
| CN110162410A (zh) | 一种消息处理方法和装置 | |
| CN103546557A (zh) | 一种平台级服务的数据交互方法、服务平台及系统 | |
| CN110324370B (zh) | 服务端向客户端推送数据的方法和装置 | |
| CN107766127A (zh) | 事务消息处理方法、装置、设备及系统 | |
| CN112306695A (zh) | 数据的处理方法、装置、电子设备及计算机存储介质 | |
| US9875147B2 (en) | Management of asynchronous and synchronous resource requests | |
| CN105245372A (zh) | 一种基于缓存和异步机制的加载网络资源的优化方法 | |
| CN108337285A (zh) | 一种通信系统及通信方法 | |
| US10523793B2 (en) | Kernel multiplexing system of communications |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| 121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 17889365 Country of ref document: EP Kind code of ref document: A1 |
|
| NENP | Non-entry into the national phase |
Ref country code: DE |
|
| 122 | Ep: pct application non-entry in european phase |
Ref document number: 17889365 Country of ref document: EP Kind code of ref document: A1 |