import _thread
from time import sleep, ctime
def thread1():
print('线程1开始:', ctime())
print('线程1挂起4秒')
sleep(4)
print('线程1结束:', ctime())
def thread2():
print('线程2开始:', ctime())
print('线程2挂起2秒')
sleep(2)
print('线程2结束:', ctime())
def thread3():
print('线程3开始:', ctime())
print('线程3挂起1秒')
sleep(1)
print('线程3结束', ctimr())
def main():
print('主线程开始!')
#线程函数为无参数函数,args为空元祖
_thread.start_new_thread(thread1, ())
_thread.start_new_thread(thread2,())
_thread.start_new_thread(thread3,())
sleep(6)
print('全部结束:', ctime())
if __name__ == '__main__':
main()
python#使用thread模块创建线程
最新推荐文章于 2025-04-27 14:13:07 发布
该博客演示了如何使用Python的_thread模块创建并运行三个线程。线程1、线程2和线程3分别有不同的挂起时间,主线程等待所有子线程结束后才打印'全部结束'。此示例展示了并发执行的基本概念。
2175

被折叠的 条评论
为什么被折叠?



