博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程
阅读量:4322 次
发布时间:2019-06-06

本文共 3487 字,大约阅读时间需要 11 分钟。

1 import _thread 2 import time 3  4 def print_time(threadName,delay): 5     count = 0 6     while count < 5: 7         time.sleep(delay) 8         count += 1 9         print('{0}: {1}'.format(threadName,time.ctime(time.time())))10 11 try:12     _thread.start_new_thread(print_time,('Thread-1',2,))13     _thread.start_new_thread(print_time,('Thread-2',4,))14 except:15     print('Error')16 while 1:17     pass

 

 

1 # 3-线程同步 2 import threading 3 import time 4 class myThread(threading.Thread): 5     def __init__(self,threadID,name,counter): 6         threading.Thread.__init__(self) 7         self.threadID = threadID 8         self.name = name 9         self.counter = counter10     def run(self):11         print('开启线程: ' + self.name)12         threadLock.acquire()13         print_time(self.name,self.counter,3)14         threadLock.release()15 16 def print_time(threadName,delay,counter):17     while counter:18         time.sleep(delay)19         print('{} :{}'.format(threadName,time.ctime(time.time())))20         counter -=121 22 threadLock = threading.Lock()23 threads  = []24 25 thread1 = myThread(1,'Thread-1',1)26 thread2 = myThread(2,'Thread-2',2)27 28 thread1.start()29 thread2.start()30 31 threads.append(thread1)32 threads.append(thread2)33 34 for t in threads:35     t.join()36 print('退出主线程')

 

1 # 线程优先级队列 2 import queue 3 import threading 4 import time 5  6 exitFlag = 0 7  8 class myThread (threading.Thread): 9     def __init__(self,threadID,name,q):10         threading.Thread.__init__(self)11         self.thredID = threadID12         self.name = name13         self.q = q14     def run(self):15         print('开启线程:' + self.name)16         process_data(self.name,self.q)17         print('退出线程: ' + self.name)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('{}: {}'.format(threadName,data))25         else:26             queueLock.release()27         time.sleep(1)28 29 threadList = ['Thread-1','Thread-2','Thread-3']30 nameList = ['One','Two','Three','Four','Five']31 queueLock = threading.Lock()32 workQueue = queue.Queue(10)33 threads = []34 threadID = 135 36 for tName in threadList:37     thread = myThread(threadID,tName,workQueue)38     thread.start()39     threads.append(thread)40     threadID += 141 42 queueLock.acquire()43 for word in nameList:44     workQueue.put(word)45 queueLock.release()46 47 while not workQueue.empty():48     pass49 50 exitFlag = 151 52 for t in threads:53     t.join()54 print('退出主鼎城')

 

 

1 # 使用threading模块创建线程 2 import threading 3 import time 4  5 exitFlag = 0 6 class myThread(threading.Thread): 7     def __init__(self,threadID,name,counter): 8         threading.Thread.__init__(self) 9         self.threadID = threadID10         self.name = name11         self.counter = counter12     def run(self):13         print('开始线程: '+ self.name)14         print_time(self.name,self.counter,5)15         print('tichuxiancheng') + self.name16 17 def print_time(threadName,delay,counter):18     while counter:19         if exitFlag:20              threadName.exit()21         time.sleep(delay)22         print('{}:{}'.format(threadName,time.ctime(time.time())))23 24 25 thread1 = myThread(1,'Thread-1',1)26 thread2 = myThread(2,'Thread-2',2)27 28 thread1.start()29 thread2.start()30 thread1.join()31 thread2.join()32 print('退出主线程')

 

转载于:https://www.cnblogs.com/jpr-ok/p/9269781.html

你可能感兴趣的文章
Linux IPC实践(3) --具名FIFO
查看>>
Qt之模拟时钟
查看>>
第一次接触安卓--记于2015.8.21
查看>>
(转)在分层架构下寻找java web漏洞
查看>>
mac下多线程实现处理
查看>>
C++ ifstream ofstream
查看>>
跟初学者学习IbatisNet第四篇
查看>>
seL4环境配置
查看>>
Git报错:insufficient permission for adding an object to repository database .git/objects
查看>>
ajax跨域,携带cookie
查看>>
BZOJ 1600: [Usaco2008 Oct]建造栅栏( dp )
查看>>
洛谷 CF937A Olympiad
查看>>
Codeforces Round #445 C. Petya and Catacombs【思维/题意】
查看>>
用MATLAB同时作多幅图
查看>>
python中map的排序以及取出map中取最大最小值
查看>>
ROR 第一章 从零到部署--第一个程序
查看>>
<form>标签
查看>>
vue去掉地址栏# 方法
查看>>
Lambda03 方法引用、类型判断、变量引用
查看>>
was集群下基于接口分布式架构和开发经验谈
查看>>