*** 3つの方法がマルチスレッド作成します
- Threadクラスを継承し、Runメソッドをオーバーライドし
Runableインタフェースを実現するために、runメソッドをオーバーライドします - 呼び出し可能インターフェース書き換えメソッド呼び出しを実現
- Threadクラスを継承し、runメソッド、クラス名.startを(上書き)スレッドを開始
-
インターフェイス、オーバーライドrunメソッド、新しいスレッド(クラスオブジェクト).start()を実装Runable;
**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("图片下载失败"); } }
}
:ダウンロード開き//
スレッド{拡張ThreadDownloadパブリッククラス
プライベート文字列のURLを; //リモートパス
プライベート文字列名を; //ストア名公共ThreadDownload(文字列のURL、文字列名)
{
this.url = URL。
this.name =名前;
}
公共ボイドラン()
{
コモンWD =新しいコモンズ();
wd.download(URL、名);
}パブリック静的無効メイン(文字列[] args)
{
ThreadDownloadのTD =新しいThreadDownload(」http://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&ie=utf-8&in=24401&cl=2&lm= -1&ST = -1&step_word =&RN = 1&CS =&LN = 1998&FMQ = 1402900904181_R&IC = 0、S =&SE = 1&SME = 0&タブ=&幅=&高さ=&顔= 0&さ=&ISTYPE = 2&IST =&JIT =&FR = ALA及びALA = 1&alatpl =他人&POS = 1&PN = 3&ワード= %はE5%9B%%E7の%89%87%20%E5%8A%A8%E6%のBC%のAB%E5%以下の8D%のA1の%E9%の80%の9A&ディ= 213510&OS = 4129833545,253122553&PI = 0&OBJURL =のhttp%3A BE %2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201209%2F02%2F20120902160411_ik2UH.jpeg "" D:/d/a.jpg「)。
ThreadDownload TD2 =新ThreadDownload(」http://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&ie=utf-8&in=24401&cl=2&lm=-1&st=-1&step_word=&rn=1&cs=&ln=1998&fmq=1402900904181_R&ic=0&s= &SE = 1&SME = 0&タブ=&幅=&高さ=&顔= 0である=&ISTYPE = 2&IST =&JIT =&FR = ALA及びALA = 1&alatpl =他人&POS = 1&PN = 2&ワード=%E5%9B%BE%のE7の%89%87%20%E5%以下の(a) %A8%E6%のBC%AB%のE5%の8D%のA1%のE9%80%の9A&ディ= 237600&OS = 1039188367,1392108594&PI = 0&OBJURL =のhttp%3A%2F%2Fimg.alicdn.com%2Fimgextra%2Fi1%2F3498538608%2FTB2BY1Be8HH8KJjy0FbXXcqlpXa_! 3498538608から0-daren.jpg "" D:/d/b.jpg ");
ThreadDownload TD3 =新しいThreadDownload("http://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&ie=utf-8&in=24401&cl=2&lm=-1&st=-1&step_word=&rn=1&cs=&ln=1998&fmq=1402900904181_R&ic=0&s= &SE = 1&SME = 0&タブ=&幅=&高さ=&顔= 0である=&ISTYPE = 2&IST =&JIT =&FR = ALA及びALA = 1&alatpl =他人&POS = 1&PN = 0&ワード=%E5%9B%BE%のE7の%89%87%20%E5%以下の(a) %A8%E6%のBC%AB%のE5%の8D%のA1%のE9%80%9A&ディ= 83710&OS = 1459185365,1117709152&PI = 0&OBJURL =のhttp%3A%2F%2Fimg.52z.com%2Fupload%2Fnews%2Fimage%2F20180129%2F20180129082229_14849 .JPG "" c.jpg「)。//启动三个线程 td.start(); td2.start(); td3.start();
}
}