跟 JDK 學英語(9)- Runnable
一、原文與翻譯
ofollow,noindex">@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