现在的位置: 首页 > 综合 > 正文

Android中的多线程机制

2014年11月24日 ⁄ 综合 ⁄ 共 2937字 ⁄ 字号 评论关闭

Android中使用多线程机制,可以实现文件下载,如果用户出现意外而导致下载未完成的情况下,当用户下次继续下载该文件时,系统会从该用户上一次下载的大小继续下载,从而提高了用户的信任度,体搞了效率。

下面是一个例子:

package com.hbsi.csdn.multile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class test {

public static final String path = "http://192.168.64.138:8080/youdaocidian.exe";
public static int threadcount;

public static void main(String[] args) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");// 设置请求方式
conn.setConnectTimeout(5000); // 设置获取响应码的时间

int len = conn.getContentLength(); // 获取相应数据的长度
System.out.println("下载的文件的长度是:" + len);

// 在客户端创建一个大小跟服务区端文件一样的资源
File file = new File("youdao.exe");
RandomAccessFile randomfile = new RandomAccessFile(file, "rwd");
randomfile.setLength(len);

// 假设开启5个子线程
int blocksize = len / 5;
threadcount = 0;
for (int i = 1; i <= 5; i++) {
int startsize = (i - 1) * blocksize;
int endsize = (i) * blocksize - 1;
if (i == 5) {
endsize = len;
}
new Thread(new DownLoadTask(i, startsize, endsize)).start();
// 解决所有的子线程全部执行完毕后,在把所有的i.txt文件删除
}

}

}

class DownLoadTask implements Runnable {
private int id, startsize, endsize;

public DownLoadTask(int id, int startsize, int endsize) {
super();
this.id = id; // 线程的id
this.startsize = startsize;
this.endsize = endsize;
}

@Override
public void run() {

try {
// 每一个线程创建执行时都创建一个id.txt的文件,这个文件用来记载当前线程的下载进度。
File idfile = new File(id + ".txt");
if (idfile.exists()) {
FileInputStream fis = new FileInputStream(idfile);
ByteArrayOutputStream boas = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
boas.write(buffer, 0, len);
}
fis.close();
boas.close();
byte[] result = boas.toByteArray();
String numberstr = new String(result);
if (numberstr != null && (!"".equals(numberstr))) {
int startposition = Integer.parseInt(numberstr);
if (startposition > startsize) {
startsize = startposition; // 重新指定下载的开始位置
}
}
}

URL url = new URL(test.path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");// 设置请求方式
conn.setConnectTimeout(5000); // 设置获取响应码的时间
// 指定当前线程从服务器上的哪个位置下载
conn.setRequestProperty("Range", "bytes=" + startsize + "-"
+ endsize);
System.out.println("文件下载的开始位置:" + startsize + "     结束位置:"
+ endsize);
InputStream is = conn.getInputStream();
File file = new File("youdao.exe");
RandomAccessFile randomfile = new RandomAccessFile(file, "rwd");
randomfile.seek(startsize);
byte[] buffer = new byte[1024];
int len = 0;
int tatol = 0;
while ((len = is.read(buffer)) != -1) {
randomfile.write(buffer, 0, len);
tatol += len;
FileOutputStream idfos = new FileOutputStream(idfile);
idfos.write(("" + (startsize + tatol)).getBytes());
idfos.flush();
idfos.close();
}
randomfile.close();
is.close();
System.out.println("线程  " + id + "      下载完毕");

/*
* if(idfile.exists()){ idfile.delete(); }
*/
synchronized (test.class) {
test.threadcount++;
if (test.threadcount >= 5) {
for (int i = 1; i <= 5; i++) {
File deletefile = new File(i + ".txt");
deletefile.delete();
}
}
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

【上篇】
【下篇】

抱歉!评论已关闭.