java多线程下载

 1 import java.io.BufferedInputStream;
2 import java.io.File;
3 import java.io.IOException;
4 import java.io.RandomAccessFile;
5 import java.net.URL;
6 import java.net.URLConnection;
7
8 public class DLRunnable implements Runnable {
9 static final int BUF_SIZE = 1024;
10 URL url = Task.url;
11 File file = Task.file;
12 long pStart, pEnd, pCur, readLen = 0;
13 int id, isDone = 0, isResume = 0;
14
15 public DLRunnable(int id, long start, long end) {
16 this.pStart = start;
17 this.pEnd = end;
18 this.id = id;
19 this.pCur = this.pStart;
20 }
21
22 public void run() {
23 try {
24 url = new URL("http://dl_dir.qq.com/qqfile/qq/QQ2011/QQ2011.exe");
25 URLConnection conn = url.openConnection();
26 conn.setAllowUserInteraction(true);
27 BufferedInputStream bins = new BufferedInputStream(
28 conn.getInputStream());
29 RandomAccessFile fos = null;
30 if (isResume == 0) {
31 conn.setRequestProperty("Range", "bytes=" + pStart + "-" + pEnd);
32 fos = new RandomAccessFile(file, "rw");
33 fos.seek(pStart);
34 System.out.println("线程" + id + "开始下载...");
35 pCur = pStart;
36 } else {
37 conn.setRequestProperty("Range", "bytes=" + pCur + "-" + pEnd);
38 fos = new RandomAccessFile(file, "rw");
39 fos.seek(pCur);
40 System.out.println("线程" + id + "继续开始下载...");
41 }
42 long startTime = System.currentTimeMillis();
43 while (pCur < pEnd) {
44 byte buf[] = new byte[BUF_SIZE];
45 int len = bins.read(buf, 0, BUF_SIZE);
46 if (len == -1)
47 break;
48 fos.write(buf, 0, len);
49 pCur += len;
50 if (pCur > pEnd) {
51 readLen += (pEnd - (pCur - len) + 1);
52 } else {
53 readLen += len;
54 }
55 }
56 long totalTime = System.currentTimeMillis() - startTime;
57 isDone = 1;
58 System.out.println("线程" + id + "下载完毕!大小" + readLen + "字节"
59 + totalTime + "ms");
60 bins.close();
61 fos.close();
62 Task.runnableArray.remove(this);
63 Task.threadArray.remove(Task.threadArray.get(id-1));
64 } catch (IOException e) {
65 e.printStackTrace();
66 }
67 }
68 }

实现runnable接口

 1 import java.io.File;
2 import java.io.IOException;
3 import java.net.MalformedURLException;
4 import java.net.URL;
5 import java.net.URLConnection;
6 import java.util.ArrayList;
7
8 public class Task {
9 static int THREAD_NUM = 3;
10 static ArrayList<Thread> threadArray = new ArrayList<Thread>();
11 static ArrayList<DLRunnable> runnableArray = new ArrayList<DLRunnable>();
12 static URL url = null;
13 static File file = null;
14 static long blockLen, totalLen, startTime, totalTime;
15
16 private void initTask() {
17 try {
18 url = new URL("http://dl_dir.qq.com/qqfile/qq/QQ2011/QQ2011.exe");
19 file = new File("D:\\Users\\Meng\\Downloads\\QQ2011.exe");
20 URLConnection conn = url.openConnection();
21 long totalLen = conn.getContentLength();
22 System.out.println("文件总大小:" + totalLen + "字节");
23 if (totalLen == -1)
24 return;
25 blockLen = (long) Math.ceil((double) totalLen / THREAD_NUM);
26 System.out.println("每块长度:" + blockLen + "字节");
27 } catch (MalformedURLException e) {
28 e.printStackTrace();
29 } catch (IOException e) {
30 e.printStackTrace();
31 }
32 }
33
34 private void startTask() {
35 System.out.println("开始下载...");
36 startTime = System.currentTimeMillis();
37 for (int i = 0; i < THREAD_NUM; i++) {
38 DLRunnable dlr = new DLRunnable(i + 1, blockLen * i, blockLen
39 * (i + 1));
40 runnableArray.add(dlr);
41 Thread dlt = new Thread(dlr);
42 threadArray.add(dlt);
43 dlt.start();
44 }
45 }
46
47 private void stopTask(){
48 System.out.println("下载中断...");
49 for (Thread dlt : threadArray) {
50 threadArray.remove(dlt);
51 dlt = null;
52 }
53 }
54
55 private void resumeTask() {
56 System.out.println("继续下载...");
57 for (DLRunnable dlr : runnableArray) {
58 dlr.isResume=1;
59 Thread dlt = new Thread(dlr);
60 threadArray.add(dlt);
61 dlt.start();
62 }
63 }
64
65 public static void main(String[] args) throws InterruptedException {
66 Task task = new Task();
67 task.initTask();
68 task.startTask();
69 new Thread() {
70 public void run() {
71 while (true) {
72 if (runnableArray.isEmpty()&&threadArray.isEmpty()) {
73 totalTime = System.currentTimeMillis() - startTime;
74 System.out.println("下载总用时:" + totalTime + "ms,平均速度:"
75 + totalLen / totalTime + "kb/s");
76 break;
77 }
78 }
79 }
80 }.start();
81 Thread.sleep(10000);
82 task.stopTask();
83 Thread.sleep(5000);
84 task.resumeTask();
85 }
86 }

task类

-------------------------------~问世间情为何物,敲敲代码停不住~ -------------------------------
原文地址:https://www.cnblogs.com/bigmengzi/p/2394343.html