通过继承Thread实现线程
第一步导入commons-io jar包,里面已经封装好了工具,可以直接使用
首先创建一个图片下载方法
class WebDownload{
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
具体实现
public class TestThread extends Thread{
private String url;
private String name;
public TestThread(String url,String name){
this.url=url;
this.name=name;
}
@Override
public void run() {
WebDownload webDownload = new WebDownload();
webDownload.downloader(url,name);
System.out.println("下载了文件为:"+name);
}
public static void main(String[] args) {
TestThread t1 = new TestThread("https://i01piccdn.sogoucdn.com/aa9f24026a5cbd65","1.jpg");
TestThread t2 = new TestThread("https://i01piccdn.sogoucdn.com/166d76632f06b0ce","2.jpg");
TestThread t3 = new TestThread("https://i01piccdn.sogoucdn.com/aa9f24026a5cbd65","3.jpg");
TestThread t4 = new TestThread("https://i04piccdn.sogoucdn.com/d961201dbd742bbc","4.jpg");
t1.start();
t2.start();
t3.start();
t4.start();
}
}