Java 继承Thread 和 实现Runnable的关联

在java中创建线程的最常用的两种方法

1.继承Thread类
2.继承Runnable接口

但是实现了Runnable接口之后,这个类不能自己启动,需要将其传递给一个Thread实例对象,然后通过Thread对象的start() 方法进行启动,因为只有Thread类中的 native 方法,才能够真正申请系统资源,启动一个单独的线程,启动的结果是这个Thread对象的 run() 方法被调用,虽然我们在java 这一层 无法从代码中找到 Thread 类的 native 方法 start() 中对 run() 的调用。

1.不对Thread的run()方法进行重写

Thread类的run()方法有默认的实现,如果未对其run()方法进行 重写(override),当我们调用start()方法进行启动的时候就会执行默认的实现。

    /**
     * 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();
        }
    }
    @Test
    public void startThread(){
        Thread thread = new Thread();
        thread.start();
    }

确实是执行了Thread类的默认的run()方法,但是由于target==null所以不会执行target.run()
可以看到这里的run()方法实现很简单,直接调用 target 的run() 方法,target是Thread类的一个属性,类型是Runnable,这个字段只能通过Thread的构造方法给其赋值,也就是创建Thread对象的时候

 

    /**
     * Allocates a new {@code Thread} object. This constructor has the same
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
     * {@code (null, null, gname)}, where {@code gname} is a newly generated
     * name. Automatically generated names are of the form
     * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
     */
    public Thread() {
        init(null, null, "Thread-" + nextThreadNum(), 0);
    }

    /**
     * Allocates a new {@code Thread} object. This constructor has the same
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
     * {@code (null, target, gname)}, where {@code gname} is a newly generated
     * name. Automatically generated names are of the form
     * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
     *
     * @param  target
     *         the object whose {@code run} method is invoked when this thread
     *         is started. If {@code null}, this classes {@code run} method does
     *         nothing.
     */
    public Thread(Runnable target) {
        init(null, target, "Thread-" + nextThreadNum(), 0);
    }

可以看到如果使用无参构造,创建Thread对象的时候,调用init()方法 传的 target 参数是 null,创建的Thread对象的target字段就是null。如果创建Thread对象时传递了 target 实例,那么调用init()方法 传的 target 参数是 我们传进去的target对象,创建的Thread对象的target字段就是我们传递的那个target 实例。所以当启动Thread的时候,执行Thread的run()方法,判断target不是null,就会直接调用 target.run() 。这样我们创建Thread对象时传进去的 Runnable 对象的 run() 就被调用了

2.对Thread的run()方法进行重写

    @Test
    public void startThread(){
        Thread thread = new Thread(){
            @Override
            public void run() {
                System.out.println("thread");
            }
        };
        thread.start();
    }

在对Thread的run()方法进行重写的情况下,根据Java面向对象 多态特征 的原则,当调用Thread类的start()方法启动时,会直接调用其子类的  run() 方法的实现逻辑,父类默认的实现就不会被调用了,这就相当于直接调用我们子类中重写的run()方法的代码了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值