您的位置:首页 > 编程语言 > Java开发

java多线程加速下载

2016-04-27 22:23 405 查看
开启多个线程在一定程度上可以提高下载速度,单位时间内,服务器的cpu更多资源给你了,但是:

1. 不是线程开的越多 下载越快 

2. 受真实带宽的影响 

3. 受服务器带宽的影响  

javase实现多线程下载



  步骤:

1、在客户端创建一个与服务器端大小一样的空白文件
2、设置子线程的个数

    3、计算每个子线程下载的数据块大小和下载起始位置、结束位置

    4、创建子线程开始下载数据
5、得到每个子线程都下载完成的标记

代码:

MultiThreadDownLoader.java:

import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class MultiThreadDownLoader {

//2、使用的子线程的个数
private static int threadCount =3;

/**
* @param args
*/
public static void main(String[] args) {
try {
String path = "http://192.168.22.136:8080/sogou.exe";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);

int code = conn.getResponseCode();
if(code == 200){
int length =	conn.getContentLength();
//1、在客户端创建一个与服务端文件一样大小的文件
RandomAccessFile file = new RandomAccessFile("temp.exe", "rw");
file.setLength(length);
//3、每个子线程下载数据块 ,下载的起始位置和结束位置
int blockSize = length/threadCount;

// threadId * blockSize  ---- (threadId+1)* blockSize -1
for(int threadId =0; threadId < threadCount; threadId++){
//下载的起始位置和结束位置
int startIndex = threadId * blockSize;
int endIndex = 0;

if(threadId != (threadCount -1)){
endIndex = (threadId + 1) * blockSize - 1;
}else{
endIndex = length-1;
}

//开启子线程下载数据
new ThreadDownLoader(path, startIndex, endIndex, threadId, threadCount).start();

}

}else{
//抛出异常
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

ThreadDownLoader.java:

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class ThreadDownLoader extends Thread {

private String path;
private int startIndex;
private int endIndex;
private int threadId;
private int threadCount;
//正在运行的线程个数
private static  int runningThreadCount = 3;
ThreadDownLoader(String path,int startIndex,int endIndex,int threadId,int threadCount){
this.path = path;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
this.threadCount = threadCount;

}

@Override
public void run() {
downLoad(path,startIndex,endIndex,threadId,threadCount);
}

public void downLoad(String path,int startIndex,int endIndex,int threadId,int threadCount)
{
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);
//设置子线程请求数据的范围
conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
int code = conn.getResponseCode();
if(code == 206){//请求部分数据

InputStream is = conn.getInputStream();
RandomAccessFile file = new RandomAccessFile("temp.exe","rw");
//指定从哪个位置开始写数据
file.seek(startIndex);

byte[] buffer = new byte[1024];
int len = -1;
while((len = is.read(buffer)) != -1){
file.write(buffer, 0, len);
}

file.close();
System.out.println("线程"+threadId+"下载完成...............");
synchronized (ThreadDownLoader.this) {
runningThreadCount--;
if(runningThreadCount == 0){
System.out.println("文件下载完成...............");
}
}
}
} catch (Exception e) {
System.out.println("线程"+threadId+"下载失败...............");
e.printStackTrace();
}

}

}


多线程下载之断点续传

思路:

   1、实时记录线程下载的位置;

   2、接上一次下载的位置继续下载;

代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;

public class ThreadDownLoader extends Thread {

private String path;
private int startIndex;
private int endIndex;
private int threadId;
private int threadCount;
//正在运行的线程个数
private static  int runningThreadCount = 3;
ThreadDownLoader(String path,int startIndex,int endIndex,int threadId,int threadCount){
this.path = path;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
this.threadCount = threadCount;

}

@Override
public void run() {
downLoad(path,startIndex,endIndex,threadId,threadCount);
}

public void downLoad(String path,int startIndex,int endIndex,int threadId,int threadCount)
{
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);

//获取上一次下载的位置,接着下载
File threadFile = new File(threadId+".txt");
if(threadFile.exists() && threadFile.length() > 0){
FileReader fr = new FileReader(threadFile);
BufferedReader br = new BufferedReader(fr);
String position = br.readLine();
//设置子线程请求数据的范围
conn.setRequestProperty("Range", "bytes="+position+"-"+endIndex);
startIndex = Integer.valueOf(position);
}else{
//设置子线程请求数据的范围
conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
}

int code = conn.getResponseCode();
if(code == 206){//请求部分数据

InputStream is = conn.getInputStream();
RandomAccessFile file = new RandomAccessFile("temp.exe","rw");
//指定从哪个位置开始写数据
file.seek(startIndex);

byte[] buffer = new byte[1024*1024];
int len = -1;
int total = 0;
while((len = is.read(buffer)) != -1){
file.write(buffer, 0, len);
total = total + len;
int currentPosition = startIndex + total;
//能够即时的把数据写到数据
RandomAccessFile f = new RandomAccessFile(threadId+".txt", "rwd");
f.write((currentPosition+"").getBytes());
f.close();

}

file.close();
System.out.println("线程"+threadId+"下载完成...............");
synchronized (ThreadDownLoader.this) {
runningThreadCount--;
if(runningThreadCount == 0){
System.out.println("文件下载完成...............");
}
}
}
} catch (Exception e) {
System.out.println("线程"+threadId+"下载失败...............");
e.printStackTrace();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息