`
mwei
  • 浏览: 121801 次
  • 性别: Icon_minigender_1
  • 来自: 抽象空间
社区版块
存档分类
最新评论

三线程顺序打印N次ABC

    博客分类:
  • java
阅读更多
记得前一阵子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...");
					}
				}
			} //结束:释放锁
		}
	}
}


分享到:
评论
3 楼 pxlfxl2 2011-04-17  
mwei 写道
pxlfxl2 写道
如果我的需求是要打印A到Z n次呢?那你就得写26个类了

你好,提出的问题很好,我在代码下面追加了另一种方法

哈哈,其实要考虑到扩展还会有其他问题,比如说用户说我不打印abc了,我要打印bca,这时你又得改代码,你可以搜搜,其实还有其他的实现,当然,就这个题目来说,你的代码已经足够了。
2 楼 mwei 2011-03-31  
pxlfxl2 写道
如果我的需求是要打印A到Z n次呢?那你就得写26个类了

你好,提出的问题很好,我在代码下面追加了另一种方法
1 楼 pxlfxl2 2011-03-29  
如果我的需求是要打印A到Z n次呢?那你就得写26个类了

相关推荐

Global site tag (gtag.js) - Google Analytics