当前位置:网站首页>多线程顺序运行的几种方法,面试可以随便问
多线程顺序运行的几种方法,面试可以随便问
2022-07-28 22:41:00 【技术琐事】
文章介绍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)的优点,串行执行所有任务。
输出结果:
打开冰箱!拿出一瓶牛奶!
关上冰箱!
边栏推荐
- Laravel permission control
- The 30th day of question brushing
- Anti shake and throttling
- What does WGet mean
- Statistical analysis of time series
- Attack and defense world web master advanced area web_ php_ include
- How to solve Oracle not available
- 【esn】 学习回声状态网络
- PTA (daily question) 7-74 yesterday
- Dynamic programming problem (VIII)
猜你喜欢

Advanced area of attack and defense world web masters warmup

How to solve the problem that the Oracle instance cannot be started

动态规划问题(八)

Software designer - intermediate, exam summary

MQ 消息丢失、重复、积压问题,如何解决?

The difference between {} and ${}

【开发教程10】疯壳·开源蓝牙心率防水运动手环-蓝牙 BLE 收发

Still writing a lot of if to judge? A rule executor kills all if judgments in the project

17.机器学习系统的设计

Idea error running 'application' command line is too long solution
随机推荐
Detailed explanation of the usage of exists in MySQL
PTA (daily question) 7-70 diamond
Cause analysis of 12 MySQL slow queries
How to learn R language
CV target detection model sketch (2)
Still writing a lot of if to judge? A rule executor kills all if judgments in the project
flyway的快速入门教程
Where is sandbox's confidence in rejecting meta's acquisition of meta universe leader sand?
“吃货联盟定餐系统”
The 30th day of question brushing
软考 --- 数据库(4)SQL语句
execute immediate 简单示例合集(DML)
CV semantic segmentation model sketch (2)
Web系统常见安全漏洞介绍及解决方案-sql注入
动态规划问题(二)
I don't know how lucky the boy who randomly typed the log is. There must be a lot of overtime!
Samsung asset management (Hong Kong) launched yuancosmos ETF to focus on investing in the future tuyere track
How to solve the problems of MQ message loss, duplication and backlog?
Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接
还在写大量 if 来判断?一个规则执行器干掉项目中所有的 if 判断...