当前位置:网站首页>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();
}

边栏推荐
- Mouse over effect II
- How to buy Hong Kong shares in China? What platform is safer?
- 鼠标悬停效果一
- 鼠标悬停效果三
- 基于Pytorch完整的训练一个神经网络并进行验证
- Detailed data governance knowledge system
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
- Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021
- Analysis and solution of anr problems
- STM32——一线协议之DS18B20温度采样
猜你喜欢

Image preloading in JS

Pulsar的Proxy支持和SNI路由

Network address translation (NAT) technology

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)

Record a service deployment failure troubleshooting

Here comes the share creators budding talent training program!

How to use Jieba participle in unity

Restcloud ETL WebService data synchronization to local

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)

STM32 - DS18B20 temperature sampling of first-line protocol
随机推荐
使用ipmitool配置X86服务器的BMC网络和用户信息
Pulsar geo replication/ disaster recovery / regional replication
Pulsar 主题压缩
AI edge computing platform - beaglebone AI 64 introduction
Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
通信协议——分类及其特征介绍
产业互联网中,「小」程序有「大」作为
Mouse over effect 9
[multi source BFS] 934 Shortest Bridge
Magnetic manometer and measurement of foreign coins
Analysis and solution of anr problems
Mouse over effect V
园区运营效率提升,小程序容器技术加速应用平台化管理
SAP ALV汇总跟导出Excel 汇总数据不一致
PTA 1017
联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
How to use Jieba participle in unity
鼠标悬停效果九
How to buy Hong Kong shares in China? What platform is safer?
If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?