美文网首页程序员码农的世界
跟 JDK 学英语(9)- Runnable

跟 JDK 学英语(9)- Runnable

作者: 码语生活 | 来源:发表于2018-11-06 22:12 被阅读6次

一、原文与翻译

@FunctionalInterface
public interface Runnable

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

如果类的实例需要在一个单独的线程中执行,那么这些类应该实现 Runnable 接口。这些类必须定义一个无参的 run 方法。

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

本接口是为了给那些在活跃时希望执行代码的对象们提供一个通用的协议而设计的。例如,Thread 类便实现了 Runnable 接口。活跃一般是指一个线程已经启动且还没有停止。

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

另外,Runnable 接口让类可以不需要继承 Thread 类也可以变成活跃的。实现了Runnable 接口的类可以不继承 Thread 类,而是通过实例化一个 Thread 实例并把自己作为目标传给该实例来获得运行。如果你只是打算覆写 Thread 类 的 run 方法,而没有覆写其他方法,那么应该使用 Runnable 接口。这很重要,因为除非程序员打算修改或加强类的基础行为,否则不应该继承类。

Since: JDK1.0
自:JDK1.0

二、词汇学习

argument: 参数
active: 活动的,活跃的
In addition: 另外,此外
intend: 打算,意向

三、句子分析

A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target.

主干:A class can run.
分短语理解:实现了 Runnable 接口的类可以运行,不需要继承 Thread 类,通过实例化一个线程实例,和传入自己作为目标。
把每个短语切割理解后,就清楚短语的修饰对象,然后拼接组装成句,便可以理解句子意思了。

四、技术要点

通常我们会把 Runnable 接口的实例传入 Thread 线程中,达到多线程运行代码的目的,Thread.run() 方法会调用 Runnable 的 run() 方法。

微信公众号.jpg

相关文章

网友评论

    本文标题:跟 JDK 学英语(9)- Runnable

    本文链接:https://www.haomeiwen.com/subject/xdouxqtx.html