当前位置:网站首页>Using multithreading to output abc10 times in sequence
Using multithreading to output abc10 times in sequence
2022-04-23 10:03:00 【N. LAWLIET】
Multithreading is a common technology in both interview and company . Today, I passed a small topic, integrating multiple multithreading technologies .
This problem is the use of multi-threaded continuous output ABC Ten times . First , Let me first explain that the solution to this problem is not the only way , And I only offer one , Other methods are expected to be tried by readers themselves . The first step is to define three threads , Inherit Runnablej Interface , Rewrite the method and add synchronized lock ,(synchronized Is a mutually exclusive synchronous lock , The function is to run only one thread at a time in a multithreaded program , And synchronize changes ), Only synchronized Not enough , We need to volatile Keyword modifies a variable used to control the execution order of threads (volatile It can ensure the visibility and order between threads , But there's no guarantee of atomicity , Therefore, thread safety can only be guaranteed under appropriate scenarios ). adopt java Medium wait() Method ( Use wait() Method needs to be started notify() Method to restart the thread ) Control thread interval .
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://yzsam.com/2022/04/202204230957069409.html
边栏推荐
- Construire neuf capacités de fabrication agile à l'ère métacosmique
- 雨生百谷,万物生长
- 0704、ansible----01
- (Extended) bsgs and higher order congruence equation
- The central control learning infrared remote control module supports network and serial port control
- DBA common SQL statements (5) - latch related
- 解决VMware卸载后再安装出现的问题
- Go语言实践模式 - 函数选项模式(Functional Options Pattern)
- LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
- SQL tuning series - Introduction to SQL tuning
猜你喜欢
随机推荐
"Gu Yu series" airdrop
F-niu Mei's apple tree (diameter combined)
NEC infrared remote control coding description
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
[COCI] Vje š TICA (subset DP)
[codeforces - 208e] blood cousins
DBA常用SQL语句 (5) - Latch 相关
Chapter 3 enable and adjust the size of IM column storage (im-3.1)
[2020wc Day2] F. Clarice picking mushrooms (subtree and query, light and heavy son thought)
杰理之系统事件有哪些【篇】
深度选择器
[hdu6833] a very easy math problem
一文看懂 LSTM(Long Short-Term Memory)
最长公共前串
LeetCode-608. 树节点
Pyqt5 and communication
ansible 云计算 自动化 命令行精简版
第二章 In-Memory 体系结构 (IM-2.2)
杰理之栈溢出 stackoverflow 怎么办?【篇】
解决VMware卸载后再安装出现的问题









