当前位置:网站首页>Several methods of multi-threaded sequential operation can be asked casually in the interview
Several methods of multi-threaded sequential operation can be asked casually in the interview
2022-07-29 00:43:00 【Technical Trivia】
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. !
边栏推荐
- What are the skills of API interface optimization?
- MySQL sub database and sub table and its smooth expansion scheme
- 聊聊异步编程的 7 种实现方式
- [ESN] learning echo state network
- 面试被问到了String相关的几道题,你能答上来吗?
- rk3399 9.0驱动添加Powser按键
- MySQL stored procedure
- [micro services ~nacos] Nacos service providers and service consumers
- Dynamic programming problem (3)
- 最长上升子序列
猜你喜欢

时间序列统计分析

Dynamic programming problem (3)
![[micro services ~nacos] Nacos service providers and service consumers](/img/b7/47ecd6979ccfeb270261681d6130be.png)
[micro services ~nacos] Nacos service providers and service consumers

数仓搭建——ADS层

requestVideoFrameCallback() 简单实例

手把手教你安装Latex(保姆级教程)

MySQL的存储过程

PTA (daily question) 7-72 calculate the cumulative sum

Anomaly detection and unsupervised learning (1)

How to solve the problems of MQ message loss, duplication and backlog?
随机推荐
Alibaba code index technology practice: provide reading experience of local IDE for code review
动态规划问题(二)
PTA (daily question) 7-73 turning triangle
Google browser, no installation required
Solutions such as failed plug-in installation and slow speed of linking remote server under vscode
Flyway's quick start tutorial
17. Design of machine learning system
zabbix部署及监控
“吃货联盟定餐系统”
2022dasctfjuly empowerment competition (reappearance)
Teach you how to install latex (nanny level tutorial)
Anomaly detection and unsupervised learning (2)
Attack and defense world web master advanced area web_ php_ include
Router view cannot be rendered (a very low-level error)
软考 --- 数据库(4)SQL语句
Software designer afternoon question
IMG tags prohibit dragging pictures
110 MySQL interview questions and answers (continuously updated)
requestVideoFrameCallback() 简单实例
NPM run serve stuck at 40%