import threading
import time
# 打印函数a
def printa(a):
count = 0
while count < 5:
time.sleep(2)
print("线程:%s。
打印:%s。
时间:%s。
" % (threading.current_thread().name, a, time.ctime()))
count += 1
# 打印函数b
def printb(b):
count = 0
while count < 5:
time.sleep(4)
print("线程:%s。
打印:%s。
时间:%s。
" % (threading.current_thread().name, b, time.ctime()))
count += 1
# threading.Thread(target=,args=(),name='')
t1 = threading.Thread(target=printa, args=(10,), name='线程1')
t2 = threading.Thread(target=printb, args=(20,), name='线程2')
t1.start()
t2.start()
t1.join()
t2.join()
print("退出主线程")
import threading
import time
# 打印函数a
def printa(a):
count = 0
while count < 5:
time.sleep(2)
print("线程:%s。
打印:%s。
时间:%s。
" % (threading.current_thread().name, a, time.ctime()))
count += 1
# threading.Thread(target=,args=(),name='')
threadList = []
for i in range(3):
t = threading.Thread(target=printa, args=(i,))
threadList.append(t)
for t in threadList:
t.start()
for t in threadList:
t.join()
print("退出主线程")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)