Better technique for MyThread class

This question already has an answer here:

  • “implements Runnable” vs. “extends Thread” 40 answers

  • While creating a thread implementing Runnable interface is better. Because you can extend your new class from other class, otherwise you can not extend it.


    Extending a Thread is better if you want to retain some control over the thread's work, like calling interrup() . In general, I'd say that extending a Thread is OK when the class is some kind of Manger or Worker.

    In other cases, especially when a class represents some work to be done ( Job , Future ) it's much better to implement runnable .


    And in case of implementing Runnable, I have to create Thread object from MyThread object, like

    MyThread mt = new MyThread();
    Thread t = new Thread(mt);
    

    NOT NECESSARILY

    You can do this...

    Thread t = new Thread(new Runnable () { public void run() { /* do stuff */ }});
    

    ... no need for MyThread class.

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

    上一篇: 如何将多个(npm)package.json文件合并为一个Gulp文件?

    下一篇: MyThread类更好的技术