*** 创建多线程的三种方式
实现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("https://cache.yisu.com/upload/information/20200311/58/227722.jpg","D:/d/a.jpg");
ThreadDownload td2=new ThreadDownload("https://cache.yisu.com/upload/information/20200311/58/227723.jpg","D:/d/b.jpg");
ThreadDownload td3=new ThreadDownload("https://cache.yisu.com/upload/information/20200311/58/227724.jpg","c.jpg");
//启动三个线程
td.start();
td2.start();
td3.start();
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。