-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_lock.py
More file actions
68 lines (49 loc) · 1.84 KB
/
Copy paththread_lock.py
File metadata and controls
68 lines (49 loc) · 1.84 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
66
#!/usr/bin/python
# coding=UTF-8
#================================================================
# Copyright (C) 2018 HALO Ltd. All rights reserved.
#
# 文件名称:thread_lock.py
# 创 建 者:Zhangdunfeng
# 创建日期:2018-11-23 09:30:34
# 描 述:threading 数据共享问题。一个列表里所有元素都是0,线程"set"从后向前把所有元素改成1,而线程"print"负责从前往后读取列表并打印。 那么,可能线程"set"开始改的时候,线程"print"便来打印列表了,输出就成了一半0一半1,这就是数据的不同步。
#
#================================================================
import threading
class setThread(threading.Thread):
def __init__(self, threadID, name, data):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.data = data
def run(self):
print("开启设置线程:" + self.name)
threadLock.acquire()
for i in reversed(range(len(data))):
self.data[i]=1
threadLock.release()
print("线程结束:" + self.name)
#打印线程
class printThread(threading.Thread):
def __init__(self, threadID, name, data):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.data = data
def run(self):
print("开启设置线程:" + self.name)
#获取锁,用于线程同步
threadLock.acquire()
print(self.data)
#释放锁
threadLock.release()
print("线程结束:" + self.name)
threadLock=threading.Lock()
data=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
sthread=printThread(1,'print', data)
pthread=setThread(2,'set', data)
pthread.start()
sthread.start()
pthread.join()
sthread.join()
print("主线程完毕")