您好,登錄后才能下訂單哦!
*** 創建多線程的三種方式
實現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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。