aula03ThreadSleepy

class SleepyHead extends Thread {
    // RUN METHOD IS EXECUTED WHEN THREAD FIRST STARTED
    public void run() {
        System.out.println("I feel sleepy. Wake me in eight hours");
        try {
            // SLEEP FOR EIGHT HOURS
            Thread.sleep(1000 * 60 * 60 * 8);
            System.out.println("That was a nice nap");
        } catch (InterruptedException e) {
            System.err.println("Just five more minutes....");
        }
    }
}

public class aula03ThreadSleepy {

    public static void main(String args[]) throws java.io.IOException {
        // CREATE A 'SLEEPY' THREAD
        Thread sleepy = new SleepyHead();

        // START THREAD SLEEPING
        sleepy.start();

        // PROMPT USER AND WAIT FOR INPUT
        System.out.println("Press enter to interrupt the thread");
        System.in.read();

        // INTERRUPT THE THREAD (DEPRECATED: TO SAFELY TERMINATE A THREAD LET IT DIE
        // OR MAKE IT EXIT ITS METHOD RUN())
        sleepy.interrupt(); //RESUME() METHOD TO RESUME THE THREAD
    }
}
Tags : ,

0 thoughts on “aula03ThreadSleepy”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.