为什么Thread实现了Runnable? [英] Why does Thread implement Runnable?

查看:170
本文介绍了为什么Thread实现了Runnable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Thread的 run()方法。要给线程做一些事情,你可以创建一个Thread的子类并覆盖它的run()方法,或者(首选)你可以为线程的构造函数提供一个Runnable。那没关系。

A Java Thread's run() method is called by the JVM, on that thread, when the thread starts. To give a thread something to do, you can make a subclass of Thread and override its run() method, or (preferred) you can supply a Runnable to the thread's constructor. That's fine.

我正在制作一个Thread的子类并重写run,我意识到我无法使该方法受到保护,因为我预期因为Thread .run()是公开的。然后我意识到了原因:它必须是公共的,因为Thread实现了Runnable。但为什么它实现Runnable?

I was in the midst of making a subclass of Thread and overriding run, and I realized I couldn't make the method protected as I expected to because Thread.run() is public. Then I realized why: it has to be public because Thread implements Runnable. But why does it implement Runnable?

这似乎不合逻辑。一个线程是 startable (来自当前线程),但你没有像运行()一个Runnable(来自当前线程)那样运行它;线程自行运行(在自己的线程上)。如果您手动调用Thread的run方法,那么您不会将它用作Thread,只是一个重量级的Runnable。

It doesn't seem logical. A thread is startable (from the current thread), but you don't run it in the same way you run() a Runnable (from the current thread); the thread runs itself (on its own thread). If you do call a Thread's run method manually, then you're not using it as a Thread, just a heavyweight Runnable.

由于设计原因,任何具有访问权限的代码一个Thread对象可以调用它的公共运行方法,并可能戳入不打算公开或设计为以这种方式调用的代码。它还允许非常奇怪的事情:

Because of the design, any code with access to a Thread object can call its public run method and potentially poke into code that is not intended to be public or designed to be called that way. It also allows very peculiar things like this:

Thread.currentThread.run();

是否有合法使用Thread实现Runnable,我没有看到?

Is there a legitimate use for Thread implementing Runnable that I'm not seeing?

推荐答案

原因是向后兼容性。

线程类源自Java 1.0 ...或更早版本。在那些日子里,Java没有内部类,因此没有一种轻量级的方法来实现 Runnable 实例。如果您查看那个时代的旧线程示例和教程,通常会看到扩展 Thread 的类并覆盖 run()方法。

The Thread class originated in Java 1.0 ... or earlier. In those days, Java didn't have inner classes, and so there wasn't a light-weight way to implement a Runnable instance. If you look at old threading examples and tutorials from that era, it is common to see classes that extend Thread and override the run() method.

随着时间的推移,人们意识到扩展 Thread 不是一个好主意(对于很多原因)。但是,无法更改 Thread 设计,因为这会使旧的Java代码与较新的JVM不兼容。

Over time, it was realized that extending Thread is not a good idea (for various reasons). However, the Thread design could not be changed because that would have made old Java code incompatible with newer JVMs.


是否有合法使用Thread实现Runnable而我没有看到?

Is there a legitimate use for Thread implementing Runnable that I'm not seeing?

这取决于你所说的合法。

It depends what you mean by "legitimate".


  • 早期写的旧代码以旧方式做事,日子不是非法的。没有什么破坏它。

  • Old code that was written in the early days is not "illegitimate" by virtue of doing things the old way. There is nothing "broken" about it.

潜在的场景,扩展线程并覆盖 run()方法。例如,您可能希望 run()实现一些特殊的机制,用于将信息传入或传出提供的 Runnable ,或者实现一些特殊的异常处理,或者......让线程可以重启。

There are potentially scenarios where it does make sense to extend Thread and override the run() method. For example, you might want run() to implement some special mechanism for passing info in or out of the supplied Runnable, or implement some special exception handling, or ... make the thread "restartable".

甚至可能有你想要打电话的情况<直接在线程对象上code> run()。例如,如果你交了一些扩展 Thread 的狗早餐代码,你必须将它转换为在线程池中运行而不用修改原始代码。您可以考虑实例化crufty线程类并将实例作为runnables传递给线程池来运行。 (是的......太可怕了!)

There might even be scenarios where you'd want to call run() directly on a thread object. For instance if you were handed some "dogs breakfast" code that extended Thread and you had to convert it to run in a thread pool without modifying the original code. You might consider instantiating the crufty thread class and passing the instances as runnables to the threadpool to run. (Yes ... horrible!)

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

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