I'm reading a Blog. But a rather familiar question occurred to me, "What's the difference between run() method and start() method in thread?". Let's test it with the following code:

#FileName: threadingDemo.py
#Python Version: 2.7
#Authour: lxw
#Date: 2015-1-28
#Usage: Demos for threading. import time
import random from threading import Thread class MyThread(Thread):
'''
Definition of my own Thread class.
''' def __init__(self, name):
Thread.__init__(self) # essential
self.name = name def run(self):
amount = random.randint(3, 10)
time.sleep(amount)
outStr = "Thread {0} sleeps {1} seconds. Thread {0} has finished.".format(self.name, amount)
print(outStr) def main():
threads = []
#Create 5 objects of MyThread.
for index in range(5):
th = MyThread(str(index))
threads.append(th) print(threads)
#Run the objects of MyThread.
for index in range(5):
threads[index].run() if __name__ == "__main__":
main()
else:
print("Imported as a module")

      Note the code on Line 38. The output of the program is(Thread 0-Thread 4 never disorder.):

[<MyThread(0, initial)>, <MyThread(1, initial)>, <MyThread(2, initial)>, <MyThread(3, initial)>, <MyThread(4, initial)>]
Thread 0 sleeps 9 seconds. Thread 0 has finished.
Thread 1 sleeps 6 seconds. Thread 1 has finished.
Thread 2 sleeps 8 seconds. Thread 2 has finished.
Thread 3 sleeps 7 seconds. Thread 3 has finished.
Thread 4 sleeps 9 seconds. Thread 4 has finished.

Let's make some modifications around Line 38.

    for index in range(5):
threads[index].start()

      The new output of the program becomes(it differs every time you run the program according to the time each thread sleeped):

[<MyThread(0, initial)>, <MyThread(1, initial)>, <MyThread(2, initial)>, <MyThread(3, initial)>, <MyThread(4, initial)>]
Thread 4 sleeps 4 seconds. Thread 4 has finished.
Thread 0 sleeps 8 seconds. Thread 0 has finished.
Thread 3 sleeps 8 seconds. Thread 3 has finished.
Thread 2 sleeps 9 seconds. Thread 2 has finished.
Thread 1 sleeps 10 seconds. Thread 1 has finished.

      Now, do you understand what's the difference between run() method and start() method in thread?

start():

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

通过调用Thread类的start()来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu

时间片,就开始执行run(),这里run()称为线程体,它包含了要执行的这个线程的内容,run方法运行结束,此

线程随即终止。

run():

      run()只是Thread类的一个普通方法,如果直接调用run(),程序中依然只有主线程这一个线程,其程序执行路径

还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到多线

程的目的

总结:

      调用start()方可启动线程,而run()只是thread的一个普通方法,还是在主线程里执行。把需要并行处理的代码放在

run()中,start()启动线程将自动调用run().

Reference:

python并发:对线程的介绍

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

start() vs. run()的更多相关文章

  1. can't run roscore 并且 sudo 指令返回 unable to resolve host

    I'm using ubuntu14 LTS. Problems: 1. When run roscore, got a mistake and an advice to ping the local ...

  2. DotNet Run 命令介绍

    前言 本篇主要介绍 asp.net core 中,使用 dotnet tools 运行 dotnet run 之后的系统执行过程. 如果你觉得对你有帮助的话,不妨点个[推荐]. 目录 dotnet r ...

  3. 布里斯班Twilight Bay Run半程马拉松

    自从8月3日跑了半马以后,又一鼓作气报了11月份的西昌马拉松.与第一次马拉松的只求完赛目标不同,第二次当然想取得一个更好的成绩.所以8月份练的比较猛,基本上是练2.3天休息一天,周么还要拉个长于21公 ...

  4. SVN:Previous operation has not finished; run 'cleanup' if it was interrupted

    异常处理汇总-开发工具  http://www.cnblogs.com/dunitian/p/4522988.html cleanup failed to process the following ...

  5. linux 环境下运行STS时 出现must be available in order to run STS

    linux 环境下运行ECLIPSE时 出现 “ A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be avai ...

  6. 0040 Java学习笔记-多线程-线程run()方法中的异常

    run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...

  7. jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the install tool.

    jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the ...

  8. .NET跨平台之旅:探秘 dotnet run 如何运行 .NET Core 应用程序

    自从用 dotnet run 成功运行第一个 "Hello world" .NET Core 应用程序后,一直有个好奇心:dotnet run 究竟是如何运行一个 .NET Cor ...

  9. 【svn】在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted

    1.svn在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted2.原因,工作队列被占用,只需 ...

  10. svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupted的解决办法

    今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...

随机推荐

  1. Atitit.java 反编译 工具  attilax 总结

    Atitit.java 反编译 工具  attilax 总结 1. 三大核心核心引擎——1 2. JAD  Jad  attitude推荐这个1 2.1. Jdec.2 2.2. 二. 源码开放的 J ...

  2. Atitit. C#.net clr 2.0  4.0新特性

    Atitit. C#.net clr 2.0  4.0新特性 1. CLR内部结构1 2. CLR 版本发展史3 3. CLR 2.0 3 4. CLR 4 新特性 概览4 4.1.1.  托管与本地 ...

  3. C# Post Json数据到对方url

    1. /// <summary> /// 调用对方Url,Post上传数据 /// </summary> /// <param name="postData&q ...

  4. iOS Socket/Tcp编程 GCDAsyncSocket的实战(带回调)

    很多同学一听到Socket TCP UDP 这几个字眼感觉特别害怕,很怕在工作当中使用,因为他们太底层了.下面我把我在工作中使用Socket类库GCDAsyncSocket进行一次实战 文章中只适用于 ...

  5. scp命令的用法

    用法: scp 命令 scp 能够在 2个 linux 主机间拷贝文件: 命令基本格式: scp [可选參数] file_source file_target ====== 从 本地 拷贝到 远程 拷 ...

  6. SharePoint管理中心配置内容数据库

    SharePoint管理中心配置内容数据库         在SharePoint2010中,内容数据库是组织数据的核心. 是全部站点内容信息,如文档.列表数据和Web部件属性等存储的地方.默认地,内 ...

  7. 中移苏研DCOS实践之路完整篇

    一.实践背景 1.1现网生产系统存在的问题 在中国移动内部各省市公司,由于技术迭代.设备更新的原因,设备繁杂,有x86server .VMware虚拟机.OpenStack虚拟机以及不同厂商的物理机( ...

  8. linux和pycharm下终端彩色打印输出

    实际上linux终端是调用ANSI控制码来实现终端彩色输出.终端的字符颜色由转义序列(Escape Sequence)控制,是文本模式下的系统显示功能,与具体语言无关. 转义序列以控制字符'ESC'开 ...

  9. Distinct powers (Project Euler 29 加强版)

    题目大意: $2<=a,b<=n$ 求 $a^b$能表示多少不同的正整数. 原题中n=100,可以直接暴力求解,常见的两种解法是写高精度或者取对数判断相等. 直觉告诉我应该有更加优秀的解法 ...

  10. 利用微信小程序实现web监控界面

    1.实现思路 利用小程序去调用公司zabbix的接口获取网站监控数据并展示出来. 2.准备阶段 1.小程序公众号 2.企业号 3.zabbix接口 3.实现过程