Runnable:
@FunctionalInterfacepublic interface Runnable { /** * When an object implementing interfaceRunnable
is used * to create a thread, starting the thread causes the object's *run
method to be called in that separately executing * thread. ** The general contract of the method
run
is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run();}
Runnable是个只有一个方法的接口。
Thread:publicclass Thread implements Runnable { /* What will be run. */ private Runnable target; /** * If this thread was constructed using a separate *Runnable
run object, then that *Runnable
object'srun
method is called; * otherwise, this method does nothing and returns. ** Subclasses of
Thread
should override this method. * * @see #start() * @see #stop() * @see #Thread(ThreadGroup, Runnable, String) */ @Override public void run() { if (target != null) { target.run(); } }}
Thread实现了Runnable接口,而且还组合了一个Runnable,可以看出,实现的方法内部是调用组合类的方法,这其实就是装饰模式。