前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java多线程网页下载代码

java多线程网页下载代码

作者头像
byronhe
发布2021-06-25 10:47:47
5570
发布2021-06-25 10:47:47
举报
文章被收录于专栏:Tech Explorer

小项目,练手的。

1.使用了java.util.concurrent包里的线程池,可以飙升到满带宽,在100M带宽上,可以达到10MB/s。

2.使用了java.nio里的channels,性能比自己缓冲有一些提高。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; import java.util.Calendar; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class HttpDownloader implements Callable { URLConnection connection; FileChannel outputChann; public static volatile int count = 0; public static void main(String[] args) throws Exception { ExecutorService poll = Executors.newFixedThreadPool(100); for (int i = 0; i < 100; i++) { Calendar now = Calendar.getInstance(); String fileName = "../data/" + now.get(Calendar.YEAR) + "年" + (now.get(Calendar.MONTH) + 1) + "月" + now.get(Calendar.DAY_OF_MONTH) + "日--" + i + ".txt"; poll.submit(new HttpDownloader("http://www.sina.com", (new FileOutputStream(fileName)).getChannel())); } poll.shutdown(); long start = System.currentTimeMillis(); while (!poll.isTerminated()) { Thread.sleep(1000); System.out.println("已运行" + ((System.currentTimeMillis() - start) / 1000) + "秒," + HttpDownloader.count + "个任务还在运行"); } } public HttpDownloader(String url, FileChannel fileChannel) throws Exception { synchronized (HttpDownloader.class) { count++; } connection = (new URL(url)).openConnection(); this.outputChann = fileChannel; } @Override public String call() throws Exception { connection.connect(); InputStream inputStream = connection.getInputStream(); ReadableByteChannel rChannel = Channels.newChannel(inputStream); outputChann.transferFrom(rChannel, 0, Integer.MAX_VALUE); // System.out.println(Thread.currentThread().getName() + " completed!"); inputStream.close(); outputChann.close(); synchronized (HttpDownloader.class) { count--; } return null; } }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2011-11-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档