Java多线程(三种方法创建:Thread、Runable、Callable)实现简单网络图片下载
需要导入的架包
-
下载commons-IO-2.7版本
-
复制commons-io-2.7.jar到IDEA
-
导入IDEA,配置属性
-
配置后效果
Thread
- 引入的包
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
- 源代码
//练习Thread,实现多线程同步下载图片;
public class TextThread extends Thread{
private String url;
private String name;
public TextThread(String url, String name){
this.url = url;
this.name =name;
}
@Override
public void run() {
// super.run();
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载的文件名为:"+name);
}
public static void main(String[] args) {
TextThread d1 = new TextThread("https://img-blog.csdnimg.cn/20200725172843694.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjgwMzI0,size_16,color_FFFFFF,t_70","h1.jpg");
TextThread d2 = new TextThread("https://img-blog.csdnimg.cn/20200725172843694.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjgwMzI0,size_16,color_FFFFFF,t_70","h2.jpg");
TextThread d3 = new TextThread("https://img-blog.csdnimg.cn/20200725172843694.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjgwMzI0,size_16,color_FFFFFF,t_70","h3.jpg");
d1.start();
d2.start();
d3.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方法问题出现异常");
}
}
}
Runable
- 引入的包
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
- 源代码
public class TextRunable implements Runnable{
private String url;
private String name;
public TextRunable(String url, String name){
this.url = url;
this.name =name;
}
@Override
public void run() {
// super.run();
WebDownloader1 webDownloader = new WebDownloader1();
webDownloader.downloader(url,name);
System.out.println("下载的文件名为:"+name);
}
public static void main(String[] args) {
TextRunable d1 = new TextRunable("https://img-blog.csdnimg.cn/20200725172843694.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjgwMzI0,size_16,color_FFFFFF,t_70","h1.jpg");
TextRunable d2 = new TextRunable("https://img-blog.csdnimg.cn/20200725172843694.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjgwMzI0,size_16,color_FFFFFF,t_70","h2.jpg");
TextRunable d3 = new TextRunable("https://img-blog.csdnimg.cn/20200725172843694.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjgwMzI0,size_16,color_FFFFFF,t_70","h3.jpg");
/*Thread thread1 = new Thread(d1);
Thread thread2 = new Thread(d2);
Thread thread3 = new Thread(d3);
thread1.start();
thread2.start();
thread3.start();*/
new Thread(d1).start();
new Thread(d2).start();
new Thread(d3).start();
}
}
//下载器
class WebDownloader1{
//下载方法
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方法问题出现异常");
}
}
}
Callable
- 引入的包
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
- 源代码
public class TextCallable implements Callable<Boolean> {
private String url;
private String name;
public TextCallable(String url,String name){
this.url = url;
this.name =name;
}
@Override
public Boolean call() {
// super.run();
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载的文件名为:"+name);
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TextCallable d1 = new TextCallable("https://img-blog.csdnimg.cn/20200725172843694.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjgwMzI0,size_16,color_FFFFFF,t_70","h1.jpg");
TextCallable d2 = new TextCallable("https://img-blog.csdnimg.cn/20200725172843694.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjgwMzI0,size_16,color_FFFFFF,t_70","h2.jpg");
TextCallable d3 = new TextCallable("https://img-blog.csdnimg.cn/20200725172843694.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MjgwMzI0,size_16,color_FFFFFF,t_70","h3.jpg");
//创建执行服务
ExecutorService ser = Executors.newFixedThreadPool(3) ;
//提交执行
Future<Boolean> r1 = ser.submit(d1);
Future<Boolean> r2 = ser.submit(d2);
Future<Boolean> r3 = ser.submit(d3);
//获取结果
boolean rs1 = r1.get();
boolean rs2 = r2.get();
boolean rs3 = r3.get();
//关闭服务
ser.shutdownNow();
}
}
//下载器
class WebDownloader3{
//下载方法
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方法问题出现异常");
}
}
}
运行结果
- Thread
- Runable
- Callable