Python多線程同步及優先級隊列


import threading
import time


class Mythread(threading.Thread):
def __init__(self,threadID,name,counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print("開啟線程:"+self.name)
#獲取鎖用於線程同步
threadLock.acquire()
print(self.name + "加鎖")
print_time(self.name,self.counter,3)
#釋放鎖,開啟下一個線程
threadLock.release()
print(self.name + "解鎖")



def print_time(threadName,delay,counter):
while counter :
time.sleep(delay)
print("%s -- %s -- %s" %(threadName,delay,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("退出主線程")

#-------------------運行結果-----------------------#
C:\python3.7\python.exe D:/Python-Test/多線程/線程同步.py
開啟線程:Thread-1
Thread-1加鎖
開啟線程:Thread-2
Thread-1 -- 1 -- Tue Nov 28 13:32:12 2017
Thread-1 -- 1 -- Tue Nov 28 13:32:13 2017
Thread-1 -- 1 -- Tue Nov 28 13:32:14 2017
Thread-1解鎖
Thread-2加鎖
Thread-2 -- 2 -- Tue Nov 28 13:32:16 2017
Thread-2 -- 2 -- Tue Nov 28 13:32:18 2017
Thread-2 -- 2 -- Tue Nov 28 13:32:20 2017
Thread-2解鎖
退出主線程
#-------------------運行結果-----------------------#


#2 線程優先級隊列
import queue

exitflag = 0

class MyThread(threading.Thread):
def __init__(self,threadID,name,q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print("開啟線程:" + self.name)
process_data(self.name,self.q)
print("退出線程:" + self.name)

def process_data(threadName,q):
while not exitflag:
queueLock.acquire()
print(threadName + "獲取鎖")
if not workQueue.empty():
data = q.get()#獲取隊列
print("獲取隊列"+data)
queueLock.release()
print(threadName + "釋放鎖")
print("%s processing %s " % (threadName,data))
else:
queueLock.release()
time.sleep(1)

threadList = ["Thread-1","Thread-2","Thread-3"]
nameList = ["One","Two","Three","Four","Five"]
queueLock = threading.Lock()
workQueue = queue.Queue(10)
threads = []
threadID = 1

#創建新線程
for tName in threadList:
thread = MyThread(threadID,tName,workQueue)
thread.start()
threads.append(thread)
threadID += 1
#填充隊列
queueLock.acquire()
for word in nameList:
workQueue.put(word)
print("寫入隊列" + word)
queueLock.release()

#等待隊列清空
while not workQueue.empty():
pass

#通知線程是時候退出
exitflag = 1

#等待所有線程完成
for t in threads:
t.join()
print("退出主線程")

#-------------------運行結果-----------------------#
C:\python3.7\python.exe D:/Python-Test/多線程/線程同步.py
開啟線程:Thread-1
Thread-1獲取鎖
開啟線程:Thread-2
Thread-2獲取鎖
開啟線程:Thread-3
Thread-3獲取鎖
寫入隊列One
寫入隊列Two
寫入隊列Three
寫入隊列Four
寫入隊列Five
Thread-3獲取鎖
獲取隊列One
Thread-3釋放鎖
Thread-3 processing One
Thread-2獲取鎖
獲取隊列Two
Thread-2釋放鎖
Thread-1獲取鎖
Thread-2 processing Two
獲取隊列Three
Thread-1釋放鎖
Thread-1 processing Three
Thread-3獲取鎖
獲取隊列Four
Thread-3釋放鎖
Thread-3 processing Four
Thread-2獲取鎖
獲取隊列Five
Thread-2釋放鎖
Thread-2 processing Five
退出線程:Thread-1
退出線程:Thread-3
退出線程:Thread-2
退出主線程
#-------------------運行結果-----------------------#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM