forked from cxinping/PythonFullStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadDemo02.py
More file actions
28 lines (23 loc) · 740 Bytes
/
Copy paththreadDemo02.py
File metadata and controls
28 lines (23 loc) · 740 Bytes
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
# -*- coding: utf-8 -*-
import threading
import time,random,math
class MutliThread(threading.Thread):
def __init__(self, threadName,num):
threading.Thread.__init__(self)
self.name = threadName
self.num = num
def run(self):
for i in range(self.num):
print("{0} i={1}".format(threading.current_thread().getName(), i))
delay = math.ceil(random.random() * 2)
time.sleep(delay)
if __name__ == '__main__':
thr1 = MutliThread("thread1",3)
thr2 = MutliThread("thread2",2)
# 启动线程
thr1.start()
thr2.start()
# 等待至线程中止
thr1.join()
thr2.join()
print("{0} 线程结束".format(threading.current_thread().getName()))