1、写一个类继承Thread类
2、在该类重写run方法
3、在run方法里面写代码
4、在main方法里实例化后,调start方法启动
利用多线程下载网页图片
package Thread01;
import org.apache.commons.io.FileUtils;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestThread01extends Thread {
private Stringurl;
private Stringname;
public TestThread01(String url, String name1) {
this.url = url;
this.name = name1;
}
@Override
public void run() {
webDownloader webDownloader =new webDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
TestThread01 t1=new TestThread01("https://img0.baidu.com/it/u=4211746499,1183298678&fm=26&fmt=auto&gp=0.jpg","1.jpg");
t1.start();
}
}
//下载器
class webDownloader{
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
}catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常,downloader方法出现问题");
}
}
}