-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_py3.py
More file actions
59 lines (41 loc) · 1.36 KB
/
Copy paththread_py3.py
File metadata and controls
59 lines (41 loc) · 1.36 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
56
#!/usr/bin/python3
# coding=UTF-8
#================================================================
# Copyright (C) 2018 HALO Ltd. All rights reserved.
#
# 文件名称:thread_py3.py
# 创 建 者:Zhangdunfeng
# 创建日期:2018-11-23 09:14:48
# 描 述:threading模块 python3专属
#
#================================================================
import threading
import time
exitFlag=0
class myThread(threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print("开始线程:" + self.name)
print_time(self.name, self.counter, 5)
print("退出线程:" + self.name)
def print_time(threadName, delay, counter):
while counter:
if exitFlag:
threadName.exit()#退出线程
time.sleep(delay)
print("%s: %s" % (threadName, time.ctime(time.time())))
counter-=1
#创建线程
thread1=myThread(1,"Thread-1",1)
thread2=myThread(2,"Thread-2",2)
#开启线程
thread1.start()
thread2.start()
#join所完成的工作就是线程同步,即主线程任务结束之后,进入阻塞状态,一直等待其他的子线程执行结束之后,主线程才终止
thread1.join()
thread2.join()
print("退出主线程")