当前位置:网站首页>阿里一面:多线程顺序运行有多少种方法?
阿里一面:多线程顺序运行有多少种方法?
2022-07-30 05:50: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)的优点,串行执行所有任务。
输出结果:
打开冰箱!拿出一瓶牛奶!
关上冰箱!
边栏推荐
- 瀑布流(自定义布局实现)
- 引导过程与服务控制
- RAID磁盘阵列
- Network Protocol 03 - Routing and NAT
- Selenium01
- OP 代币和不可转让的 NFT 致力于建立新的数字民主
- LVM和磁盘配额
- Test development engineer diary 002 - starting from 0 interface automation
- Test and Development Engineer Growth Diary 009 - Environment Pai Pai Station: Development Environment, Test Environment, Production Environment, UAT Environment, Simulation Environment
- npm安装nodejs环境配置
猜你喜欢

libgrape-lite on GPUs: GPU helps accelerate graph analysis tasks

多线程基础(多线程内存,安全,通信,线程池和阻塞队列)

Interactively compose graphs in GraphScope based on the JupyterLab plugin

I can't hide it, I want to expose the bad things about cloud native

关于memcache内核,全网最通俗的讲解

Build an intelligent network security management and control system for digital government

Ingress:从静态图分析到动态图分析

A New Paradigm for Distributed Deep Learning Programming: Global Tensor

How to save modelsim simulation data as a file

用 GraphScope 像 NetworkX 一样做图分析
随机推荐
GAIA-IR: Parallelized Graph Query Engine on GraphScope
How to create a shortcut without the "shortcut" suffix?
Dachang's annual salary of 50w+ recruits test engineers with test platform development capabilities
图计算在网络安全分析中的应用
Test Development Engineer Growth Diary 008 - Talking About Some Bugs/Use Case Management Platform/Collaboration Platform
Linx常见目录&文件管理命令&VI编辑器使用 介绍
Deploy GraphScope with Helm
测试开发工程师成长日记002 - 从0开始做接口自动化
Rapidly develop GraphScope graph analysis applications
网络协议01 - 基础概念
为数字政府构建智能化网络安全管控体系
iptables命令
Mastering JESD204B (3) – Debugging of AD6676
(GGG)JWT
空间顶点到直线的距离计算及其源码
CTO说不建议我使用SELECT * ,这是为什么?
软件测试术语 - 场景测试
GadgetInspector原理分析
libgrape-lite on GPUs:GPU助力加速图分析任务
The Force Plan Microservices | Centralized Configuration Center Config Asymmetric Encryption and Security Management