Thread run()和start()方法的区别

本文详细解析Java Thread类中的run()和start()方法的区别,通过查看源码帮助开发者理解为何在多线程场景下,使用start()方法而非run()方法来启动线程更为合适。

最近在使用FindBugs检查代码,下面这段代码:

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        getEntryConfig();
                    }
                }).run();

FindBugs报了Multithreaded correctness级别的错误,具体信息如下:

Priority:Multithreaded correctness
Invokes run on a thread (did you mean to start it instead?)
This method explicitly invokes run() on an object.? In general, classes implement the Runnable interface because they are going to have their run() method invoked in a new thread, in which case Thread.start() is the right method to call.

在网上搜了一下Thread的run和start方法的区别,还是没有听明白。于是直接看代码。我发现,看源码是一个万能的办法,不管是解决问题还是消除疑惑。

我们看一下Thread类中的run方法:

    /**
     * Calls the <code>run()</code> method of the Runnable object the receiver
     * holds. If no Runnable is set, does nothing.
     *
     * @see Thread#start
     */
    public void run() {
        if (target != null) {
            target.run();
        }
    }

这个target对象就是我们在new Thread时传入的Runnable() 对象。所以上面那段代码其实等效如下:
        getEntryConfig();

这肯定不是我们想要的结果。再看start方法的实现:

    /**
     * Starts the new Thread of execution. The <code>run()</code> method of
     * the receiver will be called by the receiver Thread itself (and not the
     * Thread calling <code>start()</code>).
     *
     * @throws IllegalThreadStateException if the Thread has been started before
     *
     * @see Thread#run
     */
    public synchronized void start() {
        if (hasBeenStarted) {
            throw new IllegalThreadStateException("Thread already started."); // TODO Externalize?
        }


        hasBeenStarted = true;


        VMThread.create(this, stackSize);
    }
VMThread是一个封装类,其中的create是一个native方法:

native static void create(Thread t, long stackSize);
这么看来,我们调用start之后就把任务交给系统了,我们接下来该干嘛就干嘛了。接下来的解释可以参看:

Thread的run()与start()的区别

总结一下:

1.Java层想要另起线程完成某任务时,调用Thread的run方法是错误的;使用start方法是正解。


### Python3 中 `threading` 模块 `run()` `start()` 方法区别 #### 基本概念 在 Python 的 `threading` 模块中,`Thread` 类提供了两个重要的方法:`start()` `run()`。它们的功能用途有所不同。 - **`start()` 方法** 调用 `start()` 方法时,Python 解释器会创建一个新的线程并执行该线程的目标函数。具体来说,当调用 `start()` 后,解释器会在新线程中自动调用 `run()` 方法[^1]。因此,开发者通常不需要直接调用 `run()` 方法,而是通过调用 `start()` 来间接触发它。 - **`run()` 方法** 这是线程的核心逻辑所在,默认情况下,`run()` 是由 `start()` 自动调用的。如果自定义了一个继承自 `Thread` 的子类,则可以重写 `run()` 方法来实现特定的任务逻辑[^2]。如果没有显式地提供目标函数(即未设置 `target` 参数),那么线程将默认执行这个 `run()` 方法中的代码。 #### 使用方式对比 以下是两种方法的具体使用场景: 1. **直接调用 `run()`** 如果直接调用了 `run()` 方法而不是通过 `start()`,则不会真正开启新的线程;相反,这相当于在一个单一线程环境中运行了 `run()` 所包含的内容。换句话说,这种方式失去了并发的优势[^3]。 2. **通过 `start()` 开启线程** 当需要真正的多线程支持时,应该始终使用 `start()` 方法。这样不仅可以利用操作系统的调度机制让多个任务同时进行,还可以享受更复杂的线程管理功能,比如同步原语等。 下面是一个简单的例子展示两者的差异: ```python import threading class MyThread(threading.Thread): def run(self): print(f"{self.name} is running") def main(): t = MyThread() # 错误示范 - 不会启动新线程 t.run() # 输出:"MyThread-1 is running" # 正确示范 - 将启动新线程 t.start() # 可能输出:"Thread-1 is running" if __name__ == "__main__": main() ``` 在这个例子中,第一次调用 `t.run()` 并没有实际启动任何新线程,而只是简单地打印了一条消息。第二次调用 `t.start()` 则确实开启了另一个独立的工作单元,并且它的行为取决于操作系统如何安排时间片给各个进程或线程。 #### 总结 为了充分利用 Python 提供的多线程能力,在大多数情况下都应该优先考虑使用 `start()` 方法而非手动调用 `run()` 。只有当你明确知道不希望引入额外开销或者根本无需涉及异步处理的时候才可能例外性地单独调用后者。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值