码迷,mamicode.com
首页 > 编程语言 > 详细

多线程-Thread与Runnable源码分析

时间:2017-09-23 15:19:52      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:hat   sse   art   多线程   private   string   abs   ons   gen   

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();
}

Runnable是个只有一个方法的接口。 
Thread:

public
class Thread implements Runnable {
    /* What will be run. */
    private Runnable target;
    /**
     * If this thread was constructed using a separate
     * <code>Runnable</code> run object, then that
     * <code>Runnable</code> object‘s <code>run</code> method is called;
     * otherwise, this method does nothing and returns.
     * <p>
     * Subclasses of <code>Thread</code> 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,可以看出,实现的方法内部是调用组合类的方法,这其实就是装饰模式。

多线程-Thread与Runnable源码分析

标签:hat   sse   art   多线程   private   string   abs   ons   gen   

原文地址:http://www.cnblogs.com/lujiango/p/7581008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!