python之Thread对象

Thread对象的属性

import threading
from time import sleep,ctime
def pri(py,n):
    name=p1.name                      # 线程名
    id=p1.ident                       # 线程标识符
    for i in range(n):
        print('name=%s now=%s id=%s'%(name,ctime(),id))
        sleep(1)
p1=threading.Thread(target=pri,args=('python',3),name='chian')
#p1.setName('chinese')                # 设置线程名
p1.start()                            # 启动线程

args参数类型为元组,所以只有一个参数时为(‘python’,)
python之Thread对象_第1张图片
join()的使用比较

import threading
from time import sleep,ctime
def pri():
    for i in range(3):
        print('pri1=%s'%ctime())
        sleep(1)
p1=threading.Thread(target=pri,daemon=True)
p1.start() 
p1.join()          # 使主线程挂起
print("___end")
print(threading.activeCount())

python之Thread对象_第2张图片python之Thread对象_第3张图片

你可能感兴趣的:(Python高级)