GeezerVison is showing github.com — Add files via upload · c-dafan/notes@9d236cf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import threading | | 2 | +import time | | 3 | + | | 4 | +class myThread(threading.Thread): | | 5 | + def __init__(self,id,name,q): | | 6 | + threading.Thread.__init__(self) | | 7 | + self.id=id | | 8 | + self.q=q | | 9 | + self.name=name | | 10 | + def run(self): | | 11 | + print ("开启线程:" + self.name) | | 12 | + print_thirty(self.name, self.q) | | 13 | + print ("退出线程:" + self.name) | | 14 | + | | 15 | +def print_thirty(name,q): | | 16 | + while stopflag: | | 17 | + queueLock.acquire() | | 18 | + if not len(q)==0: | | 19 | + print(name+":"+str(q.pop())) | | 20 | + queueLock.release() | | 21 | + else: | | 22 | + queueLock.release() | | 23 | + # time.sleep(0.5) | | 24 | + | | 25 | +threadList = ["Thread-1", "Thread-2", "Thread-3"] | | 26 | +workQueue=list(range(30)) | | 27 | +threads = [] | | 28 | +threadID = 1 | | 29 | +stopflag=True | | 30 | +queueLock = threading.Lock() | | 31 | +for tName in threadList: | | 32 | + thread = myThread(threadID, tName, workQueue) | | 33 | + thread.start() | | 34 | + threads.append(thread) | | 35 | + threadID += 1 | | 36 | +while not len(workQueue)==0: | | 37 | + pass | | 38 | + | | 39 | +stopflag=False | | 40 | + | | 41 | +for t in threads: | | 42 | + t.join() | | 43 | + | | 44 | +print ("退出主线程") |
Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import threading | | 2 | +import os | | 3 | +import queue | | 4 | + | | 5 | +path="D:/Users/a/Anaconda3/Lib" | | 6 | +class myThread(threading.Thread): | | 7 | + def __init__(self,id,name,q): | | 8 | + threading.Thread.__init__(self) | | 9 | + self.q=q | | 10 | + self.name=name | | 11 | + self.id=id | | 12 | + def run(self): | | 13 | + print('线程开启') | | 14 | + process_data(self.name,self.q) | | 15 | + print('线程关闭') | | 16 | + | | 17 | +def process_data(name,q): | | 18 | + while stopflag: | | 19 | + threadLock.acquire() | | 20 | + if not q.empty(): | | 21 | + data=q.get() | | 22 | + threadLock.release() | | 23 | + print(name+":"+data) | | 24 | + else: | | 25 | + threadLock.release() | | 26 | +threadLock=threading.Lock() | | 27 | +filesname=os.listdir(path) | | 28 | +workqueue=queue.Queue(3000) | | 29 | +stopflag=True | | 30 | +threadnames=['Thread1','Thread2','Thread3'] | | 31 | +threadid=1 | | 32 | +threads=[] | | 33 | +for i in threadnames: | | 34 | + # myThread(threadid,i,workqueue) | | 35 | + threads.append(myThread(threadid,i,workqueue)) | | 36 | + threads[-1].start() | | 37 | + threadid += 1 | | 38 | + | | 39 | +threadLock.acquire() | | 40 | +for file in filesname: | | 41 | + workqueue.put(file) | | 42 | +threadLock.release() | | 43 | + | | 44 | +while not workqueue.empty(): | | 45 | + pass | | 46 | +stopflag=False | | 47 | + | | 48 | +for t in threads: | | 49 | + t.join() | | 50 | +print ("退出主线程") |
Collapse file Copy file name to clipboard +59Lines changed: 59 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import queue | | 2 | +import threading | | 3 | +import time | | 4 | + | | 5 | +exitFlag = 0 | | 6 | + | | 7 | +class myThread (threading.Thread): | | 8 | + def __init__(self, threadID, name, q): | | 9 | + threading.Thread.__init__(self) | | 10 | + self.threadID = threadID | | 11 | + self.name = name | | 12 | + self.q = q | | 13 | + def run(self): | | 14 | + print ("开启线程:" + self.name) | | 15 | + process_data(self.name, self.q) | | 16 | + print ("退出线程:" + self.name) | | 17 | + | | 18 | +def process_data(threadName, q): | | 19 | + while not exitFlag: | | 20 | + queueLock.acquire() | | 21 | + if not workQueue.empty(): | | 22 | + data = q.get() | | 23 | + queueLock.release() | | 24 | + print ("%s processing %s" % (threadName, data)) | | 25 | + else: | | 26 | + queueLock.release() | | 27 | + time.sleep(0.2) | | 28 | + | | 29 | +threadList = ["Thread-1", "Thread-2", "Thread-3","Thread-4"] | | 30 | +nameList = ["One", "Two", "Three", "Four", "Five",'six','senve','eight','nine','ten'] | | 31 | +queueLock = threading.Lock() | | 32 | +workQueue = queue.Queue(10) | | 33 | +threads = [] | | 34 | +threadID = 1 | | 35 | + | | 36 | +# 创建新线程 | | 37 | +for tName in threadList: | | 38 | + thread = myThread(threadID, tName, workQueue) | | 39 | + thread.start() | | 40 | + threads.append(thread) | | 41 | + threadID += 1 | | 42 | + | | 43 | +# 填充队列 | | 44 | +queueLock.acquire() | | 45 | +for word in nameList: | | 46 | + workQueue.put(word) | | 47 | +queueLock.release() | | 48 | + | | 49 | +# 等待队列清空 | | 50 | +while not workQueue.empty(): | | 51 | + pass | | 52 | + | | 53 | +# 通知线程是时候退出 | | 54 | +exitFlag = 1 | | 55 | + | | 56 | +# 等待所有线程完成 | | 57 | +for t in threads: | | 58 | + t.join() | | 59 | +print ("退出主线程") |
|