当前位置:网站首页>Four methods of multi-threaded sequential operation. Ask casually during the interview
Four methods of multi-threaded sequential operation. Ask casually during the interview
2022-07-28 21:21:00 【Ah size】
The article introduces 4 Methods , Simple and easy to understand , adopt 4 individual demo throw away a brick in order to get a gem .
1、 Passing through the join() Method specifies the order
adopt join() Method to make the current thread “ Blocking ”,
“”
Running results :
2、 Pass in main thread join() Method specifies the order
A child thread refers to a thread that occurs in Thread Internal code , The main thread refers to what happens in main Code in function . The latest multithreaded interview questions have been sorted out , Click on Java Interview database Small program online brush questions .
We can do it in main Function through join() Method lets the main thread block and wait to achieve the purpose of executing in the specified order .
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( " Open the refrigerator !");
}
});
final Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println( " Take out a bottle of milk !");
}
});
final Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println( " Turn off the fridge. !");
}
});
thread1.start();
thread1.join();
thread2.start();
thread2.join();
thread3.start();
}
}
I won't introduce the foundation , Multithreading series I have written a lot of blog tutorials , Here we recommend you to have a look :https://www.javastack.cn/categories/Java/
Output results :
Open the refrigerator !Take out a bottle of milk !
Turn off the fridge. !
3、 Through the countdown timer CountDownLatch Realization
CountDownLatch More flexible control is provided through counters , As long as it is detected that the counter is 0 The current thread can execute down without taking care of the corresponding thread Whether the implementation is completed .
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( " Open the refrigerator !");
countDownLatch1.countDown();
}
});
final Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch1.await();
System.out.println( " Take out a bottle of milk !");
countDownLatch2.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
final Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch2.await();
System.out.println( " Turn off the fridge. !");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// The order of the following three lines of code can be adjusted at will , The running results of the program are not affected
thread3.start();
thread1.start();
thread2.start();
}
}
Open the refrigerator !
Take out a bottle of milk !
Turn off the fridge. !
Single threaded pool (newSingleThreadExecutor) The advantages of , Serial execution of all tasks .
Output results :
Open the refrigerator !Take out a bottle of milk !
Turn off the fridge. !
边栏推荐
- A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year
- The greatest romance of programmers~
- Eureka相互注册,只显示对方或只在一个中显示问题
- 【题目】两数相加
- DeiT:注意力Attention也能蒸馏
- 广和通&高通物联网技术开放日成功举办
- 35 道 MySQL 面试必问题图解,这样也太好理解了吧
- The EMC vnx5200 fault light is on, but there is no hardware fault prompt
- [tidb] importing TXT documents into the database is really efficient
- 顶级“Redis 笔记”, 缓存雪崩 + 击穿 + 穿透 + 集群 + 分布式锁,NB 了
猜你喜欢
![[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles](/img/4f/e7806d75cd719e181d8455e4fdc1e7.jpg)
[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles

Unity - script lifecycle

【云原生】什么是 CI/CD ? | 摆平交付障碍的 CI/CD

npm如何切换淘宝源镜像

C language function program example (super complete)

Quii Cordova plugin telerik imagepicker plug-in multi image upload out of sequence

What functions does MySQL have? Don't look everywhere. Just look at this.

Maxwell is an easy-to-use software for capturing MySQL data in real time

Unity3d tutorial notes - unity initial 02

Unity3d tutorial notes - unity initial 04
随机推荐
Tested interviewed Zuckerberg: reveal more details of four VR prototypes
The greatest romance of programmers~
ctfshow 网络迷踪做题记录(1)
Zcmu--5066: dark corridor
C#连接MySql数据库详细步骤
证券企业基于容器化 PaaS 平台的 DevOps 规划建设 29 个典型问题总结
Maintenance of delta hot metal detector principle analysis of v5g-jc-r1 laser measurement sensor / detector
Jiuxin intelligence officially joined opengauss community
Sharkteam completes the safety audit of flow ecological NFT market matrixmarket
速卖通测评自养号,国外环境如何搭建?需要多少成本?
Redis缓存雪崩、缓存穿透、缓存击穿
详细讲解C语言12(C语言系列)
[tool class] util package of map, common entity classes are converted to map and other operations
npm如何切换淘宝源镜像
Icml2022 | timing self-monitoring video transformer
取色器实战(Qt含源码)
Moco V3: visual self supervision ushers in transformer
在子组件中使用el-date-picker报错
智能家居行业发展,密切关注边缘计算和小程序容器技术
怎么理解数据网格(Data Mesh)