当前位置:网站首页>创建线程的三种方式
创建线程的三种方式
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
边栏推荐
猜你喜欢

Interview Basics

QSS, qdateedit, qcalendarwidget custom settings

多线程与高并发(3)——synchronized原理

Deep learning object detection

Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator

Common protocols of OSI layer

基于ssm 包包商城系统

Establish excel bookkeeping book through setting context menu

Frequently asked interview questions - 2 (computer network)

JVM系列(3)——内存分配与回收策略
随机推荐
Several examples of pointer transfer, parameter transfer, value transfer, etc
mysql实现主从复制/主从同步
solidity合约DOS攻击
poi导出excel,行相同数据自动合并单元格
AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
Golang implements Ping connectivity detection case through exec module
Reading notes of modern methods of C language programming
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
对象转map
Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios
MySQL triggers, stored procedures, stored functions
PHP处理json_decode()解析JSON.stringify
MySQL create Oracle exercise table
【华为机试】考试得分总数(如何处理答错的情况?回溯一次,代表答错一题)
转置卷积(Transposed Convolution)
What financial products will benefit during May Day?
DWSurvey是一个开源的调查问卷系统。解决无法运行问题,修改bug。
poi生成excel,插入图片
STD:: String implements split
MDN文档里面入参写法中括号‘[]‘的作用