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

边栏推荐
- Network address translation (NAT) technology
- Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
- Restcloud ETL data realizes incremental data synchronization through timestamp
- 【小程序项目开发-- 京东商城】uni-app之首页商品楼层
- Mouse over effect III
- Poj-3486-computers[dynamic planning]
- LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
- [wechat applet development] style summary
- Mouse over effect VI
- 7_OpenResty安装
猜你喜欢

【Qt】添加第三方库的知识补充

视觉特效,图片转成漫画功能

Sampling Area Lights

Saving images of different depths in opencv

Densenet network paper learning notes

Dell server restart Idrac method

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

Lenovo x86 server restart management controller (xclarity controller) or TSM method

Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and

Pychar open remote directory remote host
随机推荐
servlet【初识】
A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
Densenet network paper learning notes
How to open a stock account? Also, is it safe to open an account online?
Restcloud ETL practice data row column conversion
LeetCode_栈_困难_227.基本计算器(不含乘除)
Nacos configuration center tutorial
集群方法同步执行框架 Suona
JS anti shake and throttling
视觉特效,图片转成漫画功能
记一次服务部署失败问题排查
园区运营效率提升,小程序容器技术加速应用平台化管理
Restcloud ETl数据通过时间戳实现增量数据同步
【机器学习】向量化计算 -- 机器学习路上必经路
pycharm 软件deployment 灰色 无法点
import tensorflow.contrib.slim as slim报错
Restcloud ETL data realizes incremental data synchronization through timestamp
kubernetes资源对象介绍及常用命令(二)
Mouse over effect 10
Cluster method synchronous execution framework suona