参考链接: 【莫烦Python】Threading 学会多线程 Python
参考链接: 莫烦 多线程
参考链接: threading — 基于线程的并行
参考链接: queue — 一个同步的队列类
参考链接: 12.9 Python的全局锁问题
参考链接: 锁对象
lock在不同线程使用同一共享内存时,能够确保线程之间互不影响,使用lock的方法是, 在每个线程执行运算修改共享内存之前,执行lock.acquire()将共享内存上锁, 确保当前线程执行时,内存不会被其他线程访问,执行运算完毕后,使用lock.release()将锁打开, 保证其他的线程可以使用该共享内存。
testThreading6_1.py
# 教学视频第6集import threadingdef job1():global Afor i in range(10):A += 1print('job1', A)def job2():global Afor i in range(10):A += 100000print('job2', A)if __name__ == '__main__':A = 0t1 = 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-.1.502429796\pythonFiles\lib\python\debugpy\launcher' '60094' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading6_1.py'job1 1job2job1100001100002job2job1 200003200002job1 200004job1job2 200005300005job1job2 300006400006job1job2 400007500007job1job2 500008600008job1job2 600009700009job1job2 700010800010job2 900010job2 1000010END: 1000010(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
testThreading6_2.py
# 教学视频第6集import threadingdef job1():global A, locklock.acquire()for i in range(10):A += 1print('job1', A)lock.release()def job2():global A, locklock.acquire()for i in range(10):A += 100000print('job2', A)lock.release()if __name__ == '__main__':lock = threading.Lock()A = 0t1 = 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-.1.502429796\pythonFiles\lib\python\debugpy\launcher' '60103' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading6_2.py'job1 1job1 2job1 3job1 4job1 5job1 6job1 7job1 8job1 9job1 10job2 100010job2 200010job2 300010job2 400010job2 500010job2 600010job2 700010job2 800010job2 900010job2 1000010END: 1000010(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
testThreading6_3.py
# 教学视频第6集import threadingimport timedef job1():global A, lockfor i in range(10):# time.sleep(0.1)lock.acquire()A += 1print('job1', A)lock.release()def job2():global A, lockfor i in range(10):# time.sleep(0.1)lock.acquire()A += 100000print('job2', A)lock.release()if __name__ == '__main__':lock = threading.Lock()A = 0t1 = 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-.1.502429796\pythonFiles\lib\python\debugpy\launcher' '60112' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading6_3.py'job1 1job1 2job1 3job1 4job1 5job1 6job1 7job1 8job1 9job1 10job2 100010job2 200010job2 300010job2 400010job2 500010job2 600010job2 700010job2 800010job2 900010job2 1000010END: 1000010(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
testThreading6_4.py
# 教学视频第6集import threadingimport timedef job1():global A, lockfor i in range(10):time.sleep(0.1)lock.acquire() # 可以阻塞或非阻塞地获得锁。A += 1print('job1', A)lock.release() # 释放一个锁。这个方法可以在任何线程中调用,不单指获得锁的线程。def job2():global A, lockfor i in range(10):time.sleep(0.1)lock.acquire()A += 100000print('job2', A)lock.release()if __name__ == '__main__':# 实现原始锁对象的类。一旦一个线程获得一个锁,# 会阻塞随后尝试获得锁的线程,直到它被释放;任何线程都可以释放它。lock = threading.Lock() A = 0t1 = 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-.1.502429796\pythonFiles\lib\python\debugpy\launcher' '60135' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading6_4.py'job2 100000job1 100001job2 200001job1 200002job2 300002job1 300003job2 400003job1 400004job2 500004job1 500005job2 600005job1 600006job1 600007job2 700007job1 700008job2 800008job1 800009job2 900009job1 900010job2 1000010END: 1000010(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>