import _thread
import time
defgo():for i inrange(5):print(i,"-------")
time.sleep(1)for i inrange(5):#同时执行5次
_thread.start_new_thread(go,())for j inrange(6):#让主线程卡顿6秒
time.sleep(1)print("over")
#线程冲突import _thread
num =0defadd():for _ inrange(1000000):global num
num +=1print(num)for j inrange(5):
_thread.start_new_thread(add,())whileTrue:pass#线程冲突,5个线程同时抢夺num资源,导致最后结果错误
import threading
import win32api
classMythread(threading.Thread):# 继承threading.Thread类def__init__(self, num):
threading.Thread.__init__(self)# 父类初始化
self.num = num
defrun(self):# 定义函数
win32api.MessageBox(0,"hello"+str(self.num),'joker',0)print(self.getName())# 获取线程名
Mythd =[]for i inrange(5):
t = Mythread(i)# 初始化print(i)
t.start()# 开启
Mythd.append(t)# 将乱序线程(同时抢夺run这个函数)加入列表for j in Mythd:# 这里与顺序不同,上面显示所有的线程都加入Mthd列表(所以一次性跳出5个窗口,但是主线程还没死,因为有join卡住)。# j是线程
j.join()# 这里主线程同时等待所有线程都执行完毕,才执行“game over”print("game over")
import threading
num =0
mutex = threading.Lock()# 创建一个锁,threading.Lock()是一个类classMyhtread(threading.Thread):defrun(self):global num
if mutex.acquire(1):# 如果锁成功,那么线程继续干活,如果锁失败,下面的线程一直等待锁成功,1,代表独占for i inrange(1000):# 数字小的时候还是不会产生线程冲突的
num +=1
mutex.release()# 释放锁,一定切记print(num)
mythread =[]for i inrange(5):
t = Myhtread()
t.start()
mythread.append(t)for thread in mythread:
thread.join()# 或者直接将thread.join()加入for i in range(5),也能解决线程冲突,但是貌似就变成单线程了print("game over")
import threading
import time
boymutex = threading.Lock()# 创建一个锁
girlmutex = threading.Lock()# 创建一个锁classboy(threading.Thread):defrun(self):if boymutex.acquire(1):# 锁定成功就继续执行,锁定不成功,就一直等待print(self.name +"boy say i am sorry up")# time.sleep(3) # 时间过短的话也可以并发执行,不会锁死if girlmutex.acquire(1):# 锁定不成功,因为下面已经锁定print(self.name +"boy say i am sorry down")
girlmutex.release()
boymutex.release()classgirl(threading.Thread):defrun(self):if girlmutex.acquire(1):# 锁定成功就继续执行,锁定不成功,就一直等待print(self.name +"girl say i am sorry up")# time.sleep(3)if boymutex.acquire(1):# 锁定不成功,同理上面已经锁定一直等待print(self.name +"girl say i am sorry down")
boymutex.release()
girlmutex.release()# 开启两个线程# boy1 = boy() # Thread-1boy 第一个线程# boy1.start()# girl1 = girl()# girl1.start()'''
这种例子时间过短是无法很好的产生死锁
for i in range(10):
Mythread1().start()
Mythread2().start()
'''for i inrange(1000):
boy().start()
girl().start()
import threading
num = 0
mutext = threading.RLock() # PLOCK避免单线程死锁
class Mythreading(threading.Thread):
def run(self):
global num
if mutext.acquire(1):
num += 1
print(self.name, num)
if mutext.acquire(1):
num += 1000
mutext.release()
mutext.release()
for i in range(5): # 开启5个进程
t = Mythreading()
t.start()
'''
第一种用函数创建多线程,但是需要处理让脚本主线程不死
import threading
import win32api
class Mythread(threading.Thread): # 继承threading.Thread类
def run(self): # 定义函数
win32api.MessageBox(0, "hello", 'joker', 0)
Mythd = []
for i in range(5):
t = Mythread() # 初始化
print(i)
t.start() # 开启
Mythd.append(t) # 将乱序线程(同时抢夺run这个函数)加入列表
for j in Mythd:
# 这里与顺序不同,上面显示所有的线程都加入Mthd列表(所以一次性跳出5个窗口,但是主线程还没死,因为有join卡住)。
# j是线程
j.join() # 这里主线程同时等待所有线程都执行完毕,才执行“game over”
print("game over")
''''''
第二种是基于类继承创建多线程
import threading
import win32api
class Mythread(threading.Thread): # 继承threading.Thread类
def run(self): # 重写threading.Thread类中的run函数
win32api.MessageBox(0,"hello",'joker',0)
for i in range(5): # 同时创建5个线程
t = Mythread() # 初始化
t.start() # 开启
while True:
pass
''''''
def show(i):
win32api.MessageBox(0,"这是一个测试","来自Joker",0)
threading.Thread(target=show,args=(i,)).start() # 切记这里的args是一个元组
threading.Thread(target=show,args=(i,)).start()
'''# 基于函数构造实现多线程import threading
import win32api
defshow():
win32api.MessageBox(0,"这是一个测试","来自Joker",0)# target=show是线程函数,args=()是参数
threading.Thread(target=show, args=()).start()
threading.Thread(target=show, args=()).start()
import threading
import time
sem = threading.Semaphore(2)# 限制最大线程数为2个defgothread():with sem:# 锁定数量for i inrange(10):print(threading.current_thread().name, i)# 打印线程名字
time.sleep(1)for i inrange(5):
threading.Thread(target=gothread).start()# 乱序执行多线程,就可以考虑为有些cpu牛逼些能够执行快一点
import threading
# 为了合理利用资源# 凑出线程数量,也就是说一定要至少凑成两个才能执行# 换而言之,也就是说只有创建线程数是2,或者2的倍数才能全部执行
bar = threading.Barrier(2)defsever():print(threading.current_thread().name,"start")
bar.wait()print(threading.current_thread().name,"end")for i inrange(3):
threading.Thread(target=sever).start()'''
Thread-1 start
Thread-2 start
Thread-2 end
Thread-1 end
Thread-3 start
这里出现Thread-3 是因为锁定在"start"之后,所以最后面Thread-3 end 是无法出现的
'''
import threading
import time
defgoevent():
e = threading.Event()# 事件defgo():
e.wait()# 等待事件,线程卡顿,等待set消息print("go")
threading.Thread(target=go).start()# 需要创建一个线程return e
t = goevent()
time.sleep(3)
t.set()# 激发事件
import _threadimport timedef go(): for i in range(5): print(i,"-------") time.sleep(1)for i in range(5):#同时执行5次 _thread.start_new_thread(go,())for j in range(6):#让主线程卡顿6秒...