public class test {
public static volatile int flag1 = 1;
public static volatile int flag2 = 0;
public static volatile int flag3 = 0;
public static void main(String[] args) {
Thread t1 = new Thread(){
@Override
public void run() {
while(true)
{
if(flag1==1)
{
System.out.println("A");
flag1=0;
flag2=1;
}
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
while(true)
{
if(flag2==1)
{
System.out.println("B");
flag2=0;
flag3=1;
}
}
}
};
Thread t3 = new Thread(){
@Override
public void run() {
while(true)
{
if(flag3==1)
{
System.out.println("C");
flag3=0;
flag1=1;
}
}
}
};
t1.start();
t2.start();
t3.start();
}
}
打印出来结果没错,不知这样是否可以?
全部评论
(3) 回帖