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

二、线程的创建方式

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

相关文章

  • Flow 基本使用

    Flow创建方式 1.1 创建Flow方式1 1.2创建Flow方式2 1.3创建Flow方式3 二、切换线程 注...

  • iOS 线程

    pthread NSThread 第一种创建方式 第二种创建方式 第三种创建线程的方式 NSThread线程的状态...

  • 3.多线程的实现方式

    创建线程的第一种方式 继承Thread类创建线程的第二种方式 实现Runnable接口

  • NSThead

    1、线程的创建: 方式一:NSObject隐含的方法: 方式二:快速创建,无返回值: 方式三:alloc创建: 第...

  • 二、线程的创建方式

    线程的创建方式最准确的描述有两种 方式一:继承Thread并重写run方法 方式二:实现Runnable接口并重写...

  • day24-总结

    python的多线程 子线程的第一种创建方式 子线程的第二张创建方式-对象 多线程的应用-不同的用户端交流 join函数

  • iOS基础知识 (三)

    多线程 多线程创建方式 iOS创建多线程方式主要有NSThread、NSOperation、GCD,这三种方式创建...

  • python中的多线程

    python中的多线程 基本操作 创建线程: 方式一:使用_thread模块直接开启一个线程 方式二:继承一个线程...

  • NSThread

    大神连接 创建使用线程 第一种方式 创建手动启动线程 第二种 创建完成自动执行线程 第三种 相关方法

  • iOS 多线程-NSThread

    1. 创建和启动线程 创建、启动线程 2. 其他创建线程方式 创建线程后自动启动线程[NSThread detac...

网友评论

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

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