java多线程网页下载代码
来源:互联网 发布:酷我h1头戴评测 知乎 编辑:程序博客网 时间:2024/06/07 12:05
1.使用了java.util.concurrent包里的线程池,可以飙升到满带宽,在100M带宽上,可以达到10MB/s。
2.使用了java.nio里的channels,性能比自己缓冲有一些提高。
import java.io.FileOutputStream;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<String> { 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; } }
<pre style="color: rgb(51, 51, 51); font-size: 13px; line-height: 24px; background-color: rgb(214, 211, 214);">
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<String> { 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; } }
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<String> {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;}@Overridepublic 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;}}
转自:http://www.cnblogs.com/windydays/archive/2011/11/15/2298102.html
0 0
- java多线程网页下载代码
- java多线程网页下载代码
- java多线程网页下载代码
- java多线程网页下载代码
- Java多线程下载代码
- 多线程下载(断点下载)java代码
- 多线程下载,拷贝文件java代码
- Java代码实现多线程下载和断点续传
- Java代码实现多线程下载和断点续传
- Java下载网页代码 & 判断网页是否被百度收录
- 网页代码下载
- Delphi 下载网页代码
- winform下载网页代码
- svn下载网页代码
- 多线程下载 关键代码
- Python3 多线程下载代码
- 多线程下载示例代码
- 多线程下载核心代码
- 电子之TTL和CMOS门电路的区别
- poj3660 Cow Contest
- hdu1016枚举递归-经典DFS-简单回溯入门
- 蓝桥杯2015省赛第5题
- 完全背包问题
- java多线程网页下载代码
- Object类的重要方法
- ProGuard Eclipse Android
- cpu负载过高案例,解决方法记录
- How Broswers Work<二>渲染引擎--CSS解析
- 华为机试题--24点游戏--In Java
- 算法_10 : 图算法_3: 图的遍历、匹配
- android简单实例---------ActionBar的简单使用(三,补充)
- vim中的配置