public class DisplayThread extends Thread{
	private String message;
	private Display disp;

	
	DisplayThread(){
		message = "";
		disp    = null;
	}



	DisplayThread(Display disp, String message){
		this.message = message;
		this.disp = disp;
	}



	private void display(){
		for(int i = 0; i < message.length(); i++){
			System.out.print(message.charAt(i));
			try{
				sleep(100);
			}
			catch(InterruptedException ie){}
		}	
	}	



	public void handleABThread(Display disp) throws InterruptedException{
		while(disp.getIsABPrinted() == true)
			disp.wait();

		display();
		disp.setIsABPrinted(true);
		disp.notify();
	}



	public void handleCDThread(Display disp) throws InterruptedException{
		while(disp.getIsABPrinted() == false)
			disp.wait();
		
		display();
		disp.setIsABPrinted(false);
		disp.notify();
	}



	public void run(){
		try{
			synchronized(disp){
				if(message.equals("ab")){
					handleABThread(disp);
				}

				if(message.equals("cd\n")){
					handleCDThread(disp);
				}
			}
		} catch(Exception e){}
	}
}
