java多线程-同时在网上下载多个图片
*** 创建多线程的三种方式
创新互联主要从事网站设计制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务绿春,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
- 继承Thread类,重写Run方法
实现Runnable接口,重写run方法 - 实现callable接口,重写call方法
- 继承Thread类,重写run方法,类名.start()启动线程
实现Runable接口,重写run方法,new Thread(类对象).start();
**public class commons extends Thread{ //run是线程的入口点 public void download(String url,String name) { try { FileUtils.copyURLToFile(new URL(url), new File(name)); } catch (MalformedURLException e) { e.printStackTrace(); System.out.println("不合法的路径"); } catch (IOException e) { e.printStackTrace(); System.out.println("图片下载失败"); } }
}
//开启下载:
public class ThreadDownload extends Thread {
private String url; //远程路径
private String name; //存储名字public ThreadDownload(String url,String name)
{
this.url=url;
this.name=name;
}
public void run()
{
commons wd=new commons();
wd.download(url, name);
}public static void main(String[]args)
{
ThreadDownload td =new ThreadDownload("/upload/otherpic54/227722.jpg","D:/d/a.jpg");
ThreadDownload td2=new ThreadDownload("/upload/otherpic54/227723.jpg","D:/d/b.jpg");
ThreadDownload td3=new ThreadDownload("/upload/otherpic54/227724.jpg","c.jpg");//启动三个线程 td.start(); td2.start(); td3.start();
}
}
分享文章:java多线程-同时在网上下载多个图片
URL分享:http://azwzsj.com/article/giijos.html