大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
*** 创建多线程的三种方式
创新互联主要从事网站设计制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务绿春,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
实现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();
}
}