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. ERROR: Could not determine java version from 'JavaVersion.VERSION_1_8'.

    写法原为: compileOptions { sourceCompatibility 'JavaVersion.VERSION_1_8' targetCompatibility 'JavaVersio ...

  2. LLM 写标书

    云孚科技 有提到标书写作 https://www.sohu.com/a/726319389_121119682 https://www.aihub.cn/tools/writing/yfwrite/ ...

  3. Kubelet证书自动续签(为 kubelet 配置证书轮换)

    1.概述 Kubelet 使用证书进行 Kubernetes API 的认证. 默认情况下,这些证书的签发期限为一年,所以不需要太频繁地进行更新. Kubernetes 包含特性 Kubelet 证书 ...

  4. 深入理解Argo CD工作原理

    1. ArgoCD 的架构 ArgoCD 是一个 Kubernetes 原生的持续交付工具,它通过监控 Git 仓库中的应用定义来自动部署应用到 Kubernetes 集群.其核心架构由以下几个关键组 ...

  5. Unity中的三种渲染路径

    Unity中的渲染路径 Unity的渲染路径 在Unity里,渲染路径(Rendering Path)决定了光照是如何应用到Unity Shader中的.因此,我们只有为Shader正确地选择和设置了 ...

  6. Redis系列补充:聊聊布隆过滤器(go语言实践篇)

    ★ Redis24篇集合 1 介绍 布隆过滤器(Bloom Filter)是 Redis 4.0 版本之后提供的新功能,我们一般将它当做插件加载到 Redis Service服务器中,给 Redis ...

  7. Vite2 如何设置打包后文件名

    vite.config.js build: { rollupOptions: { output: { entryFileNames: `assets/[name].${timestamp}.js`, ...

  8. pytorch: grad can be implicitly created only for scalar outputs

    运行这段代码 import torch import numpy as np import matplotlib.pyplot as plt x = torch.ones(2,2,requires_g ...

  9. dotnet定义扩展方法

    // 扩展方法 // 1.创建静态类静态方法 2. 在静态方法中参数中使用 this 关键字指定需要扩展的类 // 密封类 不能直接继承,通过扩展方法,拿到父类的属性和方法进行扩展补充

  10. 1. java + react 实现 HRM

    1. 云服务的三种方式 1.1 IAAS 基础设施即服务 ,只会提供基础的设施,eg:服务器,网络等 : 1.2 PAAS 平台即服务 ,提供平台,可以把自己写好的代码部署到平台上 : 1.3 SAA ...