当前位置:网站首页>Three threads print digital demo alternately
Three threads print digital demo alternately
2022-06-13 00:49:00 【Python's path to becoming a God】
The code is as follows , The notes are written in great detail , Copy and run .
public class Xs02 {
// use volatile Decorated two things that will be used for a while int
private volatile int flag=0;
private volatile int work=0;
private Thread t1,t2,t3;
public static void main(String[] args) {
Xs02 xs02 = new Xs02();
// start-up
xs02.ts();
}
private void ts(){
// How to start a thread , This step is written directly in main Method is OK
t1=new Thread(new a());
t2=new Thread(new b());
t3=new Thread(new c());
t1.start();
t2.start();
t3.start();
}
private class a implements Runnable{
@Override
public void run() {
// First determine whether the number to be printed reaches the threshold , No, just continue printing
while (work<=100){
// This is for volatile A special flag bit is made for each thread , Only when the variable is the specified value , Only the corresponding thread prints numbers
if(flag==0){
// Print the number if it is the specified value
System.out.println("1:"+work++);
// After printing , Change to the flag bit of the next thread
flag=1;
}
}
}
}
private class b implements Runnable{
@Override
public void run() {
while (work<=100){
if(flag==1){
System.out.println("2:"+work++);
flag=2;
}
}
}
}
private class c implements Runnable{
@Override
public void run() {
while (work<=100){
if(flag==2){
System.out.println("3:"+work++);
flag=0;
}
}
}
}
}
边栏推荐
- 蓝桥杯单片机第七届决赛
- 通过抓包下载钉钉直播回放
- Druid reports an error connection holder is null
- Cards are unpredictable
- STM32 USB Basics
- Rest at home today
- Aunt learning code sequel: ability to sling a large number of programmers
- Binary tree -- using hierarchical sequence and middle sequence to determine a tree
- [JS component] create a custom horizontal and vertical scroll bar following the steam style
- Canvas game 2048 free map size
猜你喜欢
MySQL queries the quantity of each month and the year-on-year and month on month data of each month
Kotlin 协程,job的生命周期
今日睡眠质量记录74分
阿姨学代码续集:能力吊打大批程序员
Buuctf babyupload[gxyctf2019]
什么是 dummy change?
[gxyctf2019] no dolls -- detailed explanation
Kalix system - use of information collection gadgets
Druid reports an error connection holder is null
硬(磁)盘(二)
随机推荐
磁盘分区方式对比(MBR与GPT)
Map from getting started to performance optimization
Win10 home vs pro vs enterprise vs enterprise LTSC
深度学习模型剪枝
MySQL query table field information
今日睡眠质量记录74分
Win10 home vs pro vs enterprise vs enterprise LTSC
Canvas airplane game
[North Asia server data recovery] data recovery case of Hyper-V service paralysis caused by virtual machine file loss
Andersen Global通过在芬兰和丹麦的合作协议拓展北欧地区业务版图
Composite key relationships using Sqlalchemy - relationships on composite keys using Sqlalchemy
从ADK的WinPE自己手动构建自己的PE
单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
人神共愤,唐山“群殴女性事件”细节...
Using fastjson to solve the problem of returning an empty array from a null value of a field string object
[JS component library] drag sorting component
DNS attack surface analysis
Arduino controls tb6600 driver +42 stepper motor
[JS] solve the problem that removeeventlistener is invalid after the class listening event from new is bound to this
[JS component] custom paging