当前位置:网站首页>Ali: How many methods are there for multi-threaded sequential operation?
Ali: How many methods are there for multi-threaded sequential operation?
2022-07-30 07:52:00 【chenxuyuana】
文章介绍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)的优点,串行执行所有任务.
输出结果:
打开冰箱!拿出一瓶牛奶!
关上冰箱!
边栏推荐
- About memcache kernel, so one of the most popular
- MySQL主从复制配置搭建,一步到位
- Test Development Engineer Growth Diary 015 - Top 20 Test Interview Questions
- Proftpd配置文件
- CTO说不建议我使用SELECT * ,这是为什么?
- Pioneer in Distributed Systems - Leslie Lambert
- 这个终端连接工具,碾压Xshell
- prometheus-tls加密
- What happens when @Bean and @Component are used on the same class?
- Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)
猜你喜欢
随机推荐
Network Protocol 03 - Routing and NAT
舒尔补(schur completement)
Mobile phone side scroll to page to specify location
测试开发工程师成长日记009 - 环境排排站:开发环境、测试环境、生产环境、UAT环境、仿真环境
MongoDB - query
如何理解普吕克坐标(几何理解)
The calculation and source code of the straight line intersecting the space plane
Let the "label" content in Baidu map generator expand--solution
VR机器人教你如何正确打乒乓球
prometheus监控nacos
测试开发工程师成长日记015 - 最强20道测试面试题
prometheus-federation-tls加密
分布式系统中的开创者—莱斯利·兰伯特
SE_01
MySql connecting to the server remotely
New material under the plastic restriction order - polylactic acid (PLA)
STL源码剖析:bound friend template friend代码测试和理解
Redis 如何实现防止超卖和库存扣减操作?
LVM和磁盘配额
人工肌肉智能材料新突破








