java中提供两种方式实现线程,继承
java.lang.Thread
类与实现java.lang.Runnable
接口
继承Thread类
public
class Thread implements Runnable {
通过查看源码知道Thread类也是实现了Runnable接口
实现Runnable接口
@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.
* <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();
}
- 只有一个抽象方法run
- @FunctionalInterface:函数式接口
@FunctionalInterface:函数式接口
JAVA8新特性:函数式接口@FunctionalInterface
作用于接口上。表明这个接口是一个特殊的接口即 :函数式接口
一般的接口可以有任意的抽象方法。但是函数式接口却规定了该接口只能有一个抽象的方法。
正是由于这点(只有一个抽象的方法),所以我们可以实现对这个接口的函数式编程