JAVA技巧-命令行多线程源代码共享.docx

上传人:桔**** 文档编号:545755792 上传时间:2022-11-12 格式:DOCX 页数:7 大小:12.88KB
返回 下载 相关 举报
JAVA技巧-命令行多线程源代码共享.docx_第1页
第1页 / 共7页
JAVA技巧-命令行多线程源代码共享.docx_第2页
第2页 / 共7页
JAVA技巧-命令行多线程源代码共享.docx_第3页
第3页 / 共7页
JAVA技巧-命令行多线程源代码共享.docx_第4页
第4页 / 共7页
JAVA技巧-命令行多线程源代码共享.docx_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《JAVA技巧-命令行多线程源代码共享.docx》由会员分享,可在线阅读,更多相关《JAVA技巧-命令行多线程源代码共享.docx(7页珍藏版)》请在金锄头文库上搜索。

1、 JAVA技巧:命令行多线程源代码共享*该程序采纳10线程掌握下载,速度大大提升*目前支持http协议下载*切换到文件所在名目中*在命令行中输入java Download5 http:/.,回车,即可运行程序*/import java.io.*;import .*;public class Download5 extends Threadprivate long start;private long end;private String urls,name;private int ID;public Download5(String urls,String name,int ID,long s

2、tart,long end)this.start=start;this.end=end;this.urls=urls;this.ID=ID;this.name=name;public void run()trySystem.out.println(“线程“+ID+“启动.“);File file=new File(name);URL url=new URL(urls);URLConnection con=url.openConnection();con.setAllowUserInteraction(true);con.setRequestProperty(“Range“,“bytes=“+s

3、tart+“-“+end);RandomAccessFile rand=new RandomAccessFile(file,“rw“);rand.seek(start);byte b=new byte2048;BufferedInputStream buffer=new BufferedInputStream(con.getInputStream();int n=0;while(n=buffer.read(b,0,b.length)!=-1)rand.write(b,0,n);System.out.println(“线程“+ID+“下载完毕“);buffer.close();rand.clos

4、e();this.interrupt();catch(Exception ee)ee.printStackTrace();public static void main(String args)String urls=args0;int u=urls.lastIndexOf(/);String name=urls.substring(u+1,urls.length();trylong time=System.currentTimeMillis()/1000;URL url=new URL(urls);URLConnection con=url.openConnection();int file

5、length=con.getContentLength();int num=10;int size=filelength/num;Download5 t=null;CountTime count=new CountTime(urls,name,time);count.start();for(int i=0;inum;i+)if(i!=num-1)t=new Download5(urls,name,i+1,size*i,size*(i+1)-1);t.start();elset=new Download5(urls,name,i+1,size*i,filelength);t.start();ca

6、tch(Exception ee)ee.printStackTrace();class CountTime extends Threadprivate String urls,name;private long time;public CountTime(String urls,String name,long time)this.urls=urls;this.name=name;this.time=time;public void run()tryURL url=new URL(urls);URLConnection con=url.openConnection();int fileleng

7、th=con.getContentLength();File f=new File(name);while(f.length()filelength)long LEN=f.length();this.sleep(1000);System.out.println(f.length()-LEN)/1024/1.0+“kb/s“);System.out.println(“文件下载完毕“);System.out.println(“下载所用总时间: “+(System.currentTimeMillis()/1000-time)+“s“);this.interrupt();catch(Exception e)e.printStackTrace();

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 习题/试题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号