/* Three independent threads:
*
* 1. Thread one prints the letter 'a' 500 times.
* 2. Thread two prints the letter 'b' 500 times.
* 3. Thread three prints the integers 1 through 500.
*
*/
public class TestRunnable {
    public static void main(String args[]) {
        // Create threads
        Thread printA = new Thread (new PrintChar('a', 500));
        Thread printB = new Thread (new PrintChar('b', 500));
        Thread print500 = new Thread (new PrintNum(500));
    
        printA.start();
        printB.start();
        print500.start();

        System.out.println("\n Main Terminating.");
    } 
} 

class PrintChar implements Runnable {
    private char charToPrint; // the character to print
    private int times; // The times to repeat
    
    // Construct a thread with specified character and number
    // of times to print the character
    public PrintChar(char c, int t) {
        charToPrint = c;
        times = t;
    } 
    
    // 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);
        }
    } 
} 

class PrintNum implements Runnable {
    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);
        }
    } 
} 
