FileDocCategorySizeDatePackage
RandomCharacterGenerator.javaAPI DocExample1264Sun Feb 29 18:37:36 GMT 2004javathreads.examples.ch02.example4

RandomCharacterGenerator

public class RandomCharacterGenerator extends Thread implements CharacterSource

Fields Summary
static char[]
chars
static String
charArray
Random
random
CharacterEventHandler
handler
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 (!isInterrupted()) {
            nextCharacter();
            try {
                Thread.sleep(getPauseTime());
            } catch (InterruptedException ie) {
                return;
            }
        }