实现Runnable类练习-多线程下载图片
实现Runnable类练习-多线程下载图片
准备工作
-
在java目录下新建lib目录,下载commons.io包(org.apache)jar包文件放在lib目录下;
-
右键点击lib目录->Add as Library
代码, 需结合继承Thread类练习-多线程下载图片 一起看:
package com.example.multi_thread;
public class TestThread3 implements Runnable {
private String url;
private String imageName;
public TestThread3(String url, String imageName) {
this.url = url;
this.imageName = imageName;
}
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.download(url, imageName);
System.out.println("下载了图片:" + imageName);
}
public static void main(String[] args) {
TestThread3 t1 = new TestThread3("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png", "D:\\old_drive_F\\项目\\2021\\demo_kunag\\src\\main\\java\\com\\example\\multi_thread\\1.jpg");
TestThread3 t2 = new TestThread3("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png", "D:\\old_drive_F\\项目\\2021\\demo_kunag\\src\\main\\java\\com\\example\\multi_thread\\2.jpg");
TestThread3 t3 = new TestThread3("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png", "D:\\old_drive_F\\项目\\2021\\demo_kunag\\src\\main\\java\\com\\example\\multi_thread\\3.jpg");
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
}
}
运行结果
下载了图片:D:\old_drive_F\项目\2021\demo_kunag\src\main\java\com\example\multi_thread\3.jpg
下载了图片:D:\old_drive_F\项目\2021\demo_kunag\src\main\java\com\example\multi_thread\2.jpg
下载了图片:D:\old_drive_F\项目\2021\demo_kunag\src\main\java\com\example\multi_thread\1.jpg