Android中的多线程机制

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();
}
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值