java多线程网页下载代码
1.使用了java.util.concurrent包里的线程池,可以飙升到满带宽,在100M带宽上,可以达到10MB/s。
2.使用了java.nio里的channels,性能比自己缓冲有一些提高。

1 import java.io.FileOutputStream;
2 import java.io.InputStream;
3 import java.net.URL;
4 import java.net.URLConnection;
5 import java.nio.channels.Channels;
6 import java.nio.channels.FileChannel;
7 import java.nio.channels.ReadableByteChannel;
8 import java.util.Calendar;
9 import java.util.concurrent.Callable;
10 import java.util.concurrent.ExecutorService;
11 import java.util.concurrent.Executors;
12
13 public class HttpDownloader implements Callable<String> {
14 URLConnection connection;
15 FileChannel outputChann;
16 public static volatile int count = 0;
17
18 public static void main(String[] args) throws Exception {
19
20 ExecutorService poll = Executors.newFixedThreadPool(100);
21
22 for (int i = 0; i < 100; i++) {
23 Calendar now = Calendar.getInstance();
24 String fileName = "../data/" + now.get(Calendar.YEAR) + "年"
25 + (now.get(Calendar.MONTH) + 1) + "月"
26 + now.get(Calendar.DAY_OF_MONTH) + "日--" + i + ".txt";
27 poll.submit(new HttpDownloader("http://www.sina.com",
28 (new FileOutputStream(fileName)).getChannel()));
29 }
30
31 poll.shutdown();
32
33 long start = System.currentTimeMillis();
34 while (!poll.isTerminated()) {
35 Thread.sleep(1000);
36 System.out.println("已运行"
37 + ((System.currentTimeMillis() - start) / 1000) + "秒,"
38 + HttpDownloader.count + "个任务还在运行");
39 }
40 }
41
42 public HttpDownloader(String url, FileChannel fileChannel) throws Exception {
43 synchronized (HttpDownloader.class) {
44 count++;
45 }
46 connection = (new URL(url)).openConnection();
47 this.outputChann = fileChannel;
48 }
49
50 @Override
51 public String call() throws Exception {
52 connection.connect();
53 InputStream inputStream = connection.getInputStream();
54 ReadableByteChannel rChannel = Channels.newChannel(inputStream);
55 outputChann.transferFrom(rChannel, 0, Integer.MAX_VALUE);
56 // System.out.println(Thread.currentThread().getName() + " completed!");
57 inputStream.close();
58 outputChann.close();
59 synchronized (HttpDownloader.class) {
60 count--;
61 }
62 return null;
63 }
64 }


1 import java.io.FileOutputStream;
2 import java.io.InputStream;
3 import java.net.URL;
4 import java.net.URLConnection;
5 import java.nio.channels.Channels;
6 import java.nio.channels.FileChannel;
7 import java.nio.channels.ReadableByteChannel;
8 import java.util.Calendar;
9 import java.util.concurrent.Callable;
10 import java.util.concurrent.ExecutorService;
11 import java.util.concurrent.Executors;
12
13 public class HttpDownloader implements Callable<String> {
14 URLConnection connection;
15 FileChannel outputChann;
16 public static volatile int count = 0;
17
18 public static void main(String[] args) throws Exception {
19
20 ExecutorService poll = Executors.newFixedThreadPool(100);
21
22 for (int i = 0; i < 100; i++) {
23 Calendar now = Calendar.getInstance();
24 String fileName = "../data/" + now.get(Calendar.YEAR) + "年"
25 + (now.get(Calendar.MONTH) + 1) + "月"
26 + now.get(Calendar.DAY_OF_MONTH) + "日--" + i + ".txt";
27 poll.submit(new HttpDownloader("http://www.sina.com",
28 (new FileOutputStream(fileName)).getChannel()));
29 }
30
31 poll.shutdown();
32
33 long start = System.currentTimeMillis();
34 while (!poll.isTerminated()) {
35 Thread.sleep(1000);
36 System.out.println("已运行"
37 + ((System.currentTimeMillis() - start) / 1000) + "秒,"
38 + HttpDownloader.count + "个任务还在运行");
39 }
40 }
41
42 public HttpDownloader(String url, FileChannel fileChannel) throws Exception {
43 synchronized (HttpDownloader.class) {
44 count++;
45 }
46 connection = (new URL(url)).openConnection();
47 this.outputChann = fileChannel;
48 }
49
50 @Override
51 public String call() throws Exception {
52 connection.connect();
53 InputStream inputStream = connection.getInputStream();
54 ReadableByteChannel rChannel = Channels.newChannel(inputStream);
55 outputChann.transferFrom(rChannel, 0, Integer.MAX_VALUE);
56 // System.out.println(Thread.currentThread().getName() + " completed!");
57 inputStream.close();
58 outputChann.close();
59 synchronized (HttpDownloader.class) {
60 count--;
61 }
62 return null;
63 }
64 }

java多线程网页下载代码的更多相关文章
- Java多线程的下载器(1)
实现了一个基于Java多线程的下载器,可提供的功能有: 1. 对文件使用多线程下载,并显示每时刻的下载速度. 2. 对多个下载进行管理,包括线程调度,内存管理等. 一:单个文件下载的管理 1. 单文件 ...
- 用 python 实现一个多线程网页下载器
今天上来分享一下昨天实现的一个多线程网页下载器. 这是一个有着真实需求的实现,我的用途是拿它来通过 HTTP 方式向服务器提交游戏数据.把它放上来也是想大家帮忙挑刺,找找 bug,让它工作得更好. k ...
- java多线程断点下载原理(代码实例演示)
原文:http://www.open-open.com/lib/view/open1423214229232.html 其实多线程断点下载原理,很简单的,那么我们就来先了解下,如何实现多线程的断点下载 ...
- Java多线程断点下载文件
Java实现断点续传+多线程下载 如下代码所示,每一步都有注解 思路: 通过URL连接到服务器上要下载的文件,得到文件的大小: 算出每条线程下载的开始位置和结束位置,例如,有两条线程下载100Byte ...
- java 多线程安全问题-同步代码块
/* 多线程的安全问题: while(true) { if(tick>0) { //线程0,1,2,3在余票为1时,都停滞在这里,之后分别获得CPU执行权,打印出0,-1,-2等错票 Syste ...
- java 多线程断点下载功能
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.Rand ...
- java多线程批量下载文件
多线程下载文件 平时开发中有时会用到文件下载,为了提高文件的下载速率,采用多线程下载能够达到事半功倍的效果: package test; /** * 文件下载类 * @author luweichen ...
- Java多线程断点下载
public static class DownloadThread extends Thread{ private int threadId; private int startIndex; pri ...
- java 多线程断点下载demo
源码链接 import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java ...
- java的报表下载代码excel
/** * 汇总报表数据下载 * */ private ModelAndView exportSummaryDatadown(HttpServletRequest request, HttpServl ...
随机推荐
- 通过DashScope API调用将多种模态转换为向量
本文介绍如何通过模型服务灵积DashScope进行 多模态向量生成 ,并入库至向量检索服务DashVector中进行向量检索. 模型服务灵积DashScope,通过灵活.易用的模型API服务,让各种模 ...
- 工具 – Cypress
介绍 Cypress 是一款 e2e 测试工具.每当我们写好一个组件或者一个页面之后,我们会想对整体做一个测试. 在不使用工具的情况下,我们会开启 browser,然后做一系列点击.滚动.填 form ...
- ASP.NET Core – Logging & Serilog
前言 以前就写过了 Asp.net core 学习笔记 (Logging 日志), 只是有点乱, 这篇作为整理版. 参考: docs – Logging in .NET Core and ASP.NE ...
- SQL Server – 树结构 (二叉树, 红黑树, B-树, B+树)
前言 很久以前有学习过各种树结构, 但后来真的没有在实际项目中运用到. 毕竟我主要负责的都是写业务代码. 太上层了 但是忘光光还是很可惜的. 所以久久可以复习一下. 记得概念也好, 帮助思考. 参考: ...
- HTML – W3Schools 学习笔记
有用链接: HTML Attribute Reference (查看所有 Attributes) HTML Paragraphs Link to W3Schools <p> 里面 doub ...
- SqlEs-像使用数据库一样使用Elasticsearch
SqlEs SqlEs是Elasticsearch的客户端JDBC驱动程序,支持采用sql语法操作Elasticsearch.SqlEs构建在RestHighLevelClient,屏蔽了RestHi ...
- 30. 串联所有单词的子串 Golang实现
题目描述: 给定一个字符串 s 和一个字符串数组 words. words 中所有字符串 长度相同 . s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串. 例如, ...
- 北京智和信通亮相2023IT运维大会,共话数智浪潮下自动化运维新生态
2023年9月21日,由IT运维网.<网络安全和信息化>杂志社联合主办的"2023(第十四届)IT运维大会"在北京成功举办.大会以"以数为基 智引未来&quo ...
- IT运维工单高效协同,助力打造一站式运维方案
随着经济全球化的发展趋势,信息系统在企业运营中占据着愈发重要的位置.业务系统越来越多,用户对信息系统的依赖性越来越强,关键业务系统的中断都将导致企业业务.服务的中断,极大的影响了企业业务稳定运行和持续 ...
- 北京智和信通:IT资产全生命周期运维监控管理方案
IT资产是企业开展正常业务运营和拓展不可或缺的资源,也是企业财产的重要载体.随着信息科技的快速发展,各企业对IT资产的依赖逐渐增强,IT资产的可靠性和有效性面临着愈来愈大的挑战.例如IT资产管理混乱, ...