二话不说直接上代码
#代码
import threading
import time
def T1():
print('T1 start')
for i in range(0,10):
time.sleep(1)
print('last ',i,'to finish')
print('finish')
def T2():
print('T2 start')
def main():
thread1 = threading.Thread(target=T1,)
thread2 = threading.Thread(target=T2,)
thread1.start()
thread2.start()
thread1.join()
print('end')
if __name__=='__main__':
main()
首先引用了两个模块threading&time,定义两函数T1和T2,接下来就是主函数。T1主要就是延迟一秒[time.sleep(1)],然后print,这主要是模拟有些线程执行速度比较慢,T2就是模拟执行速度比较快的。开始主函数,定义两个进程,开始thread1和thread2,
网友评论