全部评论
public class PrintA_To_Z_2 {
public static volatile int i = 0;
public static Object obj = new Object();
public static boolean flag_A = true;
public static boolean flag_B = false;
public static boolean flag_C = false;
static class PrintThread_A implements Runnable{
@Override
public void run(){
while(i < 26){
if(flag_A){
synchronized (obj){
System.out.println(Thread.currentThread().getName() + (char)('A' + i));
++i;
flag_A = false;
flag_B = true;
}
}
}
}
}
static class PrintThread_B implements Runnable{
@Override
public void run(){
while(i < 26){
if(flag_B){
synchronized (obj){
System.out.println(Thread.currentThread().getName() + (char)('A' + i));
++i;
flag_B = false;
flag_C = true;
}
}
}
}
}
static class PrintThread_C implements Runnable{
@Override
public void run(){
while(i < 26){
if(flag_C){
synchronized (obj){
System.out.println(Thread.currentThread().getName() + (char)('A' + i));
++i;
flag_C = false;
flag_A = true;
}
}
}
}
}
public static void main(String[] args) throws Exception{
Thread t1 = new Thread(new PrintThread_A(), "1");
Thread t2 = new Thread(new PrintThread_B(), "2");
Thread t3 = new Thread(new PrintThread_C(), "3");
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
System.out.println("OK");
}
}
相关推荐
点赞 评论 收藏
分享
2025-11-18 18:24
北京理工大学珠海学院 嵌入式软件工程师
秋招投简历提醒助手:个人经验是,一般面二十场左右就会进入侃侃而谈阶段。我今年七月末的时候开始的第一次面试,都是很多不会,回复很慢。后面慢慢迭代,到九月中的时候基本上面啥说啥,很放松的状态 点赞 评论 收藏
分享
小型域名服务器:当看到别人比自己强的时候,即便这是对方应得的,很多人会也下意识的歪曲解构对方的意图,来消解自己在这本就不存在的比较中输掉的自信,从而平白制造出很多无谓的争论。比如你会在空余时间来写优质好文,而我回家只会暗区突围,那么我就可以作为键盘侠在这里评论你是不是XXXXXXXX。即便我自己都知道这是假的,但只要这没那么容易证伪,那么当你开始回应的时候,脏水就已经泼出去了,后面可能会有更多的人带着情绪来给我点赞,而毫不关注你写的文章内容本身是啥了。 点赞 评论 收藏
分享

