java 为什么在java中有两种使用线程的方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17311842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:39:09  来源:igfitidea点击:

why there are two way of using thread in java?

javamultithreadinginheritanceinterface

提问by TKumar

I know there are two ways to use a thread in java:

我知道在java中有两种使用线程的方法:

  1. implement Runable
  2. extend Thread
  1. 实现可运行
  2. 扩展线程

I also know implementing Runableis better than extending Thread.

我也知道实现 Runable比扩展Thread更好 。

But why there are two ways - why not only one?

但为什么有两种方式——为什么不只有一种?

If implementing Runnableis a better approach, why is there another option?

如果实施Runnable是一种更好的方法,为什么还有另一种选择?

What would be wrong with having only one option ?

只有一种选择会有什么问题?

回答by Maroun

  • extends Thread:

    your thread creates unique object and associate with it

  • implements Runnable:

    it shares the sameobject to multiple threads

  • extends Thread

    您的线程创建独特的对象并与之关联

  • implements Runnable

    它与多个线程共享同一个对象

Another thing to note, since you can extendonly 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.

另一件要注意的事情,因为在 Java 中你只能扩展一个类,如果你extends Thread,你不能扩展另一个类。如果您选择implement Runnable,则可以扩展类。

回答by Amadan

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

从技术上讲,只有一种方法:实施RunnableThread顺便说一句,就是这样做的,因此扩展它您可以轻松满足接口要求。

回答by blganesh101

Just another reason why we use each type of threading.

我们使用每种线程的另一个原因。

Extending Threadclass will not give you an option to extend any other class. But if you implement Runnableinterface you could extend other classes in your class..

扩展Thread类不会让您选择扩展任何其他类。但是如果你实现了Runnable接口,你可以在你的类中扩展其他类..

So depending on your design requirement you could use either of the menthods.

因此,根据您的设计要求,您可以使用任何一种方法。

回答by Alexander Kulyakhtin

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

如果您愿意,子类化Thread类允许您修改类的其他可覆盖函数Thread。你,可能,不应该。

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.

此外,子类化 Thread 类有时会产生更具可读性的代码,而在您的子类中,您可以拥有自己的自定义 API。可以想象像 DownloadingThread、RenderingThread 等扩展 Thread 的类。

回答by Evgeniy Dorofeev

Using Thread as a task provides a compact way to create and run a parallel thread

使用线程作为任务提供了一种创建和运行并行线程的紧凑方式

new Thread() {
    public void run() {
        ...
    }
}.start();

回答by Manish Doshi

Both methods have different approaches. Implementing Runnable interface does not give any control over thread itself.And if we extends thread class then derived class can not extend any other base class.

两种方法都有不同的方法。实现 Runnable 接口不会对线程本身进行任何控制。如果我们扩展线程类,那么派生类就不能扩展任何其他基类。

So if user wants fully control over program then Extending of Thread class is better option and if user wants flexibility of extending other base classes then Implementing Runnable Interface is good option.

因此,如果用户想要完全控制程序,那么扩展 Thread 类是更好的选择,如果用户想要扩展其他基类的灵活性,那么实现 Runnable 接口是不错的选择。

回答by Saurabh

Even if you implement Runnable interface you will need to create thread to let your task run as a thread. obvious advantages you get out implementing Runnable are

即使您实现了 Runnable 接口,您也需要创建线程来让您的任务作为线程运行。实施 Runnable 的明显优势是

  1. You have liberty to extend any other class
  2. You can implement more interfaces
  3. You can use you Runnable implementation in thread pools
  1. 您可以自由扩展任何其他类
  2. 你可以实现更多的接口
  3. 您可以在线程池中使用 Runnable 实现

Extending Thread class is just an option as it implements Runnable internally so you end up implementing Runnable interface indirectly. Its just that this class was not made final to prevent developers from extending it. As Joshua Bloch mentions in 'Effective Java' that there should be no reason to extend Thread usually

扩展 Thread 类只是一个选项,因为它在内部实现了 Runnable,因此您最终间接实现了 Runnable 接口。只是这个类不是最终的,以防止开发人员扩展它。正如 Joshua Bloch 在“Effective Java”中提到的那样,通常没有理由扩展 Thread

回答by Rajan Garg

By extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same runnable instance.

通过扩展 Thread,您的每个线程都有一个与之关联的唯一对象,而实现 Runnable,许多线程可以共享相同的可运行实例。

回答by Sajal Saha

When we create thread by implements runnable we don't initialize any value during thread creation. But when we extend Thread class to create thread we can initialize some value if needed using the advantage of constructor as follw

当我们通过实现 runnable 创建线程时,我们不会在线程创建期间初始化任何值。但是当我们扩展 Thread 类来创建线程时,我们可以根据需要使用构造函数的优势初始化一些值,如下所示

public class MyThread extends Thread
{
 int aValue;
 public Mythread(int aValue)
 {
 this.aValue = aValue; 
 }
 ............................
 ............................

}

When we create thread we can initialize as follow

当我们创建线程时,我们可以初始化如下

MyThread t = new MyThread(7);
t.start();

Beside that since java doesn't support Multiple inheritance so we if extends Thread class then we lost our opportunity to extends another class.Hence in this scenario runnable interface is so helpful for creating thread

除此之外,由于java不支持多重继承,所以如果我们扩展了Thread类,那么我们就失去了扩展另一个类的机会。因此在这种情况下可运行的接口对于创建线程非常有帮助

回答by Keane_zhou

Most of the time, we use runnable interface. Because that allows us more flexible on the structure and functionality. Java allows multiple inheritance for interface. That concept is used quite a lot in Software Design.

大多数时候,我们使用可运行的接口。因为这让我们在结构和功能上更加灵活。Java 允许接口的多重继承。这个概念在软件设计中被大量使用。

By the way, there is another interface called callable. A callable class could return value in type V and also may throw checked exceptions.

顺便说一下,还有一个叫做 callable 的接口。一个可调用的类可以返回 V 类型的值,也可以抛出已检查的异常。