`

【迅雷】多线程同步循环打印

阅读更多

/****************************************************************
 * 迅雷笔试题:
 * 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…
 *
 * @author: heartraid
 *****************************************************************/

 

代码1:

/**
 * 锁码:公共数据区
 * 码值:码值为A,表示应该由A线程来执行,B,C线程等待
 *      码值为B,C同理。
 */
class LockCode{
	/**当前锁码码值,初始码值为A,表示最初由A线程运行*/
	private char code='A';
	/**单例模式*/
	private LockCode(){
	}
	
	public static LockCode newInstance(){
		return new LockCode();
	}
	/**
	 * 循环设置锁码
	 * 每一次调用,锁码按照A-B-C-A-...-的顺序循环往复
	 */
	public void setCode(){
		this.code=(char)(this.code+1);
		if(this.code=='D')
			this.code='A';
	}
	/**
	 * 得到锁码
	 */
	public char getCode(){
		return this.code;
	}
}
/**
 * 完成打印工作的线程类
 */
class PrintRunnable implements Runnable{
    /**需要打印的字符*/
	private char character='?';
	/**公共锁码*/
	private LockCode lockCode=null;
	
	PrintRunnable(char c,LockCode l){
		this.character=c;
		this.lockCode=l;
	}
	/**
	 * 线程执行
	 */
	public void run() {
		int loopCount=1;
		while(loopCount<=10){
			synchronized(lockCode){//线程同步操作锁码
				try{
				    //如果当前运行的线程并不等于当前锁码的码值,则改线程等待
					//比如当前运行线程是A,但是码值为B,则A线程等待。
					while(lockCode.getCode()!=this.character)
						lockCode.wait();
					//码值匹配成功,打印字符
					System.out.print(this.character);
					//循环10次记数
					loopCount++;
					//设置码值,让下一个线程可以运行
					lockCode.setCode();
					//让其他所有等待线程激活
					lockCode.notifyAll();
				}catch(InterruptedException e){  
					e.printStackTrace();  
				}  
			}
		}
	}
	
}
/**
 * 测试
 */
public class ThreadLoopPrint {
	
	public static void main(String[] args) {
		LockCode lockCode=LockCode.newInstance();//公共锁码
		Thread ta=new Thread(new PrintRunnable('A',lockCode));
		Thread tb=new Thread(new PrintRunnable('B',lockCode));
		Thread tc=new Thread(new PrintRunnable('C',lockCode));
		ta.start();
		tb.start();	
		tc.start();
	}
}

 代码2:

/**
 *此代码和上面的代码有一个很大的相同点,就是都利用公共数据区中的数据变化来决定线程工作还是阻塞等待。公共数据区利用了类静态变量。因此代码简洁。
 *
 *@author jiangtao
 *@date 2010-2-27
 *@version 1.0
 */
public class MyThread extends Thread{

    public static String[] NAMES = new String[] { "A", "B", "C" };

    public static int POS = 0;

    private static final long DURATION = 1000;
    
    private int count = 10;

    public MyThread (String name) {
        this.setName(name);
    }

    @Override
    public void run() {
        while (count > 0) {
            if (this.getName().equals(NAMES[POS])) {
                this.print();
                this.count--;
            }
            try {
                Thread.sleep(DURATION);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private synchronized void print() {
        System.out.print(this.getName());
        POS = (POS >= NAMES.length - 1 ? 0 : ++POS);
    }
    
    public static void main(String[] args) {
        new MyThread ("A").start();
        new MyThread ("B").start();
        new MyThread ("C").start();
    }
}
分享到:
评论
3 楼 sky88088 2014-11-10  
这个不是单例模式吧……还是可以生成多个实例啊
2 楼 夏广元 2014-03-11  
xiao_2008 写道
private synchronized void print() { 
        System.out.print(this.getName()); 
        POS = (POS >= NAMES.length - 1 ? 0 : ++POS); 
    } 

去掉synchronized也可以


去掉不行吧。去掉的话,可能三个线程都会抢占资源执行print方法,这样就不能保证abc的顺序了吧?
1 楼 xiao_2008 2012-08-28  
private synchronized void print() { 
        System.out.print(this.getName()); 
        POS = (POS >= NAMES.length - 1 ? 0 : ++POS); 
    } 

去掉synchronized也可以

相关推荐

Global site tag (gtag.js) - Google Analytics