-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_2.py
More file actions
68 lines (51 loc) · 1.52 KB
/
Copy paththread_2.py
File metadata and controls
68 lines (51 loc) · 1.52 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
57
58
59
60
61
62
63
64
65
#coding=utf-8
import threading
import time
'''
使用threading模块
将某个类需要多线程执行的,创建时继承threading.Thread
将线程进行同步
threadlock = threading.Lock():线程锁
获取锁:threadlock.acquire()
释放锁:threadlock.realese()
'''
class myThread(threading.Thread):
'''
自定义线程类,继承父类threading.Thread
'''
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID #线程id
self.name = name #线程名称
self.counter = counter #计数
def run(self): #run执行需要执行的代码
print('starting'+self.name)
#获取锁
threadlock.acquire() #可以设置获取锁的超时时间timeout,超时返回false
print_time(self.name, self.counter, 5)
#释放锁
threadlock.release()
print('end'+self.name)
#线程执行的函数
def print_time(threadname, delay, counter):
while counter:
time.sleep(delay)
print('%s, %s' %(threadname, time.ctime(time.time())))#time.ctime(time.time()))将当地时间转换为字符串
counter -= 1
#创建一个锁
threadlock = threading.Lock()
#线程列表
threads = []
#创建线程
thread1 = myThread(1, 'thread_1', 1)
thread2 = myThread(2, 'thread_2', 2)
#开启线程
thread1.start()
thread2.start()
#将线程添加到列表中
threads.append(thread1)
threads.append(thread2)
#遍历,等待线程执行完毕
for t in threads:
t.join()
print("exit mian thread")