package demoThreads;

public class ThreadTest extends Thread {
    public static final int M= 100;
    public static final int R= 2000;
    
    public static void main(String[] args) {
        new ThreadTest().start();
        for (int h= 0; true; h= h+1) {
            try {
                sleep(M);
            } catch (InterruptedException e) {
                System.out.println("in run, InterruptedException thrown");
            }
            System.out.format("%s %d\n", Thread.currentThread(), h);
        }
        
     }

    /** Write out message every runR milliseconds. */
    @Override public void run() {
        
        for (int k= 0; true; k= k+1) {
            try {
                sleep(R);
            } catch (InterruptedException e) {
                System.out.println("in run, InterruptedException thrown");
            }
            System.out.format("%s %d\n", Thread.currentThread(), k);
        }
     }
    
    /** SEE WHY THE try-catch around sleep(..) IS NEEDED */
    public void testSleep() {
        //  Method sleep can throw a checked InterruptedException .
        //  "Checked' means that it must either 
        //  (1) sleep(...) be in a try-statement that has an
        //      appropriate catch clause or
        //  (2) the method must have a suitable throws clause.
        //  You can see this for yourself by uncommenting the line below
        //  with "sleep(5);" on it.
        //sleep(5);
    }
}
