/* Threads 1 and 2 will sleep for a specified amount of time.
*
* 1. Thread one prints the letter 'a' 10 times.
* 2. Thread two prints the letter 'b' 10 times.
* 3. Thread three prints the integers 1 through 10.
*
*/
public class TestSleep {
    public static void main(String args[]) {

        // If there are no cmd line args, the threads won't sleep
        int sleep = args.length == 0 ? 0 : Integer.parseInt(args[0]);
        // Create threads
        PrintChar printA = new PrintChar('a', 10, sleep);
        PrintChar printB = new PrintChar('b', 10, sleep);
        PrintNum print100 = new PrintNum(10);
        
        // Start threads
        printA.start();
        printB.start();
        print100.start();
    } 
}

class PrintChar extends Thread {
    private char charToPrint; // the character to print
    private int times; // The times to repeat
    private int sleep; // amount to sleep

    // Construct a thread with specified character and number
    // of times to print the character
    public PrintChar(char c, int t, int s) {
        charToPrint = c;
        times = t;
        sleep = s;
    } 
    
    // Override the run() method to tell the system what the thread will do
    public void run() {
        for (int i=0; i < times; i++) {
            System.out.print(" " + charToPrint);
            // sleep for specified amount of time
            try {
                Thread.sleep(sleep);
            }
            catch (InterruptedException ex) {
            }
        }
    }
}

class PrintNum extends Thread {
    private int lastNum; // the last number to print

    // Construct a thread with the last number
    public PrintNum(int n) {
        lastNum = n;
    } 

    // Override the run() method to tell the system
    // what the thread will do
    public void run() {
        for (int i=0; i<=lastNum; i++) {
            System.out.print(" " + i);
        }
    } 
} 

