当前位置:网站首页>利用多线程按顺序连续输出abc10次
利用多线程按顺序连续输出abc10次
2022-04-23 09:57:00 【N. LAWLIET】
多线程无论是在面试还是在公司都是经常会出现和用到的技术。今天我通过一道小题融合多项多线程技术。
这道题是利用多线程连续输出ABC十次。首先,我先说明解决这道题的方法不唯一,而我只提供一种,其他方法希望读者自己尝试。第一步需要定义三个线程,继承Runnablej接口,重写其中方法并且加上synchronized锁,(synchronized是一种互斥同步锁,作用是在多线程程序中一次只能运行一个线程,并且同步更改),只用synchronized还不够吗,我们需要volatile关键字修饰一个变量用来控制线程执行顺序(volatile可以保证线程间的可见性和有序性,但是不能保证原子性,所以需要在合适的场景下才能保证线程安全)。通过java中的wait()方法(使用wait()方法后需要启动notify()方法才能重启线程)控制线程间隔。
public class ThreadABC {
private volatile static int flag = 1;
public static void main(String[] args) throws IOException, InterruptedException{
Thread threada = new Thread(new Runnable() {
@Override
public synchronized void run() {
try {
while(flag!=1) {
wait();
}
System.out.println("A");
flag = 2;
notify();
}catch (Exception e) {
e.printStackTrace();
}
}
});
Thread threadb = new Thread(new Runnable() {
@Override
public synchronized void run() {
try {
while(flag!=2) {
wait();
}
System.out.println("B");
flag =3;
notify();
}catch (Exception e) {
e.printStackTrace();
}
}
});
Thread threadc = new Thread(new Runnable() {
@Override
public synchronized void run() {
try {
while(flag !=3) {
wait();
}
System.out.println("C");
flag = 1;
notify();
}catch (Exception e) {
e.printStackTrace();
}
}
});
ExecutorService pool = Executors.newCachedThreadPool();
for(int i = 0;i<10;i++) {
pool.execute(threada);
pool.execute(threadb);
pool.execute(threadc);
}
}
}
版权声明
本文为[N. LAWLIET]所创,转载请带上原文链接,感谢
https://blog.csdn.net/jiangzuofengqiao/article/details/124307876
边栏推荐
- "Gu Yu series" airdrop
- [hdu6868] absolute math (pusher + Mobius inversion)
- LeetCode-608. 树节点
- 1D / 1D dynamic programming learning summary
- Pyqt5 and communication
- 杰理之系统事件有哪些【篇】
- 从知识传播的维度对比分析元宇宙
- 通过流式数据集成实现数据价值(4)-流数据管道
- 论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——5结果
- Construire neuf capacités de fabrication agile à l'ère métacosmique
猜你喜欢
MapReduce压缩
Nine abilities of agile manufacturing in the era of meta universe
Sim Api User Guide(5)
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
PHP notes (I): development environment configuration
解决VMware卸载后再安装出现的问题
元宇宙时代的职业规划与执行
"Gu Yu series" airdrop
ABAP publishes OData service samples from CDs view
随机推荐
2022年上海市安全员C证考试题库及答案
实践六 Windows操作系统安全攻防
Es aggregation aggregation analysis
[hdu6868] absolute math (pusher + Mobius inversion)
C language: expression evaluation (integer promotion, arithmetic conversion...)
Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
構建元宇宙時代敏捷制造的九種能力
使用IDEA开发Spark程序
Function realization of printing page
[CF 1425d] danger of mad snakes
工业元宇宙平台规划与建设
NEC infrared remote control coding description
Custom login failure handling
Redis 异常 read error on connection 解决方案
Sim Api User Guide(5)
Juc并发编程09——Condition实现源码分析
1D / 1D dynamic programming learning summary
Understand scope