溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java 多線程-同時在網(wǎng)上下載多個圖片

發(fā)布時間:2020-07-08 10:08:52 來源:網(wǎng)絡(luò) 閱讀:482 作者:wx5d21d5e6e5ab1 欄目:編程語言

*** 創(chuàng)建多線程的三種方式

  • 繼承Thread類,重寫Run方法
    實現(xiàn)Runnable接口,重寫run方法
  • 實現(xiàn)callable接口,重寫call方法
  • 繼承Thread類,重寫run方法,類名.start()啟動線程
  • 實現(xiàn)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();

    }
    }

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI