当前位置:网站首页>3.练习Thread
3.练习Thread
2022-08-09 09:23:00 【过来我的小熊】
Thread,网图下载
package com.xiancheng.demo;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
// 练习Thread,实现多线程同步下载图片
// 继承Thread类
// 重写run()方法
// 运行start()方法
public class TestThread2 extends Thread{
private String url; // 网络图片地址
private String name; // 保存的文件地址
public TestThread2(String url,String name){
this.url = url;
this.name = name;
}
public TestThread2() {
}
// 下载图片线程的执行体
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url, name);
System.out.println("下载的文件名为:" + name);
}
public static void main(String[] args) {
TestThread2 testThread2 = new TestThread2("https://tse2-mm.cn.bing.net/th/id/OIP-C.2rQ25qnSMQHXGcHz3Rp2pAHaEo?w=277&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7","2.jpg");
TestThread2 testThread3 = new TestThread2("https://tse2-mm.cn.bing.net/th/id/OIP-C.2rQ25qnSMQHXGcHz3Rp2pAHaEo?w=277&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7","3.jpg");
TestThread2 testThread4 = new TestThread2("https://tse2-mm.cn.bing.net/th/id/OIP-C.2rQ25qnSMQHXGcHz3Rp2pAHaEo?w=277&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7","4.jpg");
testThread2.start();
testThread3.start();
testThread4.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方法出现问题");
}
}
}
边栏推荐
猜你喜欢
中国打造国产“谷歌地球”清晰度吓人
Do you know the principles of test cases and how to write defect reports?
Swap swap partition
How much do you know about the mobile APP testing process specifications and methods?
使用图新地球无法加载谷歌地球的完美解决方法(附软件下载)
本体开发日记05-努力理解SWRL(Built-Ins)
【百日行动】炎炎夏日安全不松懈 消防培训“加满”安全知识“油”
QT sets the icon of the exe executable
Django实现对数据库数据增删改查(一)
国产谷歌地球,地形分析秒杀同款地图软件
随机推荐
学习双向链表的心得与总结
AES/ECB/PKCS5Padding加解密
These 12 GIS software are better than the other
How much do you know about the mobile APP testing process specifications and methods?
MySQL锁
秒拍app分析
本体开发日记05-努力理解SWRL(中)
测试计划包括哪些内容?目的和意义是什么?
GBase数据库产生迁移工具假死的原因是什么?
这12个GIS软件一个比一个好用
【百日行动】炎炎夏日安全不松懈 消防培训“加满”安全知识“油”
JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
一篇文章让你彻底搞懂关于性能测试常见术语的定义
Venture DAO Industry Research Report: Macro and Classic Case Analysis, Model Summary, Future Suggestions
MySQL Leak Detection and Filling (2) Sorting and Retrieval, Filtering Data, Fuzzy Query, Regular Expression
lateral view explode的另一种实现方式
WAVE SUMMIT 2022深度学习开发者峰会
关于链表的几道大厂面试题
Do you know the basic process and use case design method of interface testing?
makefile学习-解决目标文件输出路径问题