首页 技术 正文
技术 2022年11月15日
0 收藏 305 点赞 3,538 浏览 2268 个字

      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 randomfrom threading import Threadclass 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()方法的区别

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291