'''
中国
'''
from threading import Thread
import time
class P(Thread):
def __init__(self, threadID, name, counter):
# 新式用法
super().__init__()
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print('开始线程:' + self.name)
# 成员函数
self.print_time()
print('结束线程:' + self.name)
def print_time(self):
while self.counter:
time.sleep(1)
print("{name}:{time}".format(name=self.name, time=time.ctime()))
self.counter -= 1
if __name__ == '__main__':
p1 = P(1, '线程-1', 2)
p2 = P(1, '线程-2', 2)
p1.start()
p2.start()
p1.join()
p2.join()
print('主线程退出')
运行结果: