当前位置:网站首页>Multithreaded printing
Multithreaded printing
2022-07-01 02:53:00 【Panda_ Java】
Two threads alternately print 1 To 100
1 Two threads alternately print 1 To 100
static volatile int falg = 0;
@Test
public void test001(){
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
int i = -1;
while(i < 100) {
if(KinglexCompanyExecutorApplicationTests.falg == 0){
i = i + 2;
System.out.println(i);
KinglexCompanyExecutorApplicationTests.falg = 1;
}
}
}
});
threadA.start();
Thread threadB = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while(i < 100){
if(KinglexCompanyExecutorApplicationTests.falg == 1){
i = i + 2;
System.out.println(i);
KinglexCompanyExecutorApplicationTests.falg = 0;
}
}
}
});
threadB.start();
}

2 Three threads print alternately a、b、c
static volatile int falg = 0;
@Test
public void test001(){
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while(i < 100) {
if(KinglexCompanyExecutorApplicationTests.falg == 0){
i = i + 1;
System.out.println('a');
KinglexCompanyExecutorApplicationTests.falg = 1;
}
}
}
});
threadA.start();
Thread threadB = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while(i < 100){
if(KinglexCompanyExecutorApplicationTests.falg == 1){
i = i + 1;
System.out.println('b');
KinglexCompanyExecutorApplicationTests.falg = 2;
}
}
}
});
threadB.start();
Thread threadC = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while(i < 100){
if(KinglexCompanyExecutorApplicationTests.falg == 2){
i = i + 1;
System.out.println('c');
KinglexCompanyExecutorApplicationTests.falg = 0;
}
}
}
});
threadC.start();
}

边栏推荐
- STM32 - DS18B20 temperature sampling of first-line protocol
- Prototype and prototype chain in JS
- PHP batch Excel to word
- Is it safe to open an account online in a small securities firm? Will my money be unsafe?
- Pychar open remote directory remote host
- 产业互联网中,「小」程序有「大」作为
- PCB defect detection based on OpenCV and image subtraction
- xxl-job使用指南
- Communication protocol -- Classification and characteristics Introduction
- Share Creators萌芽人才培養計劃來了!
猜你喜欢

xxl-job使用指南

Pychart software deployment gray unable to point

Sampling Area Lights

【小程序项目开发-- 京东商城】uni-app之首页商品楼层

Applet custom top navigation bar, uni app wechat applet custom top navigation bar

lavaweb【初识后续问题的解决】

Sampling Area Lights

MCU firmware packaging Script Software

Detailed data governance knowledge system

Nacos configuration center tutorial
随机推荐
Restcloud ETL practice data row column conversion
Optimal Transport系列1
Borrowing constructor inheritance and composite inheritance
JS anti shake and throttling
UE4渲染管线学习笔记
How to buy Hong Kong shares in China? What platform is safer?
彻底解决Lost connection to MySQL server at ‘reading initial communication packet
联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
【微信小程序開發】樣式匯總
Share Creators萌芽人才培養計劃來了!
Is it safe to open a stock account? Shanghai stock account opening procedures.
鼠标悬停效果二
Od modify DLL and exe pop-up contents [OllyDbg]
Here comes the share creators budding talent training program!
PCB defect detection based on OpenCV and image subtraction
lavaweb【初识后续问题的解决】
Mouse over effect I
Complete training and verification of a neural network based on pytorch
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
Mouse over effect VI