三线程顺序打印N次ABC

记得前一阵子JE上讨论线程顺序打印的面试题,现在有空也练练。
说是,三个线程,其一只打印A,其一只打印B,其一只打印C,要求开启三线程顺序打印多次ABC。
对wait和notify(All)刚刚理解,不能错过这个练习的机会,然后再写个生产者-消费者的程序多练练。写的不好的、需要改进的地方还请JE上的朋友给建议^..^。

public class ThreadABC implements Runnable {
public static final int RUN_TOTAL_COUNT=30; //打印ABC的次数
public static volatile boolean arun=true; //开始打印A
public static volatile boolean brun=false;
public static volatile boolean crun=false;
public static final byte[] LOCK=new byte[0]; //互斥器
public void run() {
new Thread(new PrintB()).start();
new Thread(new PrintC()).start();
new Thread(new PrintA()).start();
}
private static class PrintA implements Runnable{
private int runCount=0;
public void run(){
synchronized(LOCK){
while(runCount<RUN_TOTAL_COUNT)
if(arun){
LOCK.notifyAll();
System.out.print("A");
runCount++;
arun=false;
brun=true;
crun=false;
}else{
try{
LOCK.wait();
}catch(InterruptedException ie){
System.out.println("PrintA InterruptedException occured...");
}
}
} //end syn
}
}
private static class PrintB implements Runnable{
private int runCount=0;
public void run(){
synchronized(LOCK){
while(runCount<RUN_TOTAL_COUNT)
if(brun){
LOCK.notifyAll();
System.out.print("B");
runCount++;
arun=false;
brun=false;
crun=true;
}else{
try{
LOCK.wait();
}catch(InterruptedException ie){
System.out.println("PrintB InterruptedException occured...");
}
}
} //end syn
}
}
private static class PrintC implements Runnable{
private int runCount=0;
public void run(){
synchronized(LOCK){
while(runCount<RUN_TOTAL_COUNT)
if(crun){
LOCK.notifyAll();
System.out.print("C-");
runCount++;
arun=true;
brun=false;
crun=false;
}else{
try{
LOCK.wait();
}catch(InterruptedException ie){
System.out.println("PrintC InterruptedException occured...");
}
}
} //end syn
}
}

//test in main
public static void main(String[] args){
new Thread(new ThreadABC()).start();
}
}

-----------------顺序打印A-Z-----------------------------------

public class Print implements Runnable {
public static final int COUNT=26; //字母的个数
public static final int TIMES=10; //循环打印的次数
public static final byte[] LOCK=new byte[0]; //互斥器
private static volatile int run=0; //每打印一个字母,值会+1
private List<Printor> printors=new ArrayList<Printor>(); //收集所有线程对象

@Override
public void run() {
for(int i=0;i<COUNT;i++){ //初始化26个线程对象
printors.add(new Printor((char)('A'+i), i));
}
for(int i=printors.size()-1;i>=0;i--){ //逆序:依次运行
new Thread(printors.get(i)).start();
}
}
//test in main
public static void main(String[] args){
new Thread(new Print()).start();
}

private static class Printor implements Runnable{
private char name;
private int id;
private int times=0;
public Printor(char name,int id){
this.name=name;
this.id=id;
}
@Override
public void run() {
synchronized(LOCK){ //把门锁上,别人别想进来
while(this.times<TIMES)
if(this.id==run%COUNT){
System.out.print(this.name);
if(this.id==COUNT-1) System.out.println(); //换行
run++;
this.times++;
LOCK.notifyAll(); //吼一嗓子:其他的兄弟,你们准备好了吗
}else {
try {
LOCK.wait(); //我K,白来了,我得出去等(释放锁)...
} catch (InterruptedException e) {
System.out.println(this.name+"=>exception occurs...");
}
}
} //结束:释放锁
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值