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.7python.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.7python.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
退出主线程
#-------------------运行结果-----------------------#
原文地址:https://www.cnblogs.com/acer-haitao/p/7909994.html