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

java中thread的start()和run()的区别:

1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码:

通过调用Thread类的start()方法来启动一个线程,
这时此线程是处于就绪状态,
并没有运行。
然后通过此Thread类调用方法run()来完成其运行操作的,
这里方法run()称为线程体,
它包含了要执行的这个线程的内容,
Run方法运行结束,
此线程终止,
而CPU再运行其它线程,

 

2.run()方法当作普通方法的方式调用,程序还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码:

而如果直接用Run方法,
这只是调用一个方法而已,
程序中依然只有主线程--这一个线程,
其程序执行路径还是只有一条,
这样就没有达到写线程的目的。

 

举例说明一下:

记住:线程就是为了更好地利用CPU,
提高程序运行速率的!

public class TestThread1{
public static void main(String[] args){
Runner1 r=new Runner1();
//r.run();//这是方法调用,而不是开启一个线程
Thread t=new Thread(r);//调用了Thread(Runnable target)方法。且父类对象变量指向子类对象。
t.start();

for(int i=0;i<100;i++){
System.out.println("进入Main Thread运行状态");
System.out.println(i);
}
}
}
class Runner1 implements Runnable{ //实现了这个接口,jdk就知道这个类是一个线程
public void run(){

for(int i=0;i<100;i++){
System.out.println("进入Runner1运行状态");
System.out.println(i);
}
}
}

同时摘取一段外文网站论坛上的解释:
Why do we need start() method in Thread class? In Java API description for Thread class is written : "Java Virtual Machine calls the run method of this thread..".

Couldn't we call method run() ourselves, without doing double call: first we call start() method which calls run() method? What is a meaning to do things such complicate?



 

There is some very small but important difference between using start() and run() methods. Look at two examples below:

Example one:

Code:

Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();

Example two:

Code:

Thread one = new Thread();
Thread two = new Thread();
one.start();
two.start();

The result of running examples will be different.

In Example one the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.

In Example two both threads start and run simultaneously.

Conclusion: the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - we wait when it quits and only then we can run the next line of our code.
### 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、付费专栏及课程。

余额充值