package com.ai.demo01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestThread2 implements Runnable{
private String url;
private String name;
public TestThread2(String url,String name){
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
TestThread2 t1 = new TestThread2("https://tse3-mm.cn.bing.net/th/id/OIP-C.TUKYhAWfxGhNf8oecCTx2wHaEK?w=271&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7","1.jpg");
TestThread2 t2 = new TestThread2("https://tse3-mm.cn.bing.net/th/id/OIP-C.TUKYhAWfxGhNf8oecCTx2wHaEK?w=271&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7","2.jpg");
TestThread2 t3 = new TestThread2("https://tse3-mm.cn.bing.net/th/id/OIP-C.TUKYhAWfxGhNf8oecCTx2wHaEK?w=271&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7","3.jpg");
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).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方法出现问题");
}
}
}
package com.ai.demo01;
public class TestThread3 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码--"+i);
}
}
public static void main(String[] args) {
TestThread3 testThread3 = new TestThread3();
new Thread(testThread3).start();
for (int i = 0; i < 20; i++) {
System.out.println("我在学习多线程--"+i);
}
}
}