参考链接: 【莫烦Python】Threading 学会多线程 Python
参考链接: 莫烦 多线程
参考链接: threading — 基于线程的并行
参考链接: queue — 一个同步的队列类
参考链接: 12.9 Python的全局锁问题
参考链接: 锁对象
lock在不同线程使用同一共享内存时,能够确保线程之间互不影响,
使用lock的方法是, 在每个线程执行运算修改共享内存之前,
执行lock.acquire()将共享内存上锁, 确保当前线程执行时,
内存不会被其他线程访问,执行运算完毕后,
使用lock.release()将锁打开,
保证其他的线程可以使用该共享内存。
testThreading6_1.py
# 教学视频第6集
import threading
def job1():
global A
for i in range(10):
A += 1
print('job1', A)
def job2():
global A
for i in range(10):
A += 100000
print('job2', A)
if __name__ == '__main__':
A = 0
t1 = threading.Thread(target=job1)
t2 = threading.Thread(target=job2)
t1.start()
t2.start()
t1.join()
t2.join()
print('END:',A)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 863 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '60094' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading6_1.py'
job1 1job2
job1100001
100002
job2job1 200003
200002job1 200004
job1job2 200005300005
job1job2 300006400006
job1job2 400007500007
job1job2 500008600008
job1job2 600009700009
job1job2 700010800010
job2 900010
job2 1000010
END: 1000010
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
testThreading6_2.py
# 教学视频第6集
import threading
def job1():
global A, lock
lock.acquire()
for i in range(10):
A += 1
print('job1', A)
lock.release()
def job2():
global A, lock
lock.acquire()
for i in range(10):
A += 100000
print('job2', A)
lock.release()
if __name__ == '__main__':
lock = threading.Lock()
A = 0
t1 = threading.Thread(target=job1)
t2 = threading.Thread(target=job2)
t1.start()
t2.start()
t1.join()
t2.join()
print('END:',A)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 836 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '60103' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading6_2.py'
job1 1
job1 2
job1 3
job1 4
job1 5
job1 6
job1 7
job1 8
job1 9
job1 10
job2 100010
job2 200010
job2 300010
job2 400010
job2 500010
job2 600010
job2 700010
job2 800010
job2 900010
job2 1000010
END: 1000010
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
testThreading6_3.py
# 教学视频第6集
import threading
import time
def job1():
global A, lock
for i in range(10):
# time.sleep(0.1)
lock.acquire()
A += 1
print('job1', A)
lock.release()
def job2():
global A, lock
for i in range(10):
# time.sleep(0.1)
lock.acquire()
A += 100000
print('job2', A)
lock.release()
if __name__ == '__main__':
lock = threading.Lock()
A = 0
t1 = threading.Thread(target=job1)
t2 = threading.Thread(target=job2)
t1.start()
t2.start()
t1.join()
t2.join()
print('END:',A)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 874 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '60112' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading6_3.py'
job1 1
job1 2
job1 3
job1 4
job1 5
job1 6
job1 7
job1 8
job1 9
job1 10
job2 100010
job2 200010
job2 300010
job2 400010
job2 500010
job2 600010
job2 700010
job2 800010
job2 900010
job2 1000010
END: 1000010
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
testThreading6_4.py
# 教学视频第6集
import threading
import time
def job1():
global A, lock
for i in range(10):
time.sleep(0.1)
lock.acquire() # 可以阻塞或非阻塞地获得锁。
A += 1
print('job1', A)
lock.release() # 释放一个锁。这个方法可以在任何线程中调用,不单指获得锁的线程。
def job2():
global A, lock
for i in range(10):
time.sleep(0.1)
lock.acquire()
A += 100000
print('job2', A)
lock.release()
if __name__ == '__main__':
# 实现原始锁对象的类。一旦一个线程获得一个锁,
# 会阻塞随后尝试获得锁的线程,直到它被释放;任何线程都可以释放它。
lock = threading.Lock()
A = 0
t1 = threading.Thread(target=job1)
t2 = threading.Thread(target=job2)
t1.start()
t2.start()
t1.join()
t2.join()
print('END:',A)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 924 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '60135' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading6_4.py'
job2 100000
job1 100001
job2 200001
job1 200002
job2 300002
job1 300003
job2 400003
job1 400004
job2 500004
job1 500005
job2 600005
job1 600006
job1 600007
job2 700007
job1 700008
job2 800008
job1 800009
job2 900009
job1 900010
job2 1000010
END: 1000010
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>