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 ...
随机推荐
- 数据库中查询含有某个emoji表情的行数据
数据库中查询含有某个emoji表情的行数据 MySQL的情况 代码如下 create table tt6(id int, name varchar(800)); insert into tt6 s ...
- LLM 写标书
云孚科技 有提到标书写作 https://www.sohu.com/a/726319389_121119682 https://www.aihub.cn/tools/writing/yfwrite/ ...
- sentinel中如何使用@SentinelResource和openFeign来进行服务熔断和降级的操作
sentinel 前方参考 计算QPS-Sentinel限流算法 https://www.cnblogs.com/yizhiamumu/p/16819497.html Sentinel 介绍与下载使用 ...
- C++源码中司空见惯的PIMPL是什么?
前言: C++源码中司空见惯的PIMPL是什么?用原始指针.std::unique_ptr和std::shared_ptr指向Implementation,会有什么不同?优缺点是什么?读完这篇文章,相 ...
- Tenzing and Random Operations CF1842G 题解
[1] 分析 设 \(m\) 次选的位置分别为 \(b_{1\sim m}\). 于是答案为 \(\mathbb E(\prod\limits_{i = 1}^{n}(a_i + \sum\limit ...
- 从浏览器输入url到回车发生了什么
1. 域名解析,即把域名解析成以为唯一的ip ps:ip是每个网站的对应的一个key,域名是为了语义化,方便使用而设计的 : ps:第一次域名解析需要花费较长的时间,所以一般第一次解析就会把DNS解析 ...
- 云原生周刊:Kubernetes v1.30 一瞥 | 2024.3.25
开源项目推荐 Retina Retina 是一个与云无关的开源 Kubernetes 网络可观测平台,它提供了一个用于监控应用程序运行状况.网络运行状况和安全性的集中中心.它为集群网络管理员.集群安全 ...
- 云原生周刊:Meshery v0.70 发布 | 2024.1.22
开源项目推荐 flux-cluster-template 该项目用于部署由 Flux.SOPS.GitHub Actions.Renovate.Cilium 等支持的 Kubernetes 集群,专注 ...
- 使用 OpenFunction 在任何基础设施上运行 Serverless 工作负载
作者: 霍秉杰:KubeSphere 可观测性.边缘计算和 Serverless 团队负责人,Fluent Operator 和 OpenFunction 项目的创始人,还是多个可观测性开源项目包括 ...
- Oracle的用户如何优雅地达成软件合规目标
企业一旦发展到了一定规模,就会衍生软件100%合规正版化的需求. 而对于使用到Oracle的用户,当然,具体核定的购买数量和off等商务问题,需要客户管理层直接和对应的Oracle销售代表进行商务谈判 ...