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
|
#!/usr/bin/python
import threading
import time
exitFlag = 0
class QThread(threading.Thread):#
"""QThread class"""
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.__threadID = threadID
self.__name = name
self.__counter = counter
def run(self):#线程在创建后会直接运行run函数
print "%s start" % (self.__name)
showMessage(self.__name, self.__counter, 6)
print "Exit from" + self.__name
#定义函数
def showMessage(threadName, delayTime, counter):
while counter:
if exitFlag:
thread.exit()
time.sleep(delayTime)
print "%s : %s" % (threadName, time.ctime(time.time()) )
counter -= 1
#创建线程
t1 = QThread(1,"ksnow", 2)
t2 = QThread(2,"kair", 3)
t3 = QThread(3,"baby", 3)
#开启线程
t1.start()
t2.start()
t3.start()
print "exit from Main Thread"
|