Python3 threading.Thread 的jion()用法实例

Python
# -*- coding: utf-8 -*- import time __author__ = 'songhao' import threading def work(): # time.sleep(tt) print("work 开始") for x in range(10): time.sleep(0.1) # print(threading.current_thread()) print("work 结束") def man(): add_work = <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/threading-thread" title="View all posts in threading.Thread" target="_blank">threading.Thread</a></span>(target=work) add_work.start() print("all done") if __name__ == '__main__': man()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*- coding: utf-8 -*-
import time
 
__author__ = 'songhao'
import threading
 
 
def work ( ) :
     # time.sleep(tt)
     print ( "work 开始" )
     for x in range ( 10 ) :
         time . sleep ( 0.1 )
     # print(threading.current_thread())
     print ( "work 结束" )
 
 
def man ( ) :
     add_work = threading . Thread ( target = work )
     add_work . start ( )
 
     print ( "all done" )
 
 
if __name__ == '__main__' :
     man ( )

如果不加 jion 输出的结果是什么呢?

 

/usr/local/bin/python3 "/Users/songhao/Desktop/Python3 入门和进阶/Python file/d3/c8.py"
work 开始
all done
work 结束

加上 jion后呢?

Python
# -*- coding: utf-8 -*- import time __author__ = 'songhao' import threading def work(): # time.sleep(tt) print("work 开始") for x in range(10): time.sleep(0.1) # print(threading.current_thread()) print("work 结束") def man(): add_work = <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/threading-thread" title="View all posts in threading.Thread" target="_blank">threading.Thread</a></span>(target=work) add_work.start() add_work.join() print("all done") if __name__ == '__main__': man()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -*- coding: utf-8 -*-
import time
 
__author__ = 'songhao'
import threading
 
 
def work ( ) :
     # time.sleep(tt)
     print ( "work 开始" )
     for x in range ( 10 ) :
         time . sleep ( 0.1 )
     # print(threading.current_thread())
     print ( "work 结束" )
 
 
def man ( ) :
     add_work = threading . Thread ( target = work )
     add_work . start ( )
     add_work . join ( )
 
     print ( "all done" )
 
 
if __name__ == '__main__' :
     man ( )

结果是:

join(timeout) 在join()位置等待另一线程结束后再继续运行join()后的操作,timeout是可选项,表示最大等待时间




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值