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多线程网页下载代码的更多相关文章

  1. Java多线程的下载器(1)

    实现了一个基于Java多线程的下载器,可提供的功能有: 1. 对文件使用多线程下载,并显示每时刻的下载速度. 2. 对多个下载进行管理,包括线程调度,内存管理等. 一:单个文件下载的管理 1. 单文件 ...

  2. 用 python 实现一个多线程网页下载器

    今天上来分享一下昨天实现的一个多线程网页下载器. 这是一个有着真实需求的实现,我的用途是拿它来通过 HTTP 方式向服务器提交游戏数据.把它放上来也是想大家帮忙挑刺,找找 bug,让它工作得更好. k ...

  3. java多线程断点下载原理(代码实例演示)

    原文:http://www.open-open.com/lib/view/open1423214229232.html 其实多线程断点下载原理,很简单的,那么我们就来先了解下,如何实现多线程的断点下载 ...

  4. Java多线程断点下载文件

    Java实现断点续传+多线程下载 如下代码所示,每一步都有注解 思路: 通过URL连接到服务器上要下载的文件,得到文件的大小: 算出每条线程下载的开始位置和结束位置,例如,有两条线程下载100Byte ...

  5. java 多线程安全问题-同步代码块

    /* 多线程的安全问题: while(true) { if(tick>0) { //线程0,1,2,3在余票为1时,都停滞在这里,之后分别获得CPU执行权,打印出0,-1,-2等错票 Syste ...

  6. java 多线程断点下载功能

    import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.Rand ...

  7. java多线程批量下载文件

    多线程下载文件 平时开发中有时会用到文件下载,为了提高文件的下载速率,采用多线程下载能够达到事半功倍的效果: package test; /** * 文件下载类 * @author luweichen ...

  8. Java多线程断点下载

    public static class DownloadThread extends Thread{ private int threadId; private int startIndex; pri ...

  9. java 多线程断点下载demo

    源码链接 import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java ...

  10. java的报表下载代码excel

    /** * 汇总报表数据下载 * */ private ModelAndView exportSummaryDatadown(HttpServletRequest request, HttpServl ...

随机推荐

  1. 消息队列的对比测试与RocketMQ使用扩展

    消息队列的对比测试与RocketMQ使用扩展     本文的主要内容包括以下几个方面: 原有的消息技术选型 RocketMQ与kafka 测试对比 如何构建自己的消息队列服务 RocketMQ扩展改造 ...

  2. 为何不要随便用from xx import *

    新单位,新工作,远程办公,你想想有多麻烦吧. 一块块看每个模块的详细功能. 看到一个函数,返回值也是一个函数,这本来没啥难的,但是文件里只出现一次.怎么都找不到来自哪个文件,后来全项目搜才找到,原来来 ...

  3. 知识点考古:php5的面向对象学习笔记

    闲来无事翻看以前收藏的资料,考古到保存的这篇文章对php的OOP的整理还很系统.原链接已经打不开(http://www.cublog.cn/u/17686/showart.php?id=146562) ...

  4. WPF 实现一个吃豆豆的Loading加载动画

    运行的效果如下 先引入一下我们需要的库 在nuget上面搜一下"expression.Drawing",安装一下这个包 我们再创建一个Window,引入一下这个包的命名空间 我们设 ...

  5. Azure 入门系列 (第三篇 Publish Web Application to VM)

    本系列 这个系列会介绍从 0 到 1 搭建一个 Web Application 的 Server. 间中还会带上一些真实开发常用的功能. 一共 6 篇 1. Virtual Machine (VM) ...

  6. Java 动态编译工具 Janino 和 Liquor 差别

    如果你只要 Java7 及以下的语法支持,建议 Janino.如果要你想更全的 Java8.Java11.Java17.Java21 等语法,可以选 Liquor. 1.它们相同的地方 提供的相似的能 ...

  7. Sqoop简介安装及使用

    Sqoop简介 sqoop 是 apache 旗下一款"Hadoop 和关系数据库服务器之间传送数据"的工具. 核心的功能有两个: 导入.迁入 导出.迁出 导入数据:MySQL,O ...

  8. 【赵渝强老师】NoSQL数据库之Cassandra基础

    一.Cassandra简介 Cassandra是一个混合型的非关系的数据库,类似于Google的BigTable.其主要功能比Dynamo (分布式的Key-Value存储系统)更丰富,但支持度却不如 ...

  9. 【赵渝强老师】MySQL高可用架构:MHA

    MHA(Master HA)是一款开源的 MySQL 的高可用程序,它为 MySQL 主从复制架构提供了 automating master failover 功能.MHA 在监控到 master 节 ...

  10. [python] 基于PyOD库实现数据异常检测

    PyOD是一个全面且易于使用的Python库,专门用于检测多变量数据中的异常点或离群点.异常点是指那些与大多数数据点显著不同的数据,它们可能表示错误.噪声或潜在的有趣现象.无论是处理小规模项目还是大型 ...