使用Thread类创建多线程
一、代码示例二、两者比较
一、代码示例
import threading
import time
class Coding(threading
.Thread
):
def run(self
):
for i
in range(3):
print("正在写代码%s" % threading
.current_thread
())
time
.sleep
(1)
class Drawing(threading
.Thread
):
def run(self
):
for i
in range(3):
print("正在写画图%s" % threading
.current_thread
())
time
.sleep
(1)
def main():
t1
= Coding
()
t2
= Drawing
()
t1
.start
()
t2
.start
()
if __name__
== '__main__':
main
()
二、两者比较
使用类的方法来创建多线程会使代码的封装性更好。
转载请注明原文地址:https://tech.qufami.com/read-20129.html