当前位置:网站首页>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. !
边栏推荐
- 上市1个月接连发生两起安全事故,理想L9还理想吗?
- Color finder actual combat (QT including source code)
- IJCAI2022教程 | 对话推荐系统
- 分页功能(板子)
- C#连接MySql数据库详细步骤
- 使用缓冲的方式采集视频
- Nacos principle
- It is not only convenient, safe + intelligent, but also beautiful. Fluorite releases the Big Dipper face lock dl30f and Aurora face video lock y3000fv
- C language function program example (super complete)
- Invalid prompt object name in SQL Server
猜你喜欢

Ctfshow question making web module web11~web14

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

CVPR 2022 | in depth study of batch normalized estimation offset in network

Coding with these 16 naming rules can save you more than half of your comments!

Confession of a graduate student: why am I addicted to opengauss community?

(转)冒泡排序及优化详解

Lazada店铺如何产号高效补单?(测评自养号技术详解篇)

上市1个月接连发生两起安全事故,理想L9还理想吗?
![[Zhou Zhou has a prize] cloud native programming challenge](/img/0d/e26e37cddf3cf01b5e9dcaf7211106.png)
[Zhou Zhou has a prize] cloud native programming challenge "edge container" track invites you to fight!

What is ci/cd| Achieve faster and better software delivery
随机推荐
Moco V3: visual self supervision ushers in transformer
Tested interviewed Zuckerberg: reveal more details of four VR prototypes
Link with Bracket Sequence I(状态基多维dp)
Ctfshow question making web module web11~web14
职场高薪 |「中高级测试」面试题
牛客打开摄像头几秒后画面消失 | 相机打开画面一闪一闪
【TiDB】txt文档导入数据库,这样做真的很高效
【题目】两数相加
MFC WPF WinForm (Industrial MFC or QT)
Study - 几何计算总结
Capture video by buffering
Api 接口优化的几个技巧
实习日记第一周
Unity3d tutorial notes - unity initial 04
How to turn on or off the disk LED of EMC Vmax
Ijcai2022 tutorial | dialogue recommendation system
Basic operations of unity3d scene production
证券企业基于容器化 PaaS 平台的 DevOps 规划建设 29 个典型问题总结
SSM-使用@Async和创建ThreadPoolTaskExecutor线程池
DeiT:注意力Attention也能蒸馏