CN111324458A - 一种基于Java的大文件下载加速方法 - Google Patents
一种基于Java的大文件下载加速方法 Download PDFInfo
- Publication number
- CN111324458A CN111324458A CN202010098294.1A CN202010098294A CN111324458A CN 111324458 A CN111324458 A CN 111324458A CN 202010098294 A CN202010098294 A CN 202010098294A CN 111324458 A CN111324458 A CN 111324458A
- Authority
- CN
- China
- Prior art keywords
- downloading
- java
- file
- task
- thread pool
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR 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/5061—Partitioning or combining of resources
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/30—Monitoring
- G06F11/3003—Monitoring arrangements specially adapted to the computing system or computing system component being monitored
- G06F11/3037—Monitoring arrangements specially adapted to the computing system or computing system component being monitored where the computing system component is a memory, e.g. virtual memory, cache
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/30—Monitoring
- G06F11/3055—Monitoring arrangements for monitoring the status of the computing system or of the computing system component, e.g. monitoring if the computing system is on, off, available, not available
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR 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; CALCULATING OR 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/505—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 load
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2209/00—Indexing scheme relating to G06F9/00
- G06F2209/50—Indexing scheme relating to G06F9/50
- G06F2209/5011—Pool
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2209/00—Indexing scheme relating to G06F9/00
- G06F2209/50—Indexing scheme relating to G06F9/50
- G06F2209/5017—Task decomposition
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2209/00—Indexing scheme relating to G06F9/00
- G06F2209/50—Indexing scheme relating to G06F9/50
- G06F2209/5018—Thread allocation
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2209/00—Indexing scheme relating to G06F9/00
- G06F2209/50—Indexing scheme relating to G06F9/50
- G06F2209/508—Monitor
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- Software Systems (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Computing Systems (AREA)
- Quality & Reliability (AREA)
- Mathematical Physics (AREA)
- Devices For Executing Special Programs (AREA)
- Stored Programmes (AREA)
Abstract
本发明公开了一种基于Java的大文件下载加速方法,所述方法基于Java多线程技术,采用线程池技术,利用JUC包中的ForkJoinPool,均衡CPU的负载,把一个大文件分割成多个小文件进行下载,下载完成之后再重新合并成一个大文件。本发明方法具有以下突出的有益效果:1.通过JVM可用的处理器个数,保证CPU负载均衡;2.通过线程池做线程管理,保证了线程有效的执行;3.具有查看下载进度,监控内存的功能。
Description
技术领域
本发明涉及Java/多线程技术领域,涉及一种加速文件下载装置,具体提供一种基于Java的大文件下载加速方法。
背景技术
现有的常用下载方式是基于浏览器的单线程下载。这种单线程下载的方式,是通过HTTP传输协议请求下载。一般而言,把需要下载的文件放在服务器的某个路径下,或者服务端通过数据库拼接文件,当客户端请求服务器进行下载的时候,服务端通过对应路径或者数据库拼接文件返回给客户端文件字节流。客户端进行格式整理,然后弹出下载窗口,浏览器自带的下载引擎进行下载。
基于浏览器的单线程下载,或者说是计算整个文件的比特流下载,在一定程度上是简洁可靠的。这样下载的好处是能有效的保证可靠性。但同时,在某些情况下,因为网速的限制加上某些站点的限流政策,下载一个大文件往往是很困难的,因为文件容量太大,无论是传输字节流或者单线程的浏览器下载,对下载速度都会有很大的影响。
发明内容
本发明为了解决下载速度过慢的问题,提供一种基于Java的大文件下载加速方法,基于Java多线程技术,采用线程池技术,和JUC包中的ForkJoinPool,均衡CPU的负载,把一个大文件使用分而治之,分割成多个小文件进行下载,下载完成之后在重新合并成一个大文件。
为实现上述目的,本发明提供了如下技术方案:
一种基于Java的大文件下载加速方法,所述方法基于Java多线程技术,采用线程池技术,利用JUC包中的ForkJoinPool,均衡CPU的负载,把一个大文件分割成多个小文件进行下载,下载完成之后再重新合并成一个大文件。
Java 5.0提供了java.util.concurrent(简称JUC)包,在此包中增加了在并发编程中很常用的工具类,用于定义类似于线程的自定义子系统,包括线程池,异步IO和轻量级任务框架;还提供了设计用于多线程上下文中的Collection实现等。
所述方法的实现包括进程如下:
1)连接请求进程:连接远程HTTP,获取相关参数,保证稳定的HTTP请求连接;
2)文件处理进程:进行文件切割,文件合并等操作;
3)任务进程:把文件处理成Java可执行的任务;
4)下载进程:进行下载管理;
5)汇报进程:汇报内存和下载进度情况。
所述方法实现步骤包括:
1)指定下载文件;
2)创建线程池;
3)分割任务;
4)提交分割后的任务到线程池;
5)使用ForkJoinPool搭建框架图;
6)监控下载进度;
7)监控内存使用情况。
所述线程池的核心线程数为JVM可用的处理器数量的2倍,保证CPU负载均衡。因为下载文件是IO密集型文件,所以根据线程池的设计经验,设计线程池的核心线程数为处理器数量*2。
JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的。
Java语言的一个非常重要的特点就是与平台的无关性。而使用Java虚拟机是实现这一特点的关键。一般的高级语言如果要在不同的平台上运行,至少需要编译成不同的目标代码。而引入Java语言虚拟机后,Java语言在不同平台上运行时不需要重新编译。Java语言使用Java虚拟机屏蔽了与具体平台相关的信息,使得Java语言编译程序只需生成在Java虚拟机上运行的目标代码(字节码),就可以在多种平台上不加修改地运行。Java虚拟机在执行字节码时,把字节码解释成具体平台上的机器指令执行。这就是Java的能够"一次编译,到处运行"的原因。
所述方法通过使用Java的JUC包中的构造器创建线程池。具体的代码如下:
ExecutorService executorPool=new ThreadPoolExecutor(8,8,60,
TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(20){},
(runnable)->new Thread(runnable,"MyThread"),new ThreadPoolExecutor.DiscardPolicy());
所述方法中分割任务的实现包括内容如下:计算文件的总大小,然后除以处理器数量n,按byte流分割成n个小任务,把每个小任务通过sumit()方法提交到线程池。
所述方法使用ForkJoinPool框架采用递归的方式,分割执行每个小任务,然后使用合并(merge)的方式把任务结合起来。
递归做为一种算法在程序设计语言中广泛应用。是指函数/过程/子程序在运行过程序中直接或间接调用自身而产生的重入现象。在计算机编程里,递归指的是一个过程:函数不断引用自身,直到引用的对象已知。
所述方法通过综合每个子任务的下载的进度,汇报总下载进度。
所述方法通过使用Java自带工具jconsole监控进程使用内存大小。
所述方法实现系统包括系统模块如下:
1)连接请求系统;
2)文件处理系统;
3)任务系统;
4)下载系统;
5)汇报系统。
与现有技术相比,本发明一种基于Java的大文件下载加速方法具有以下突出的有益效果:
1.通过JVM可用的处理器个数,保证CPU负载均衡;
2.通过线程池做线程管理,保证了线程有效的执行;
3.具有查看下载进度,监控内存的功能。
附图说明
图1是本发明方法实现流程图;
图2为分割任务流程图;
图3为ForkJoinPool架构图;
图4为本发明实现系统模块图。
具体实施方式
下面将结合附图和实施例,对本发明作进一步详细说明。
实施例
一种基于Java的大文件下载加速方法,所述方法基于Java多线程技术,采用线程池技术,利用JUC包中的ForkJoinPool,均衡CPU的负载,把一个大文件分割成多个小文件进行下载,下载完成之后再重新合并成一个大文件。
所述方法的实现包括进程如下:
1)连接请求进程:连接远程HTTP,获取相关参数,保证稳定的HTTP请求连接;
2)文件处理进程:进行文件切割,文件合并等操作;
3)任务进程:把文件处理成Java可执行的任务;
4)下载进程:进行下载管理;
5)汇报进程:汇报内存和下载进度情况。
如图1所示,所述方法实现步骤包括:
1)指定下载文件;
2)创建线程池;
3)分割任务;
4)提交分割后的任务到线程池;
5)使用ForkJoinPool搭建框架图;
6)监控下载进度;
7)监控内存使用情况。
所述线程池的核心线程数为JVM可用的处理器数量的2倍,保证CPU负载均衡。因为下载文件是IO密集型文件,所以根据线程池的设计经验,设计线程池的核心线程数为处理器数量*2。
所述方法通过使用Java的JUC包中的构造器创建线程池。具体的代码如下:
ExecutorService executorPool=new ThreadPoolExecutor(8,8,60,
TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(20){},
(runnable)->new Thread(runnable,"MyThread"),new ThreadPoolExecutor.DiscardPolicy());
如图2所示,所述方法中分割任务的实现包括内容如下:计算文件的总大小,然后除以处理器数量n,按byte流分割成n个小任务,把每个小任务通过sumit()方法提交到线程池。
所述方法使用ForkJoinPool框架采用递归的方式,分割执行每个小任务,然后使用合并(merge)的方式把任务结合起来。递归做为一种算法在程序设计语言中广泛应用。是指函数/过程/子程序在运行过程序中直接或间接调用自身而产生的重入现象。在计算机编程里,递归指的是一个过程:函数不断引用自身,直到引用的对象已知。
如图3所示,为ForkJoinPool架构图,其中:
Executor运行;
Executorservice ExecutorService是Java提供的线程池,也就是说,每次我们需要使用线程的时候,可以通过ExecutorService获得线程;
Scheduledexecutorservice延迟/周期执行线程池,主要作用就是可以将定时任务与线程池功能结合使用;
Scheduledthreadpoolexecutor用来在给定延时后执行异步任务或者周期性执行任务,相对于任务调度的Timer来说,其功能更加强大,Timer只能使用一个后台线程执行任务,而ScheduledThreadPoolExecutor则可以通过构造函数来指定后台线程的个数;
Threadpoolexecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程调度,线程池管理等等服务;
Forkjoinpool的优势在于,可以充分利用多cpu,多核cpu的优势,把一个任务拆分成多个“小任务”,把多个“小任务”放到多个处理器核心上并行执行;当多个“小任务”执行完成之后,再将这些执行结果合并起来即可。
所述方法通过综合每个子任务的下载的进度,汇报总下载进度。
所述方法通过使用Java自带工具jconsole监控进程使用内存大小。
如图4所示,所述方法实现系统包括系统模块如下:
1)连接请求系统;
2)文件处理系统;
3)任务系统;
4)下载系统;
5)汇报系统。
以上所述的实施例,只是本发明较优选的具体实施方式,本领域的技术人员在本发明技术方案范围内进行的通常变化和替换都应包含在本发明的保护范围内。
Claims (10)
1.一种基于Java的大文件下载加速方法,其特征在于,所述方法基于Java多线程技术,采用线程池技术,利用JUC包中的ForkJoinPool,均衡CPU的负载,把一个大文件分割成多个小文件进行下载,下载完成之后再重新合并成一个大文件。
2.根据权利要求1所述的一种基于Java的大文件下载加速方法,其特征在于,所述方法的实现包括进程如下:
1)连接请求进程:连接远程HTTP,获取相关参数,保证稳定的HTTP请求连接;
2)文件处理进程:进行文件切割,文件合并;
3)任务进程:把文件处理成Java可执行的任务;
4)下载进程:进行下载管理;
5)汇报进程:汇报内存和下载进度情况。
3.根据权利要求2所述的一种基于Java的大文件下载加速方法,其特征在于,所述方法实现步骤包括:
1)指定下载文件;
2)创建线程池;
3)分割任务;
4)提交分割后的任务到线程池;
5)使用ForkJoinPool搭建框架图;
6)监控下载进度;
7)监控内存使用情况。
4.根据权利要求3所述的一种基于Java的大文件下载加速方法,其特征在于,所述线程池的核心线程数为JVM可用的处理器数量的2倍。
5.根据权利要求4所述的一种基于Java的大文件下载加速方法,其特征在于,所述方法通过使用Java的JUC包中的构造器创建线程池。
6.根据权利要求5所述的一种基于Java的大文件下载加速方法,其特征在于,所述方法中分割任务的实现包括内容如下:计算文件的总大小,然后除以处理器数量n,按byte流分割成n个小任务,把每个小任务通过sumit()方法提交到线程池。
7.根据权利求6所述的一种基于Java的大文件下载加速方法,其特征在于,所述方法使用ForkJoinPool框架采用递归的方式,分割执行每个小任务,然后使用合并的方式把任务结合起来。
8.根据权利要求7所述的一种基于Java的大文件下载加速方法,其特征在于,所述方法通过综合每个子任务的下载的进度,汇报总下载进度。
9.根据权利要求8所述的一种基于Java的大文件下载加速方法,其特征在于,所述方法通过使用Java自带工具jconsole监控进程使用内存大小。
10.根据权利要求1-9任一所述的一种基于Java的大文件下载加速方法,其特征在于,所述方法实现系统包括系统模块如下:
1)连接请求系统;
2)文件处理系统;
3)任务系统;
4)下载系统;
5)汇报系统。
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010098294.1A CN111324458A (zh) | 2020-02-18 | 2020-02-18 | 一种基于Java的大文件下载加速方法 |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010098294.1A CN111324458A (zh) | 2020-02-18 | 2020-02-18 | 一种基于Java的大文件下载加速方法 |
Publications (1)
Publication Number | Publication Date |
---|---|
CN111324458A true CN111324458A (zh) | 2020-06-23 |
Family
ID=71167069
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202010098294.1A Pending CN111324458A (zh) | 2020-02-18 | 2020-02-18 | 一种基于Java的大文件下载加速方法 |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN111324458A (zh) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113132463A (zh) * | 2021-03-17 | 2021-07-16 | 杭州当虹科技股份有限公司 | 一种分布式的大文件快速下载方法及系统 |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
KR20140140974A (ko) * | 2013-05-30 | 2014-12-10 | 고려대학교 산학협력단 | 클라우드 환경에 비밀분산 기법을 이용한 데이터 보호 방법 |
CN106790525A (zh) * | 2016-12-20 | 2017-05-31 | 广东小天才科技有限公司 | 一种文件下载方法及装置 |
CN109040191A (zh) * | 2018-07-03 | 2018-12-18 | 平安科技(深圳)有限公司 | 文件下载方法、装置、计算机设备和存储介质 |
CN110288309A (zh) * | 2019-05-22 | 2019-09-27 | 平安科技(深圳)有限公司 | 数据交互方法、装置、系统、计算机设备以及存储介质 |
-
2020
- 2020-02-18 CN CN202010098294.1A patent/CN111324458A/zh active Pending
Patent Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
KR20140140974A (ko) * | 2013-05-30 | 2014-12-10 | 고려대학교 산학협력단 | 클라우드 환경에 비밀분산 기법을 이용한 데이터 보호 방법 |
CN106790525A (zh) * | 2016-12-20 | 2017-05-31 | 广东小天才科技有限公司 | 一种文件下载方法及装置 |
CN109040191A (zh) * | 2018-07-03 | 2018-12-18 | 平安科技(深圳)有限公司 | 文件下载方法、装置、计算机设备和存储介质 |
CN110288309A (zh) * | 2019-05-22 | 2019-09-27 | 平安科技(深圳)有限公司 | 数据交互方法、装置、系统、计算机设备以及存储介质 |
Non-Patent Citations (1)
Title |
---|
EXTREU: "使用ForkJoin方式实现多线程下载功能", 《HTTPS://MY.OSCHINA.NET/PTCZY/BLOG/1612656》 * |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113132463A (zh) * | 2021-03-17 | 2021-07-16 | 杭州当虹科技股份有限公司 | 一种分布式的大文件快速下载方法及系统 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
JP7327744B2 (ja) | ファンクションアズアサービス(FaaS)システムの強化 | |
Von Behren et al. | Capriccio: Scalable threads for internet services | |
US7275246B1 (en) | Executing programs for a first computer architecture on a computer of a second architecture | |
EP2062136B1 (en) | System and method for using stream objects to perform stream processing in a text-based computing environment | |
US20040111729A1 (en) | External event interrupt for server-side programs | |
Nicol et al. | The dark side of risk (what your mother never told you about time warp) | |
Eriksen | Your server as a function | |
Johnsen et al. | Dynamic resource reallocation between deployment components | |
Huh et al. | Cross‐layer resource control and scheduling for improving interactivity in Android | |
Schmidt et al. | Applying patterns to develop extensible and maintainable ORB middleware | |
CN111324458A (zh) | 一种基于Java的大文件下载加速方法 | |
CN101976206B (zh) | 一种中断处理方法和装置 | |
Johnsen et al. | A formal model of object mobility in resource-restricted deployment scenarios | |
EP4204960A1 (en) | Thread-local return structure for asynchronous state machine | |
Vu et al. | Formalizing sane virtual processor in thread algebra | |
Nilsson et al. | Deterministic Java in tiny embedded systems | |
Page et al. | Scalability of streaming anomaly detection in an unbounded key space using migrating threads | |
Paulino et al. | A multi-threaded asynchronous language | |
Huber | Worst-case execution time analysis for real-time Java | |
Poroor et al. | Formal analysis of event-driven cyber physical systems | |
Gioulekas et al. | Correct-by-construction model-based design of reactive streaming software for multi-core embedded systems | |
Aichouch | Evaluation of a multiple criticality real-time virtual machine system and configuration of an RTOS's resources allocation techniques | |
Gu et al. | Model-checking of component-based event-driven real-time embedded software | |
Barreto et al. | Abstract specification and formalization of an operating system kernel in Z | |
Bouyssounouse et al. | Programming languages for real-time systems |
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 | ||
RJ01 | Rejection of invention patent application after publication | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20200623 |