Java多线程系列(2)--Thread中start()和run()

本文详细解析了Java中线程的start()与run()方法的区别,通过具体代码示例展示了两者的不同之处,并深入分析了源码,帮助读者理解如何正确地使用这两个方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、start()和run()的用处

start():用于启动一个新的线程,新线程会执行相应的run()方法,start()不能被重复使用。
run():和普通的成员方法一样,可以被重复调用。单独调用run()时,会在当前线程中执行run(),而并不会开启新的线程。

二、通过代码实例进行区分

package Test;

/**
 * Created by LKL on 2017/2/20.
 */
public class TestStartAndRun {
    public static void main(String[] args){
        Thread mythread2 = new MyThread2("mythread2");
        System.out.println(Thread.currentThread().getName()+" call mythread2.run()");
        mythread2.run();

        System.out.println(Thread.currentThread().getName()+" call mythread2.start()");
        mythread2.start();

    }
}


package Test;

/**
 * Created by LKL on 2017/2/20.
 */
public class MyThread2 extends Thread {
    public MyThread2(String name){
        super(name);
    }
    public void run(){
        System.out.println(Thread.currentThread().getName()+" is running");
    }
}
  • 1

运行结果如下:

main call mythread2.run()
main is running
main call mythread2.start()
mythread2 is running

  • 1

由上述结果得到:
1>mythread2.run()是在主线程main中调用的,该run()方法直接运行在主线程main上
2>mythread2.start()会启动线程mythread2,线程mythread2启动之后,会调用run()方法,此时的run()方法是运行在线程mythread2上的。

当我将上述main函数中的代码修改成如下时,即运行多个run()方法时:

package Test;

/**
 * Created by LKL on 2017/2/20.
 */
public class TestStartAndRun {
    public static void main(String[] args){
        Thread mythread2 = new MyThread2("mythread2");
        System.out.println(Thread.currentThread().getName()+" call mythread2.run()");
        mythread2.run();

        System.out.println(Thread.currentThread().getName()+" call mythread2.start()");
        mythread2.start();

        System.out.println(Thread.currentThread().getName()+" call mythread2.run()");
        mythread2.run();

        System.out.println(Thread.currentThread().getName()+" call mythread2.run()");
        mythread2.run();

    }
}
  • 1

运行结果如下:

main call mythread2.run()
main is running
main call mythread2.start()
main call mythread2.start()
main is running
main call mythread2.start()
main is running
mythread2 is running
  • 1

此时的结果就有一定的不确定性。此时主线程和mythread2线程都开启了,后续优先级再进行说明。

三、线程中start()和run()方法的源码

Thread中start()源码如下:

public synchronized void start() {
    // 如果线程不是"就绪状态",则抛出异常!
    if (threadStatus != 0)
        throw new IllegalThreadStateException();

    // 将线程添加到ThreadGroup线程组中
    group.add(this);

    boolean started = false;
    try {
        // 通过start0()启动线程
        start0();
        // 设置started标记
        started = true;
    } finally {
        try {
            if (!started) {
                group.threadStartFailed(this);
            }
        } catch (Throwable ignore) {
        }
    }
}
  • 1

由上面的代码可以得到,start()是通过调用本地方法start0()启动线程。此时会新运行一个新的线程,新的线程会调用run()方法。
Thread.java中run()源码如下:

public void run() {
    if (target != null) {
        target.run();
    }
}
  • 1

由上面的代码可以得到,target是一个Runnable对象,run()直接调用线程中的Runnable成员的run()方法,并不会新建一个线程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G11176593

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值