当前位置:网站首页>利用多线程按顺序连续输出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
边栏推荐
- SAP pi / PO soap2proxy consumption external WS example
- DBA常用SQL语句 (5) - Latch 相关
- MapReduce计算流程详解
- 通过流式数据集成实现数据价值(5)- 流分析
- 杰理之通常程序异常情况有哪些?【篇】
- PHP two-dimensional array specifies that the elements are added after they are equal, otherwise new
- MapReduce压缩
- Rain produces hundreds of valleys, and all things grow
- 0704、ansible----01
- ABAP implementation publishes restful services for external invocation example
猜你喜欢
Comparative analysis of meta universe from the dimension of knowledge dissemination
SAP pi / PO soap2proxy consumption external WS example
SAP ECC connecting SAP pi system configuration
The central control learning infrared remote control module supports network and serial port control
Planning and construction of industrial meta universe platform
云身份过于宽松,为攻击者打开了大门
2022年上海市安全员C证考试题库及答案
2022茶艺师(初级)考试试题模拟考试平台操作
PHP notes (I): development environment configuration
2022年流动式起重机司机考试题库模拟考试平台操作
随机推荐
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
[COCI] Vje š TICA (subset DP)
2022茶艺师(初级)考试试题模拟考试平台操作
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
最长公共前串
第三章 启用和调整IM列存储的大小(IM-3.1)
Juc并发编程07——公平锁真的公平吗(源码剖析)
Odoo 服务器搭建备忘
杰理之栈溢出 stackoverflow 怎么办?【篇】
2022年制冷与空调设备运行操作考试练习题及模拟考试
DBA常用SQL语句(3)- cache、undo、索引和等待事件
Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie
Number theory blocking (integer division blocking)
代码源每日一题 div1 (701-707)
Pyqt5 and communication
DBA常用SQL语句(6)- 日常管理
SAP 03-amdp CDs table function using 'with' clause
[CF 1425d] danger of mad snakes
杰理之通常程序异常情况有哪些?【篇】
Go language practice mode - functional options pattern