FileDocCategorySizeDatePackage
RandomCharacterGenerator.javaAPI DocExample1257Sun Feb 29 18:49:12 GMT 2004javathreads.examples.ch02.example5

RandomCharacterGenerator

public class RandomCharacterGenerator extends Object implements Runnable, CharacterSource

Fields Summary
static char[]
chars
static String
charArray
Random
random
CharacterEventHandler
handler
private volatile boolean
done
Constructors Summary
public RandomCharacterGenerator()


      
        random = new Random();
	handler = new CharacterEventHandler();
    
Methods Summary
public voidaddCharacterListener(CharacterListener cl)

        handler.addCharacterListener(cl);
    
public intgetPauseTime()

        return (int) (Math.max(1000, 5000 * random.nextDouble()));
    
public voidnextCharacter()

	handler.fireNewCharacter(this,
				(int) chars[random.nextInt(chars.length)]);
    
public voidremoveCharacterListener(CharacterListener cl)

        handler.removeCharacterListener(cl);
    
public voidrun()

        while (!done) {
	    nextCharacter();
	    try {
	        Thread.sleep(getPauseTime());
	    } catch (InterruptedException ie) {
	        return;
	    }
	}
    
public voidsetDone()

        done = true;