Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用继承和多态性实例化线程子类的对象之间的主要区别是什么?_Java_Multithreading_Inheritance_Java 8_Polymorphism - Fatal编程技术网

Java 使用继承和多态性实例化线程子类的对象之间的主要区别是什么?

Java 使用继承和多态性实例化线程子类的对象之间的主要区别是什么?,java,multithreading,inheritance,java-8,polymorphism,Java,Multithreading,Inheritance,Java 8,Polymorphism,当我们有一个类,比如AnotherThread,它扩展了Thread,并且我们重写了其中的run方法时,下面两个代码在多线程中的主要区别是什么。 什么时候我们需要使用多态性来创建另一个线程类的实例? 当我们必须使用继承来创建另一个线程类的线程时 这是Thread的一个子类: public class AnotherThread extends Thread{ @Override public void run() { System.out.println(&qu

当我们有一个类,比如AnotherThread,它扩展了Thread,并且我们重写了其中的run方法时,下面两个代码在多线程中的主要区别是什么。 什么时候我们需要使用多态性来创建另一个线程类的实例? 当我们必须使用继承来创建另一个线程类的线程时

这是Thread的一个子类:

public class AnotherThread extends Thread{

    @Override
    public void run() {
        System.out.println("Hello from another thread.");
    }
}
public static void main(String[] args) {

    System.out.println("This is the main thread.");


    Thread anotherThread = new AnotherThread();
    anotherThread.start();
public static void main(String[] args) {

    System.out.println("This is the main thread.");

    AnotherThread anotherThread_2 = new AnotherThread();
    anotherThread_2.start();
在这段代码中,为什么我们使用多态性来实例化另一个Thread类的对象,其中另一个Thread是Thread的子类:

public class AnotherThread extends Thread{

    @Override
    public void run() {
        System.out.println("Hello from another thread.");
    }
}
public static void main(String[] args) {

    System.out.println("This is the main thread.");


    Thread anotherThread = new AnotherThread();
    anotherThread.start();
public static void main(String[] args) {

    System.out.println("This is the main thread.");

    AnotherThread anotherThread_2 = new AnotherThread();
    anotherThread_2.start();
在这段代码中,我们为什么使用继承来实例化另一个Thread类的对象,其中另一个Thread是Thread的子类:

public class AnotherThread extends Thread{

    @Override
    public void run() {
        System.out.println("Hello from another thread.");
    }
}
public static void main(String[] args) {

    System.out.println("This is the main thread.");


    Thread anotherThread = new AnotherThread();
    anotherThread.start();
public static void main(String[] args) {

    System.out.println("This is the main thread.");

    AnotherThread anotherThread_2 = new AnotherThread();
    anotherThread_2.start();

线程的实例化没有任何区别。唯一的区别在于保存线程引用的变量的类型

第一个问题:你是否理解对象引用对象之间的区别?变量不是对象,它持有引用


所以问题是,您希望您的程序将该线程视为一般线程(第一个示例)还是另一个线程(第二个示例)?这两者都是有效的,这取决于你想要完成什么。一般来说,如果后续代码不需要将其作为另一个线程进行专门处理,则最好使用线程接口编程。

这两个代码块的工作原理完全相同。它们的实例化完全相同(两个块中都有新的另一个线程()唯一的区别是在第二个块中,如果要在另一个线程中调用添加的功能,则需要对另一个线程进行强制转换。这取决于另一个线程偏离基类的确切行为。同样,作为提示,最好实现Runnable或Callable,然后构造一个简单的具有Runnable/Callable的java线程。这样做的一个好处是,如果需要的话,它允许您同步调用Runnable/Callable。另一个好处是在Runnable/Callable中安全地使用wait/notify/notifyAll方法;多态性是对任何类型的
线程
使用
线程
变量的能力,包括
其他线程
。根据定义实例化从来都不是多态的,因为它会立即创建实际的具体对象。