6
回答
线程突然停止不动了,求助啊啊啊啊啊啊
终于搞明白,存储TCO原来是这样算的>>>   

我用多线程下载文件的时候,有的线程突然就停止不动了,而且每次都会停止,这是为什么?

<无标签>
举报
SONAIS
发帖于5年前 6回/558阅

以下是问题补充:

  • @SONAIS :private class DownLoadChild extends Thread{ private static final int bufsize=1024*5; int downsize; int startposition; int curposition; int endposition; private File file; private boolean finished=false; private boolean iscancel=false; URL url; public DownLoadChild(URL url,File file,int start,int end){ this.url=url; this.file=file; startposition=start; curposition=start; endposition=end; } public void run(){ BufferedInputStream bis = null; RandomAccessFile fos = null; byte[] buf = new byte[bufsize]; URLConnection con = null; try{ con = url.openConnection(); con.setAllowUserInteraction(true); //设置当前线程下载的起点,终点 con.setRequestProperty("Range", "bytes=" + startposition + "-" + endposition); //使用java中的RandomAccessFile 对文件进行随机读写操作 fos = new RandomAccessFile(file, "rw"); //设置开始写文件的位置 fos.seek(startposition); bis = new BufferedInputStream(con.getInputStream()); while (curposition < endposition&&!iscancel) { int len = bis.read(buf, 0, bufsize); if (len == -1) { break; } fos.write(buf, 0, len); curposition = curposition + len; if (curposition > endposition) { downsize+=len - (curposition - endposition) + 1; } else { downsize+=len; } } //下载完成设为true this.finished = true; bis.close(); fos.close(); }catch (IOException e) { } } public boolean isFinished(){ return finished; } public void Cancel(){ this.iscancel=true; } public int getDownSize(){ return downsize; } } (5年前)
顶部