小凡156

博客园 首页 新随笔 联系 订阅 管理
 1 package java.lang;
 2 
 3 /**
Runnable接口应该被要通过thread来执行的类实例来实现
这个类必须定义一个没有参数的run方法
4 * The <code>Runnable</code> interface should be implemented by any 5 * class whose instances are intended to be executed by a thread. The 6 * class must define a method of no arguments called <code>run</code>. 7 * <p>
  
  此接口旨在为希望在活动时执行代码的对象提供通用协议。
  例如,<code>runnable</code>由类<code>thread</code>实现。
  处于活动状态只意味着线程已启动但尚未停止。
8 * This interface is designed to provide a common protocol for objects that 9 * wish to execute code while they are active. For example, 10 * <code>Runnable</code> is implemented by class <code>Thread</code>. 11 * Being active simply means that a thread has been started and has not 12 * yet been stopped. 13 * <p>
  
  此外,Runnable 提供了使类不需要继承Thread也可处于活动状态的方法
14 * In addition, <code>Runnable</code> provides the means for a class to be 15 * active while not subclassing <code>Thread</code>.
  
  实现 Runnable 的类可以在不继承Thread类的情况下运行
  通过实例化一个<code>线程<code>实例并将其自身作为目标传入。
  A class that implements
16 * <code>Runnable</code> can run without subclassing <code>Thread</code> 17 * by instantiating a <code>Thread</code> instance and passing itself in 18 * as the target.
  
  在大多数情况下,如果您只计划重写<code>run()</code>方法,而不打算重写其他<code>thread</code>方法,则应使用<code>runnable</code>接口。
  In most cases, the <code>Runnable</code> interface should
19 * be used if you are only planning to override the <code>run()</code> 20 * method and no other <code>Thread</code> methods.

  这一点很重要,因为除非程序员打算修改或增强类的基本行为,否则不应该对类进行子类化。
21 * This is important because classes should not be subclassed 22 * unless the programmer intends on modifying or enhancing the fundamental 23 * behavior of the class. 24 * 25 * @author Arthur van Hoff 26 * @see java.lang.Thread 27 * @see java.util.concurrent.Callable 28 * @since JDK1.0 29 */ 30 @FunctionalInterface 31 public interface Runnable { 32 /**
      当对象实现Runnable接口被用来创建一个线程时,启动这个线程会导致对象的run方法在单独执行的线程中被调用
33 * When an object implementing interface <code>Runnable</code> is used 34 * to create a thread, starting the thread causes the object's 35 * <code>run</code> method to be called in that separately executing 36 * thread. 37 * <p>

      方法run的一般约定是它可以采取任何操作。
38 * The general contract of the method <code>run</code> is that it may 39 * take any action whatsoever. 40 * 41 * @see java.lang.Thread#run() 42 */ 43 public abstract void run(); 44 }

 总结:

1. 要通过thread来执行的类实例,并且不需要重写除了run()以外其它thread方法,则应使用runnable接口
2. 实现runnable接口必须定义一个没有参数的run方法,run方法中可以做任何操作

posted on 2019-10-31 20:30  小凡156  阅读(251)  评论(0编辑  收藏  举报