当前位置:网站首页>多线程顺序运行有几种方法?
多线程顺序运行有几种方法?
2022-07-28 13:45:00 【chenzixia】
文章介绍4种方法,简单易懂,通过4个demo抛砖引玉。
1、在子线程中通过join()方法指定顺序
通过join()方法使当前线程“阻塞”,
“”
运行结果:
2、在主线程中通过join()方法指定顺序
子线程指的是发生在Thread内部的代码,主线程指的是发生在main函数中的代码。最新多线程面试题整理好了,点击Java面试库小程序在线刷题。
我们可以在main函数中通过join()方法让主线程阻塞等待以达到指定顺序执行的目的。
public class ThreadMainJoinDemo {public static void main(String[] args) throws InterruptedException {
final Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println( "打开冰箱!");
}
});
final Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println( "拿出一瓶牛奶!");
}
});
final Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println( "关上冰箱!");
}
});
thread1.start();
thread1.join();
thread2.start();
thread2.join();
thread3.start();
}
}
基础就不介绍了,多线程系列我博客教程写了很多了,这里推荐大家看看吧:https://www.javastack.cn/categories/Java/
输出结果:
打开冰箱!拿出一瓶牛奶!
关上冰箱!
3、通过倒数计时器CountDownLatch实现
CountDownLatch通过计数器提供了更灵活的控制,只要检测到计数器为0当前线程就可以往下执行而不用管相应的thread是否执行完毕。
public class ThreadCountDownLatchDemo {private static CountDownLatch countDownLatch1 = new CountDownLatch(1);
private static CountDownLatch countDownLatch2 = new CountDownLatch(1);
public static void main(String[] args) {
final Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println( "打开冰箱!");
countDownLatch1.countDown();
}
});
final Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch1.await();
System.out.println( "拿出一瓶牛奶!");
countDownLatch2.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
final Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch2.await();
System.out.println( "关上冰箱!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
//下面三行代码顺序可随意调整,程序运行结果不受影响
thread3.start();
thread1.start();
thread2.start();
}
}
打开冰箱!
拿出一瓶牛奶!
关上冰箱!
单线程化线程池(newSingleThreadExecutor)的优点,串行执行所有任务。
输出结果:
打开冰箱!拿出一瓶牛奶!
关上冰箱!
边栏推荐
- C语言实现简单学生成绩管理系统的方法
- Three methods to disassemble the rotation array
- Copy excel row to specified row
- 十、时间戳
- [translation] salt companies come to linkerd for load balancing, and stay for efficiency, reliability and performance
- Unittest executes runtestcase prompt <_ io. Textiowrapper name= '< stderr>' mode=W encoding=UTF-8 > solution
- QML picture preview
- Hcip day 10
- Langjing Technology (Trax China) "robot +ai" opens the era of Chinese retail meta universe
- Why is it reverse to convert from other formats to BMP
猜你喜欢

MeterSphere--开源持续测试平台

Hcip day 11

Metersphere -- Open Source continuous testing platform

2022年安全员-A证操作证考试题库模拟考试平台操作
Some problems encountered in the development of Excel VBA, solutions, and continuous updates

用友BIP CRM新品发布,赋能大中型企业营销增长

C语言中浮点数据类型(你学废了吗)

Forage QR code -- online QR code generator

Install mysql5.7.36 in CentOS

C语言实现简单学生成绩管理系统的方法
随机推荐
[server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks
2022高处安装、维护、拆除考试题库及在线模拟考试
Development and definition of software testing
Detailed explanation of C language student achievement management system [easy to understand]
朗镜科技(Trax中国)“机器人+AI”开启中国零售元宇宙时代
PowerDesigner creates a database model (conceptual model example)
如何只降3D相机不降UI相机的分辨率
【Utils】CookieUtil
Daily question - Scholarship
OKR and grad
开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
草料二维码--在线二维码生成器
UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing
【LeetCode】1331. 数组序号转换
分集技术简略
Collaborative office tools: Online whiteboard is in its infancy, and online design has become a red sea
软件测试的发展与定义
Hcip day 12
十、时间戳
How to effectively conduct the review meeting (Part 1)?