为什么我应该使用Runnable而不是Thread? [英] Why should I use Runnable instead of Thread?

查看:125
本文介绍了为什么我应该使用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).

这篇关于为什么我应该使用Runnable而不是Thread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆