A sentence out of my anticipation!

Discussion in 'Java' started by Jack Dowson, May 4, 2007.

  1. Jack Dowson

    Jack Dowson Guest

    Hello Everybody:
    Here is a demo to test java's multithread:
    class MyThread extends Thread{
    public void run(){
    while(true){
    System.out.println(getName() + " is running!");
    try{
    sleep(1000);
    }catch(InterruptedException e){
    System.out.println(e.getMessage());
    }
    }
    }
    }
    class InterruptThreadDemo{
    public static void main(String[] args) throws InterruptedException{
    MyThread m = new MyThread();
    System.out.println("Starting thread...");
    m.start();
    Thread.sleep(2000);
    System.out.println("Interrupt thread...");
    m.interrupt();
    Thread.sleep(2000);
    System.out.println("Stopping application...");
    }
    }


    The result is:
    Starting thread...
    Thread-0 is running!
    Thread-0 is running!
    Interrupt thread...
    Thread-0 is running!
    sleep interrupted
    Thread-0 is running!
    Thread-0 is running!
    Stopping application...
    Thread-0 is running!
    Thread-0 is running!
    Thread-0 is running!
    Thread-0 is running!
    Thread-0 is running!
    .......


    The outcome is bit out of my anticipation:
    What happened to create the sentence "sleep interrupted!"

    Any help will be greatly appreciated!
    Jack Dowson, May 4, 2007
    #1
    1. Advertising

  2. On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
    > The outcome is bit out of my anticipation:
    > What happened to create the sentence "sleep interrupted!"


    You called m.interrupt() from main(), which resulted in an exception
    in thread "m", currently sleeping. The exception contained the message
    which you then printed in the catch block in run().

    /gordon

    --
    Gordon Beaton, May 4, 2007
    #2
    1. Advertising

  3. Jack Dowson

    Jack Dowson Guest

    Gordon Beaton дµÀ:
    > On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
    >> The outcome is bit out of my anticipation:
    >> What happened to create the sentence "sleep interrupted!"

    >
    > You called m.interrupt() from main(), which resulted in an exception
    > in thread "m", currently sleeping. The exception contained the message
    > which you then printed in the catch block in run().
    >
    > /gordon
    >

    So when we interrupt a sleep thread it will become runnable!
    Right?

    Thank you!
    Jack Dowson, May 4, 2007
    #3
  4. Jack Dowson

    Jack Dowson Guest

    Gordon Beaton дµÀ:
    > On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
    >> The outcome is bit out of my anticipation:
    >> What happened to create the sentence "sleep interrupted!"

    >
    > You called m.interrupt() from main(), which resulted in an exception
    > in thread "m", currently sleeping. The exception contained the message
    > which you then printed in the catch block in run().
    >
    > /gordon
    >

    So that means if we interrupt a sleep thread then it will become
    runnable,right?

    Thank you!
    Jack Dowson, May 4, 2007
    #4
  5. Jack Dowson wrote:
    > Gordon Beaton дµÀ:
    >> On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
    >>> The outcome is bit out of my anticipation:
    >>> What happened to create the sentence "sleep interrupted!"

    >> You called m.interrupt() from main(), which resulted in an exception
    >> in thread "m", currently sleeping. The exception contained the message
    >> which you then printed in the catch block in run().
    >>
    >> /gordon
    >>

    > So that means if we interrupt a sleep thread then it will become
    > runnable,right?


    Yes, an interrupt ends a sleep etc.

    If you want to be sure to sleep for a specified time regardless of
    interrupt, you need to keep track of when the sleep is due to end, and
    put the sleep in a while loop.

    However, it is more usual to send an interrupt to a thread when it
    should stop what it is currently doing and do something else.

    Patricia
    Patricia Shanahan, May 4, 2007
    #5
  6. Jack Dowson

    Jack Dowson Guest

    Patricia Shanahan 写é“:
    > Jack Dowson wrote:
    >> Gordon Beaton ôµÀ:
    >>> On Fri, 04 May 2007 16:57:18 +0800, Jack Dowson wrote:
    >>>> The outcome is bit out of my anticipation:
    >>>> What happened to create the sentence "sleep interrupted!"
    >>> You called m.interrupt() from main(), which resulted in an exception
    >>> in thread "m", currently sleeping. The exception contained the message
    >>> which you then printed in the catch block in run().
    >>>
    >>> /gordon
    >>>

    >> So that means if we interrupt a sleep thread then it will become
    >> runnable,right?

    >
    > Yes, an interrupt ends a sleep etc.
    >
    > If you want to be sure to sleep for a specified time regardless of
    > interrupt, you need to keep track of when the sleep is due to end, and
    > put the sleep in a while loop.
    >
    > However, it is more usual to send an interrupt to a thread when it
    > should stop what it is currently doing and do something else.
    >
    > Patricia

    Thank you so much!
    Jack Dowson, May 4, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jluis
    Replies:
    3
    Views:
    688
    Riyaz
    May 5, 2004
  2. Geo
    Replies:
    0
    Views:
    466
  3. Geo
    Replies:
    6
    Views:
    515
  4. Matt
    Replies:
    11
    Views:
    52,392
    satyam000
    Aug 16, 2010
  5. Raymundo
    Replies:
    1
    Views:
    133
    Raymundo
    Apr 24, 2011
Loading...

Share This Page