为什么我应该使用Runnable而不是Thread?

问题描述:

我只是学习关于线程的理论。还有Thread和Runnable。

I just learn the theory about thread. And there is Thread and Runnable.

class A extends Thread{
    public void run(){
            while(true) {
                System.out.println("Hi");
            }           
        }
    }







class B implements Runnable{
    public void run(){
        System.out.println("Hi");
    }
}

线程适用于丰富的API,为什么我会这样做使用Runnable而不是Thread?

Thread is good with rich API, so why would I use Runnable instead of Thread?

谢谢。

1。 Java不支持多重继承,这意味着你只能扩展一个Java类,所以一旦你扩展了 Thread 类,你就失去了机会而不能扩展(继承)java中的另一个类。

1. Java doesn't support multiple inheritance, which means you can only extend one Java class, so once you extended Thread class you lost your chance and cannot extend(inherit) another class in java.

2. 在OOP中扩展类通常意味着添加新功能,修改或改进行为。如果您未对 Thread 进行任何修改,请改用 Runnable 界面。

2. In OOP extending a class generally means adding new functionality, modifying or improve behaviors. If you are not making any modification on Thread, then use Runnable interface instead.

3. 实施 Runnable 使您的课程更加灵活(您可以实施多个界面)。

3. Implementing Runnable makes your class more flexible(you can implement more than one interface).