FileDocCategorySizeDatePackage
RandomCharacterGenerator.javaAPI DocExample1407Mon Apr 05 09:13:28 BST 2004javathreads.examples.ch03.example1

RandomCharacterGenerator

public class RandomCharacterGenerator extends Thread implements 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;