why there are two way of using thread in java?

This question already has an answer here:

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

  • extends Thread :

    your thread creates unique object and associate with it

  • implements Runnable :

    it shares the same object to multiple threads

  • Another thing to note, since you can extend only one class in Java, if you extends Thread , you can't extend another class. If you choose to implement Runnable , you can extend class then.


    Technically, there is only one way: implement Runnable . Thread , incidentally, does just that, so extending it you trivially satisfy the interface requirement.


    Subclassing Thread class allows you to modify other overridable functions of the Thread class, should you wish to do so. You, probably, shouldn't.

    Also, subclassing Thread class can result in a more readable code sometimes where in your subclass you can have your own custom API. One can imagine classes such as DownloadingThread, RenderingThread etc extending Thread.

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

    上一篇: 实现Runnable与扩展Thread

    下一篇: 为什么在java中使用线程有两种方式?