-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch20_4.py
More file actions
55 lines (38 loc) · 1.83 KB
/
ch20_4.py
File metadata and controls
55 lines (38 loc) · 1.83 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 用自定义的子类MyThread继承threading.Thread类,并把线程放进子类自动运行的的run方法里;
# 实例化线程时就不用再传是哪个线程了,只传线程需要的参数即可,适合线程相同的情况;
# 之后同样是用.start()方法启动,用.join()方法在主程序中等待所有线程结束。
# 在构造函数中继承父类构造函数:super(MyThread, self).__init__()
import time
import datetime
import threading
date_time_format = '%H:%M:%S'
def get_time_str():
now = datetime.datetime.now()
return datetime.datetime.strftime(now, date_time_format)
class MyThread(threading.Thread):
def __init__(self, thread_id):
super(MyThread, self).__init__()
self.thread_id = thread_id
def run(self):
print('Thread %d\t start at %s' % (self.thread_id, get_time_str()))
print('Thread %d\t sleeping' % self.thread_id)
time.sleep(4)
print('Thread %d\t finish at %s' % (self.thread_id, get_time_str()))
def main():
print('Main thread start at %s' % get_time_str())
threads = []
for item in range(5): # 实例化了5个线程放在列表里(创建线程)
# thread = threading.Thread(target=thread_function, args=(item, ))
thread = MyThread(item)
threads.append(thread) # 类threading.Thread的对象自动管理线程锁
print(threads)
for item in range(5): # 5次触发,每隔一秒触发一个线程(启动线程)
threads[item].start() # 每个实例的start()方法
time.sleep(1)
print(threads)
for item in range(5): # 等待所有线程执行结束
threads[item].join()
print(threads)
print('Main thread finish at %s' % get_time_str())
if __name__ == '__main__':
main()