商城微网站建设方案,广告联盟官网入口,济南网站制作0531soso,网页设计师的主要职责版本 JDK8(JDK1.8)
Runnable接口源码重点 1.Runnable是一个函数式接口,里面只有一个run()方法,可以重写run()方法,并将Runnable传递进Thread类,开启一个新线程执行Runnable中的run()方法
2.Runnable接口的改进版是Callable接口&…
版本
JDK8(JDK1.8)
Runnable接口源码重点
1.Runnable是一个函数式接口,里面只有一个run()方法,可以重写run()方法,并将Runnable传递进Thread类,开启一个新线程执行Runnable中的run()方法
2.Runnable接口的改进版是Callable接口,Callable接口call()方法会返回结果,还能抛出已检查的异常
Callable源码可以看我这篇文章 Callable
Runnable接口源码
package java.lang;/*** The <code>Runnable</code> 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 <code>run</code>.* 可运行接口应该由其实例将由线程执行的任何类实现。* 该类必须定义一个名为run的无参数方法。* * * <p>* This interface is designed to provide a common protocol for objects that* wish to execute code while they are active. For example,* <code>Runnable</code> is implemented by class <code>Thread</code>.* Being active simply means that a thread has been started and has not* yet been stopped.* 此接口旨在为希望在激活时执行代码的对象提供通用协议。* 例如,Runnable由类线程实现。处于活动状态仅仅意味着线程已经启动,但尚未停止。* * * * <p>* In addition, <code>Runnable</code> provides the means for a class to be* active while not subclassing <code>Thread</code>. A class that implements* <code>Runnable</code> can run without subclassing <code>Thread</code>* by instantiating a <code>Thread</code> instance and passing itself in* as the target. In most cases, the <code>Runnable</code> interface should* be used if you are only planning to override the <code>run()</code>* method and no other <code>Thread</code> 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提供了一种方法,使类在不子类化线程的情况下处于活动状态。* 通过实例化线程实例并将自身作为目标传入,实现Runnable的类可以在不子类化线程的情况下运行。* 在大多数情况下,如果只计划重写run()方法,而不打算重写其他线程方法,* 则应使用Runnable接口。这一点很重要,因为类不应该被子类化,* 除非程序员打算修改或增强类的基本行为。* * ** @author Arthur van Hoff* @see java.lang.Thread* @see java.util.concurrent.Callable* @since 1.0*/
@FunctionalInterface
public interface Runnable {/*** When an object implementing interface <code>Runnable</code> is used* to create a thread, starting the thread causes the object's* <code>run</code> method to be called in that separately executing* thread.* 当使用实现接口Runnable的对象创建线程时,* 启动该线程会导致在该单独执行的线程中调用该对象的run方法。* * * <p>* The general contract of the method <code>run</code> is that it may* take any action whatsoever.* 方法运行的总约定是它可以采取任何行动。** @see java.lang.Thread#run()*/public abstract void run();
}