*** 創(chuàng)建多線程的三種方式
實(shí)現(xiàn)Runable接口,重寫run方法,new Thread(類對象).start();
**
public class commons extends Thread{
//run是線程的入口點(diǎn)
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; //遠(yuǎn)程路徑
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("/file/tupian/20230130/227722.jpg","D:/d/a.jpg");
ThreadDownload td2=new ThreadDownload("/file/tupian/20230130/227723.jpg","D:/d/b.jpg");
ThreadDownload td3=new ThreadDownload("/file/tupian/20230130/227724.jpg","c.jpg");
//啟動三個(gè)線程
td.start();
td2.start();
td3.start();
}
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。