This discussion is archived
1 2 Previous Next 16 Replies Latest reply: May 14, 2008 4:24 PM by 807591 RSS

start, stop thread

807591 Newbie
Currently Being Moderated
Hello,

I'm trying to create a thread and to create my own start and stop methos as is showen in http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html.
But when I want to create the stop() methot my Eclipse says that it is not possible to redefine the final method stop of the Thread class. But in the example given on the link they use the redefinition of the stop method... So I'm little bit confused :) Can someone explain this?

Here is my code:
public class MyClass extends Thread {

  private volatile Thread t;

  public MyClass(){
    //this is my constructor
  }

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

  //here is my stop() method
  public void stop(){
    t = null;
  }

  public void run(){

    Thread thisThread = Thread.currentThread();
        while (t == thisThread) {
            try {
                thisThread.sleep(interval);
            } catch (InterruptedException e){
            }
            //do something ...
        }
   }

}
May be instead of using extend Thread, I sould use the implement Runnable ?
  • 1. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    You're not even in the ball park. Start with the tutorial:
    http://java.sun.com/docs/books/tutorial/essential/concurrency/
  • 2. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    sorry but this not answers to my question ... and the link you gave me I know it well and I was there many times. I need an explanation ...
  • 3. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    What are you trying to accomplish by overriding start and stop?

    Typically, you don't want to subclass Thread unless you have a good reason. If you want to start or stop a thread with your own actions, do this:
    Runnable runnable = new Runnable() {
       public void run() {
          //do my actions
       }
    Thread thread = new Thread(runnable);
    thread.start();
    thread.stop();
    Even if you want to add some functionality as a wrapper over a thread, I think you'd still be best to use composition instead of inheritance.
  • 4. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    I'm trying just to do the example shown on the : http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html.
    That all, and in fact my class was extending the Thread class but when I started to redefine the method stop, there was an error telling me that it is not possible to redefine the final method stop() of the class Thread. So I asked my self, is the example given on http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html is written about a class that extends the Thread class or is written about a class that implements the Runnable interface. So I'm trying to understand what is the differentce between the use of the Thread class and the Runnable interface, why it is more preferable to use the Runnable and so on ...
  • 5. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    If you've taken the tutorial then you haven't learned anything.

    You can never, ever override the start() method. Obviously you have no idea what you are doing.

    Google for thread.stop() of something similar. There are many, many examples of how to "inform a thread that you want it to end."

    The example you mention is wrong.
  • 6. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    If you're talking about the example under "What should I use instead of Thread.stop?" then, well that is subclassing an applet, not a Thread. The start and stop methods come from the Applet API, not the thread API, and they should not be confused.
  • 7. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    endasil,

    thanks for the answer :), yes I'm understanding now that in the example that I found on the Sun's Java site may be they are talking about two different methods, called the same way.

    Regards,

    Anton

    PS. Some words to cooper6, do you think that on the java.sun.com there will be a wrong example ?!
  • 8. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    PS. Some words to cooper6, do you think that on the java.sun.com there will be a wrong example ?!
    But of course. Just look at the bug parade sometime. It's called being human.
  • 9. Re: start, stop thread
    EJP Guru
    Currently Being Moderated
    You can never, ever override the start() method
    Sure you can. It isn't final. But if you don't call super.start(), no new thread of execution will begin.
  • 10. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    The start method yes, but the stop is final ..
  • 11. Re: start, stop thread
    EJP Guru
    Currently Being Moderated
    Yes. So?
  • 12. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    When requesting a thread to terminate, you should use the Interupt mechanism as well as a flag. (You can also use the interrupted flag as the loop test.

    If the thread is in a sleep/wait call when you stop it calling interrupt on the thread will wake it up and cause an interrupted exception, which the thread should respond to by closing up. So the thread will terminate immediately, rather than continuing until it hits the loop test.

    Most background threads speand most of their time in sleep or wait.

    I typically do something like:
    private Thread t;
    
    public void run() {
         try {
             while(!Thread.interrupted()) {
                ...
               Thread.sleep(...); 
               ...
              }
         catch(InterruptedException ex) {
               // expception breaks for loop
            }
         // thread tidies up here
        }
    
    public void startMe() {
         t = new Thread(this, "My background thread");
         t.start();
    }
    
    public void stopMe() throws InterruptedException {
        t.interrupt();  // wake up and die
           t.join();  // wait for thread to wind up
        }
  • 13. Re: start, stop thread
    807591 Newbie
    Currently Being Moderated
    nothing :) ... the question was about the stop method.
  • 14. Re: start, stop thread
    EJP Guru
    Currently Being Moderated
    nothing :) ... the question was about the stop method.
    No, the thread is about the start() and the stop() methods.

    The posting you replied to was about the start() method. The posting it replied to was also about the start() method.

    So your posting was irrelevant.
1 2 Previous Next