non-stop thread

Discussion in 'Java' started by Peter, Jul 4, 2003.

  1. Peter

    Peter Guest

    Hi
    I have a class that extend Thread. If i put this line
    "thread.currentThread().sleep(1000);" into the "public void run()",
    after i call interrupt(), the thread will stop.

    thanks
    from Peter ()
     
    Peter, Jul 4, 2003
    #1
    1. Advertising

  2. Peter

    Tomy Guest

    Peter <> wrote:
    > Hi
    > I have a class that extend Thread. If i put this line
    > "thread.currentThread().sleep(1000);" into the "public void run()",
    > after i call interrupt(), the thread will stop.
    >
    > thanks
    > from Peter ()



    Did you catch InterruptedException which is being thrown if you
    call interrupt on Thread while in sleep().....?????

    --
    Tomy.
    -----------------------
     
    Tomy, Jul 4, 2003
    #2
    1. Advertising

  3. Peter

    Peter Guest

    Hi

    I have catch InterruptedException , but the thread still running even
    you call "interrupt()"

    from Peter
     
    Peter, Jul 4, 2003
    #3
  4. Peter

    Peter Guest

    Hi

    Nothing want to ask, just want to share my experimence.

    from Peter
     
    Peter, Jul 4, 2003
    #4
  5. Peter

    Peter Guest

    This is my Thread template, even call sleep() in run(), it will stop
    when you call interrupt()..

    class MyThread extends Thread {
    private volatile boolean shouldRun = false;

    public void start() {
    shouldRun = true;
    new Thread(this).start();
    }

    public void suspendThread() {
    shouldRun = false;
    }

    public boolean isRunning() {
    return shouldRun;
    }

    public void run() {
    Thread thisThread = Thread.currentThread();
    while (shouldRun) {
    try {
    sleep(50);
    } catch (InterruptedException e) {
    }
    }
    }
    }

    thanks
    from Peter ()
     
    Peter, Jul 4, 2003
    #5
  6. Peter

    Sergio Juan Guest

    "Peter" <> escribió en el mensaje
    news:...
    > This is my Thread template, even call sleep() in run(), it will stop
    > when you call interrupt()..
    >
    > class MyThread extends Thread {
    > private volatile boolean shouldRun = false;
    >
    > public void start() {
    > shouldRun = true;
    > new Thread(this).start();


    ???
    It should not even compile IMHO, there is no constructor Thread(Thread) in
    java.lang.Thread.
    I don't understand this.

    Regards.
     
    Sergio Juan, Jul 4, 2003
    #6
  7. On Thu, 03 Jul 2003 23:23:54 -0700, Peter wrote:

    > Hi
    > I have a class that extend Thread. If i put this line
    > "thread.currentThread().sleep(1000);" into the "public void run()",
    > after i call interrupt(), the thread will stop.
    >
    > thanks
    > from Peter ()


    Do you have a question? If so, I suggest you post some code with it so
    others can see more clearly what you're on about.

    Steve
     
    Steve Horsley, Jul 4, 2003
    #7
  8. On Fri, 04 Jul 2003 06:33:50 -0700, Peter wrote:

    > This is my Thread template, even call sleep() in run(), it will stop
    > when you call interrupt()..
    >
    > class MyThread extends Thread {
    > private volatile boolean shouldRun = false;
    >
    > public void start() {
    > shouldRun = true;
    > new Thread(this).start();
    > }
    > }
    > public void suspendThread() {
    > shouldRun = false;
    > }
    > }
    > public boolean isRunning() {
    > return shouldRun;
    > }
    > }
    > public void run() {
    > Thread thisThread = Thread.currentThread();
    > while (shouldRun) {
    > try {
    > sleep(50);
    > } catch (InterruptedException e) {
    > }
    > }
    > }
    > }
    > }
    > thanks
    > from Peter ()


    Sorry to be rude, but this is horrible.

    MyThread extends Thread but also overrides start() such that instead of
    starting the MyThread thread when start() is called, it creates and starts
    a different Thread instead.

    I don't know how you are excercising this MyThread class, but I suspect
    that your apparent problem derives form confusion over which of the two
    threads is running.

    I suggest that in order to avoid confusion, you do NOT extend Thread at
    all, but instead use normal classes that implement Runnable, and create
    ordinary Threads as required.

    Steve
     
    Steve Horsley, Jul 4, 2003
    #8
  9. On Fri, 04 Jul 2003 14:11:45 +0000, Sergio Juan wrote:

    > "Peter" <> escribió en el mensaje
    > news:...
    >> This is my Thread template, even call sleep() in run(), it will stop
    >> when you call interrupt()..
    >>
    >> class MyThread extends Thread {
    >> private volatile boolean shouldRun = false;
    >>
    >> public void start() {
    >> shouldRun = true;
    >> new Thread(this).start();

    >
    > ???
    > It should not even compile IMHO, there is no constructor Thread(Thread) in
    > java.lang.Thread.
    > I don't understand this.
    >
    > Regards.


    But Thread implements Runnable and therefore so does MyThread. And there
    is a Thread(Runnable) constructor.

    Steve
     
    Steve Horsley, Jul 4, 2003
    #9
  10. Peter

    Sergio Juan Guest

    Ups... Ok. Thanks for the hint.

    "Jon Skeet" <> escribió en el mensaje
    news:...
    > Sergio Juan <> wrote:
    > > ???
    > > It should not even compile IMHO, there is no constructor Thread(Thread)

    in
    > > java.lang.Thread.
    > > I don't understand this.

    >
    > Thread implements Runnable, and there's Thread(Runnable) constructor.
    >
    > --
    > Jon Skeet - <>
    > http://www.pobox.com/~skeet/
    > If replying to the group, please do not mail me too
     
    Sergio Juan, Jul 5, 2003
    #10
  11. On Sun, 06 Jul 2003 19:05:55 -0700, Peter wrote:

    > Thanks for the opinions
    > If implement Runnable, and you call "sleep()" within run(). After
    > you call interrupt(), then thread won't stop too.
    >
    > from Peter


    It's not clear whether you want the thread to stop or not.
    Anyway, here's an example where the thread doesn't stop when interrupted.
    Uncomment the line in the loop to make it stop when it is interrupted.

    Steve


    /** QuickTest.java */
    public class QuickTest implements Runnable {

    private volatile boolean shouldRun = false;

    public static void main(String[] args) {
    // Create and start a QuickTest
    QuickTest qt = new QuickTest();
    Thread t = new Thread(qt);
    t.start();

    // wait 5 seconds
    try {
    Thread.sleep(5000);
    } catch(InterruptedException ie) {
    System.out.println("Main thread interrupted - shouldn't happen");
    }

    // interrupt the test thread
    t.interrupt();

    // wait another 5 seconds
    try {
    Thread.sleep(5000);
    } catch(InterruptedException ie) {
    System.out.println("Main thread interrupted - shouldn't happen");
    }
    }

    /** Loop forever. */
    public void run() {
    shouldRun = true;
    while(shouldRun) {
    System.out.println("Snooze...");
    try {
    Thread.sleep(1000);
    } catch(InterruptedException ie) {
    System.out.println("Sleeper loop was interrupted.");
    shouldRun = false;
    }
    }
    System.out.println("Sleeper loop terminating");
    }



    }
    //=============================================================
     
    Steve Horsley, Jul 7, 2003
    #11
  12. On Mon, 07 Jul 2003 10:40:51 +0100, Steve Horsley wrote:

    > On Sun, 06 Jul 2003 19:05:55 -0700, Peter wrote:
    >
    >
    > It's not clear whether you want the thread to stop or not.
    > Anyway, here's an example where the thread doesn't stop when interrupted.
    > Uncomment the line in the loop to make it stop when it is interrupted.
    >


    Oops, I lied.
    The posted example _does_ stop. Comment out the line:
    shouldRun = false;
    to make it carry on even after an interrupt.

    Steve
     
    Steve Horsley, Jul 7, 2003
    #12
  13. Peter

    Dale King Guest

    "Steve Horsley" <_SPAM.net> wrote in message
    news:p_SPAM.net...
    > On Thu, 03 Jul 2003 23:23:54 -0700, Peter wrote:
    >
    > > Hi
    > > I have a class that extend Thread. If i put this line
    > > "thread.currentThread().sleep(1000);" into the "public void run()",
    > > after i call interrupt(), the thread will stop.
    > >
    > > thanks
    > > from Peter ()

    >
    > Do you have a question? If so, I suggest you post some code with it so
    > others can see more clearly what you're on about.



    I'm gathering that his question involves the fact that the thread is not
    interrupted at the instant that interrupt is called on it. If that is the
    question, then that is the way it is supposed to work.

    It will only be interrupted when in a wait, join, sleep, or blocked in
    certain I/O operations in the New I/O API. If it is not currently blocked
    all that will happen is that the threads interrupted flag is set so that it
    will be interrupted when it does do a blocking operation or you can also
    query the status of the interrupt flag with the interrupted method (which
    also clears the flag).
    --
    Dale King
     
    Dale King, Jul 8, 2003
    #13
    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. Matt Theule

    Stop Debugging doesn't stop in ASP.NET

    Matt Theule, Jul 23, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    754
    Matt Theule
    Jul 24, 2003
  2. Son KwonNam
    Replies:
    11
    Views:
    2,624
    mr_organic
    Apr 9, 2004
  3. Will
    Replies:
    1
    Views:
    15,349
    Thomas Weidenfeller
    Nov 2, 2004
  4. Benji
    Replies:
    34
    Views:
    1,204
    pkriens
    Oct 28, 2005
  5. Lialie - KingMax
    Replies:
    1
    Views:
    306
    John Henry
    Dec 15, 2006
Loading...

Share This Page