在多執行緒程式設計中常用到的乙個概念就是鎖,它用於將執行緒需要獨佔的資源進行加鎖,使用後再進行釋放,防止死鎖發生。
此處給出乙個不加鎖的多執行緒例子(實現整數n在每個執行緒內加1並列印):
#!/usr/bin/python
# -*- coding: utf-8 -*-
import threading
import time
class mythread(threading.thread):
def __init__(self, tid, times):
super(mythread, self).__init__()
self.tid = tid
self.times = times
def run(self):
fun_threading(self.tid, self.times)
def fun_threading(tid, times):
global n
time.sleep(1)
n = n + 1
print('thread-:'.format(tid = tid), n)
n = 0
def main():
threads =
num_threads = 6 # 執行緒數
id = 1
for i in range(1, num_threads + 1):
new_thread = mythread(id, 4) # 建立執行緒並加入執行緒佇列
id += 1
for i in range(1, num_threads + 1):
threads[i - 1].start() # 啟動執行緒活動
for i in range(1, num_threads + 1):
threads[i - 1].join()
print("exiting main thread")
if __name__ == "__main__":
main()
執行結果:
thread-2: 1
thread-6: 4
thread-1: 2
thread-5: 3
thread-4: 5
thread-3: 6
exiting main thread
會發現執行緒順序是錯亂的,且n的累加順序也有問題,在某些執行緒內n加完1後還沒來得及列印,該執行緒就被「搶占」了。我們加上鎖之後看一下效果:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import threading
import time
class mythread(threading.thread):
def __init__(self, tid, times):
super(mythread, self).__init__()
self.tid = tid
self.times = times
def run(self):
if lock.acquire():
fun_threading(self.tid, self.times)
lock.release()
def fun_threading(tid, times):
global n
time.sleep(1)
n = n + 1
print('thread-:'.format(tid = tid), n)
lock = threading.lock()
n = 0
def main():
threads =
num_threads = 6 # 執行緒數
id = 1
for i in range(1, num_threads + 1):
new_thread = mythread(id, 4) # 建立執行緒並加入執行緒佇列
id += 1
for i in range(1, num_threads + 1):
threads[i - 1].start() # 啟動執行緒活動
for i in range(1, num_threads + 1):
threads[i - 1].join()
print("exiting main thread")
if __name__ == "__main__":
main()
#!/usr/bin/python
# -*- coding: utf-8 -*-
import threading
import time
class mythread(threading.thread):
def __init__(self, tid, times):
super(mythread, self).__init__()
self.tid = tid
self.times = times
def run(self):
if lock.acquire():
fun_threading(self.tid, self.times)
lock.release()
def fun_threading(tid, times):
for i in range(1, times + 1):
print('thread-: '.format(tid = tid, time = time.ctime(time.time())))
time.sleep(1)
threads =
num_threads = 3 # 執行緒數
id = 1
lock = threading.lock() # 建立鎖
for i in range(1, num_threads + 1):
new_thread = mythread(id, 4) # 建立執行緒並加入執行緒佇列
id += 1
for i in range(1, num_threads + 1):
threads[i-1].start() # 啟動執行緒活動
for i in range(1, num_threads + 1):
threads[i-1].join()
print("exiting main thread")
執行結果:
thread-1: 1
thread-2: 2
thread-3: 3
thread-4: 4
thread-5: 5
thread-6: 6
exiting main thread
發現6個執行緒依次列印完後將i/o交給下乙個執行緒列印,搞定~ 多執行緒之鎖機制
多執行緒實現方式的其中之一是實現runnable方式,並且重寫run方法 package thrad author 子曰無衣 public class mythread implements runnable public mythread string name override public s...
python 多執行緒 鎖
參考 python cookbook 12章 啟動和停止執行緒 start 啟動執行緒 is alive 判斷是否已經結束 join 請求連線某個執行緒,等待該執行緒結束,才退出join函式 daemon引數,守護執行緒,該執行緒無法被連線,主線程結束後自動銷毀。2.7不適用 終止執行緒 需要自己構...
python多執行緒鎖 python的多執行緒程式設計之鎖
1 背景概述 在python中,要保證資料的正確性,並且自己對資料進行控制,對資料進行加鎖並且自己釋放鎖。多執行緒的主要目的為了提高效能與速度,用在無關的方向是最好的,例如在使用爬蟲的時候,可以使用多執行緒來進行爬取資料,因為在這些執行緒之間沒有需要共同操作的資料,從而在這個時候利用是最好的。如果需...