当前位置:网站首页>创建线程的三种方式
创建线程的三种方式
2022-04-23 05:41:00 【hanyc..】
利用url下载网图的类:
package creatthread;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//需要commons-io-2.6 jar包
public class WebPicDownLoad {
public void download(String url, String pathName) {
try {
FileUtils.copyURLToFile(new URL(url), new File(pathName));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常,download方法出现问题!");
}
}
}
1、继承Thread类:
package creatthread;
public class DownLoadContentWithThread extends Thread {
//1、继承Thread类
private String url;
private String pathName;
public DownLoadContentWithThread(String url, String pathName) {
this.url = url;
this.pathName = pathName;
}
//2、重写run方法
@Override
public void run() {
WebPicDownLoad wpc = new WebPicDownLoad();
wpc.download(this.url, this.pathName);
System.out.println("下载" + pathName + "完成!");
}
public static void main(String[] args) {
//3、创建目标对象
DownLoadContentWithThread dt1 = new DownLoadContentWithThread("https://mmbiz.qpic.cn/mmbiz_jpg/xq9PqibkVAzr9bLeYf7ia7YrKlfqxlKIJz7P32mLI6RfWAsQ11Zr1NWZ40XDNyy99Q7rm6wau50mQZjTXvvjLwYg/640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1&wx_co=1", "F:\\javaworkspaceforwork\\multithread\\src\\1.jpg");
DownLoadContentWithThread dt2 = new DownLoadContentWithThread("https://cdn.jsdelivr.net/gh/justacoder99/r2coding@master/img/contentmap.3sdmma4od740.png", "F:\\javaworkspaceforwork\\multithread\\src\\2.jpg");
DownLoadContentWithThread dt3 = new DownLoadContentWithThread("https://cn.bing.com/th?id=OHR.RobinsEgg_EN-CN2393977438_1920x1080.jpg&rf=LaDigue_1920x1080.jpg", "F:\\javaworkspaceforwork\\multithread\\src\\3.jpg");
//4、开启线程
dt1.start();
dt2.start();
dt3.start();
}
}
2、实现Runnable接口:
package creatthread;
public class DownLoadContentWithRunnable implements Runnable {
//1、实现Runnable接口
private String url;
private String pathName;
public DownLoadContentWithRunnable(String url, String pathName) {
this.url = url;
this.pathName = pathName;
}
//2、重写run方法
@Override
public void run() {
WebPicDownLoad wpc = new WebPicDownLoad();
wpc.download(this.url, this.pathName);
System.out.println("下载" + pathName + "完成!");
}
public static void main(String[] args) {
//3、创建目标对象
DownLoadContentWithRunnable dt1 = new DownLoadContentWithRunnable("https://mmbiz.qpic.cn/mmbiz_jpg/xq9PqibkVAzr9bLeYf7ia7YrKlfqxlKIJz7P32mLI6RfWAsQ11Zr1NWZ40XDNyy99Q7rm6wau50mQZjTXvvjLwYg/640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1&wx_co=1", "F:\\javaworkspaceforwork\\multithread\\src\\4.jpg");
DownLoadContentWithRunnable dt2 = new DownLoadContentWithRunnable("https://cdn.jsdelivr.net/gh/justacoder99/r2coding@master/img/contentmap.3sdmma4od740.png", "F:\\javaworkspaceforwork\\multithread\\src\\5.jpg");
DownLoadContentWithRunnable dt3 = new DownLoadContentWithRunnable("https://cn.bing.com/th?id=OHR.RobinsEgg_EN-CN2393977438_1920x1080.jpg&rf=LaDigue_1920x1080.jpg", "F:\\javaworkspaceforwork\\multithread\\src\\6.jpg");
//4、传入Runnable接口逇实现类对象
//5、开启线程
new Thread(dt1).start();
new Thread(dt2).start();
new Thread(dt3).start();
}
}
3、实现Collable接口:
package creatthread;
import java.util.concurrent.*;
public class DownLoadContentWithCallable implements Callable<Boolean> {
//1、实现Callable接口
private String url;
private String pathName;
public DownLoadContentWithCallable(String url, String pathName) {
this.url = url;
this.pathName = pathName;
}
//2、重写call方法
@Override
public Boolean call() {
WebPicDownLoad wpc = new WebPicDownLoad();
wpc.download(this.url, this.pathName);
System.out.println("下载" + pathName + "完成!");
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
//3、创建目标对象
DownLoadContentWithCallable dt1 = new DownLoadContentWithCallable("https://mmbiz.qpic.cn/mmbiz_jpg/xq9PqibkVAzr9bLeYf7ia7YrKlfqxlKIJz7P32mLI6RfWAsQ11Zr1NWZ40XDNyy99Q7rm6wau50mQZjTXvvjLwYg/640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1&wx_co=1", "F:\\javaworkspaceforwork\\multithread\\src\\7.jpg");
DownLoadContentWithCallable dt2 = new DownLoadContentWithCallable("https://cdn.jsdelivr.net/gh/justacoder99/r2coding@master/img/contentmap.3sdmma4od740.png", "F:\\javaworkspaceforwork\\multithread\\src\\8.jpg");
DownLoadContentWithCallable dt3 = new DownLoadContentWithCallable("https://cn.bing.com/th?id=OHR.RobinsEgg_EN-CN2393977438_1920x1080.jpg&rf=LaDigue_1920x1080.jpg", "F:\\javaworkspaceforwork\\multithread\\src\\9.jpg");
//4、创建执行服务
ExecutorService es = Executors.newFixedThreadPool(3);//创建固定3个线程大小的线程池
//5、提交执行
Future<Boolean> s1 = es.submit(dt1);
Future<Boolean> s2 = es.submit(dt2);
Future<Boolean> s3 = es.submit(dt3);
//6、获取结果
boolean b1 = s1.get();
boolean b2 = s2.get();
boolean b3 = s3.get();
//输出结果
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
//7、关闭服务
es.shutdown();
}
}
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42732184/article/details/124350754
边栏推荐
- Idea plug-in --- playing songs in the background
- MySQL事务
- C language - Spoof shutdown applet
- Excel sets row and column colors according to cell contents
- Qwebsocket communication
- 多线程与高并发(1)——线程的基本知识(实现,常用方法,状态)
- Frequently asked interview questions - 1 (non technical)
- QT displays the specified position and size of the picture
- Solid contract DoS attack
- MySQL transaction
猜你喜欢
Interview Basics
QT drawpixmap and DrawImage blur problem
Issue 36 summary of atcoder beginer contest 248
Flutter nouvelle génération de rendu graphique Impeller
Flutter 新一代图形渲染器 Impeller
AcWing 836. Merge set (merge set)
Establish excel bookkeeping book through setting context menu
Some pits used by uni
Batch import of orange single micro service
opensips(1)——安装opensips详细流程
随机推荐
JS number capitalization method
deep learning object detection
Batch import of orange single micro service
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
Golang通过exec模块实现Ping连通性检测案例
catkin_package到底干了什么
Parameter analysis of open3d material setting
excel获取两列数据的差异数据
Establish excel bookkeeping book through setting context menu
手动删除eureka上已经注册的服务
Usage and difference of shellexecute, shellexecuteex and winexec in QT
Strategy for improving the conversion rate of independent stations | recovering abandoned users
7-10 longest symmetric substring (25 points) (violence problem solution) C language
AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
Hongji | how does HR carry out self change and organizational change in the digital era?
C, class library
SQL statement simple optimization
poi生成excel,插入图片
qt. qpa. plugin: Could not find the Qt platform plugin “xcb“ in ““
Step on the pit: Nacos uses startup CMD - M standalone startup error