美文网首页并发编程
二、线程的创建方式

二、线程的创建方式

作者: liyc712 | 来源:发表于2020-08-30 17:21 被阅读0次

    线程的创建方式最准确的描述有两种

    方式一:继承Thread并重写run方法

    public class MyThread extends Thread {
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
        }
    
        public static void main(String[] args) {
            MyThread myThread = new MyThread();
            myThread.setName("线程demo");
            myThread.start();
        }
    }
    

    方式二:实现Runnable接口并重写run方法

    public class MyRunable implements Runnable,Serializable {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            Thread thread = new Thread(new MyRunable());
            thread.setName("xdclass");
            thread.start();
        }
    }
    

    注意:只有调用start()方法才会启动线程

    源码分析

    调用Thread.start()方法最终是调用run()方法,run()方法的源码如下:

    
    // 如果target不为空则是执行target的run方法
    /* What will be run. */
    private Runnable target;
    
    public synchronized void start() {
        /**
            * This method is not invoked for the main method thread or "system"
            * group threads created/set up by the VM. Any new functionality added
            * to this method in the future may have to also be added to the VM.
            *
            * A zero status value corresponds to state "NEW".
            */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();
    
        /* Notify the group that this thread is about to be started
            * so that it can be added to the group's list of threads
            * and the group's unstarted count can be decremented. */
        group.add(this);
    
        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                    it will be passed up the call stack */
            }
        }
    }
    
    // 调用底层代码(c),最终调用run方法
    private native void start0();
    
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }
    

    问题:同时继承Thread和实现Runnable接口,会执行哪个run方法

    继承Thread创建线程,是重写Thread的run方法,而实现Runnable接口则是实现Runnable接口的run方法
    run方法一旦被重写,就不会存在经典的三行代码,也就不会执行Runnable接口的run方法了,故此同时继承Thread和实现Runnable接口,只会执行Thread子类重写的run方法

    if (target != null) {
            target.run();
        }
    

    相关文章

      网友评论

        本文标题:二、线程的创建方式

        本文链接:https://www.haomeiwen.com/subject/rfuesktx.html