当前位置:网站首页>Three ways to create threads
Three ways to create threads
2022-04-23 06:01:00 【hanyc..】
utilize url Download class of network diagram :
package creatthread;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
// need commons-io-2.6 jar package
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 abnormal ,download There is a problem with the method !");
}
}
}
1、 Inherit Thread class :
package creatthread;
public class DownLoadContentWithThread extends Thread {
//1、 Inherit Thread class
private String url;
private String pathName;
public DownLoadContentWithThread(String url, String pathName) {
this.url = url;
this.pathName = pathName;
}
//2、 rewrite run Method
@Override
public void run() {
WebPicDownLoad wpc = new WebPicDownLoad();
wpc.download(this.url, this.pathName);
System.out.println(" download " + pathName + " complete !");
}
public static void main(String[] args) {
//3、 Create target object
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、 Open thread
dt1.start();
dt2.start();
dt3.start();
}
}
2、 Realization Runnable Interface :
package creatthread;
public class DownLoadContentWithRunnable implements Runnable {
//1、 Realization Runnable Interface
private String url;
private String pathName;
public DownLoadContentWithRunnable(String url, String pathName) {
this.url = url;
this.pathName = pathName;
}
//2、 rewrite run Method
@Override
public void run() {
WebPicDownLoad wpc = new WebPicDownLoad();
wpc.download(this.url, this.pathName);
System.out.println(" download " + pathName + " complete !");
}
public static void main(String[] args) {
//3、 Create target object
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、 Pass in Runnable Interface implementation class object
//5、 Open thread
new Thread(dt1).start();
new Thread(dt2).start();
new Thread(dt3).start();
}
}
3、 Realization Collable Interface :
package creatthread;
import java.util.concurrent.*;
public class DownLoadContentWithCallable implements Callable<Boolean> {
//1、 Realization Callable Interface
private String url;
private String pathName;
public DownLoadContentWithCallable(String url, String pathName) {
this.url = url;
this.pathName = pathName;
}
//2、 rewrite call Method
@Override
public Boolean call() {
WebPicDownLoad wpc = new WebPicDownLoad();
wpc.download(this.url, this.pathName);
System.out.println(" download " + pathName + " complete !");
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
//3、 Create target object
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、 Create execution service
ExecutorService es = Executors.newFixedThreadPool(3);// Create fixed 3 Thread pool of thread size
//5、 Submit for execution
Future<Boolean> s1 = es.submit(dt1);
Future<Boolean> s2 = es.submit(dt2);
Future<Boolean> s3 = es.submit(dt3);
//6、 To get the results
boolean b1 = s1.get();
boolean b2 = s2.get();
boolean b3 = s3.get();
// Output results
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
//7、 Close the service
es.shutdown();
}
}
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230541159350.html
边栏推荐
- Pytorch学习记录(七):处理数据和训练模型的技巧
- Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
- 图解HashCode存在的意义
- Numpy common function table sorting of data processing
- Pytoch learning record (x): data preprocessing + batch normalization (BN)
- Pytorch学习记录(十):数据预处理+Batch Normalization批处理(BN)
- Dva中在effects中获取state的值
- Graphic numpy array matrix
- 自动控制(韩敏版)
- On traversal of binary tree
猜你喜欢
container
On traversal of binary tree
Pytorch学习记录(十二):学习率衰减+正则化
Development environment EAS login license modification
开发环境 EAS登录 license 许可修改
String notes
PyQy5学习(三):QLineEdit+QTextEdit
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
Filebrowser realizes private network disk
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
随机推荐
深入源码分析Servlet第一个程序
Rsync for file server backup
基于thymeleaf实现数据库图片展示到浏览器表格
Linear algebra Chapter 1 - determinant
Shansi Valley P290 polymorphism exercise
Gaussian processes of sklearn
Pytorch学习记录(十一):数据增强、torchvision.transforms各函数讲解
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
线性代数第二章-矩阵及其运算
Complete example demonstration of creating table to page - joint table query
Kingdee EAS "general ledger" system calls "de posting" button
Multithreading and high concurrency (1) -- basic knowledge of threads (implementation, common methods, state)
Linear algebra Chapter 2 - matrices and their operations
字符串(String)笔记
Practical operation - Nacos installation and configuration
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
自定义异常类
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
Viewer: introduce MySQL date function