questions on implementing Runnable

Possible Duplicate:
Java: “implements Runnable” vs. “extends Thread”

I have two questions about multithreaded programming. I read a few answers on the internet, but still I can't find a satisfying answer.

  • Implementing the Runnable is preferred over extending the thread class. Why?

  • How is it that we are able to get away with overriding just the run() method?

  • According to 'The Complete Reference to Java' by Herbert Schildt, If we are not overriding any methods of Thread class other than run(), it's better we implement Runnable.

    My 2nd question might sound a bit silly, but I seem to be missing something and I'm not sure about how the whole thing works.


    1: Implementing the Runnable is preferred over extending the thread class. why?

    Because it allows your class to extend another class if it wants to instead of being forced to extend Thread .

    2: How is it that we are able to get away with over-ridding just the run() method?

    You can certainly override other methods but the Thread object will call the run() method when the thread is started. That's how it works. The default Thread.run() method is:

    public void run() {
        if (target != null) {
            target.run();
        }
    }
    

    If you call the Thread constructor with a Runnable then that is what the target is set to. If you instead extend Thread and @Override the run() method then that is the method that will be called when the thread object is started.

    That's how the Thread class works.

    2: How is it that we are able to get away with over-ridding just the run() method?

    You may have misspoke here and were instead asking why we only need to implement the run() method in Runnable() .

  • Runnable is an interface with a single run() method that you must implement.
  • Thread is a concrete class that you are extending. You can override any methods in a concrete class unless the class is final or the method is final .
  • Make sure you use proper Java terminology and don't mixup implement and override .


    Three fundamentals before you delve deeper.

  • A class can only extend one class.
  • A class can implement any number of interfaces.
  • An interface can extend any number of interfaces.
  • Additionally, an abstract class is a class. So it behaves like a class and you can't implement it. You only extend it.


    There are a number of traps which can occur if you extend Thread which you don't get with implementing Runnable.

    public static String getName() {
        return "My Test Application";
    }
    
    public static void stop() {
        System.out.println("Shutting down");
    }
    
    public static void main(String... args) {
        new Thread() {
            @Override
            public void run() {
                System.out.println(getName());
                stop();
            }
        }.run();
        System.out.println("Bye now.");
    }
    

    prints

    Thread-0
    Bye now.
    

    See how many bugs you can spot.

    链接地址: http://www.djcxy.com/p/92072.html

    上一篇: 新的线程(Runnable runnableObj)比。 扩展Thread

    下一篇: 有关实现Runnable的问题