import threading
def func(arg):
t = threading.current_thread()#获取当前线程对象
name=t.getName()#获取当前线程名称
print(arg)
t = threading.Thread(target=func,args=(11,))
t.setName(‘xx’)#给线程设置名字
t.setDaemon(True)#主线程不再等待,主线程终止,所有子线程终止
t.join(1)#z主线程等1秒,直到子线程t执行完毕
t.start()
print(123)
import threading
import time
lock = threading.RLock()
n = 10
def task(i):
print(‘这段代码不加锁’,i)
lock.acquire() #加锁
global n
print(‘当前线程’,i,‘读到的n值为:’,n)
n = i
time.sleep(1)
print(‘当前线程’,i,‘修改n值为:’,n)
lock.release() #释放锁
for i in range(10):
t = threading.Thread(target=task,args=(1,))
t.start()