多线程入门

1.多线程实现并发:

同时进行多个操作弹窗:

import _thread
import win32api

def show():
    win32api.MessageBox(0,"你的电脑有点危险","360安全卫士",1)

for i in range(5):
    _thread.start_new_thread(show,())
show()

结果:

2.并发传参:

import _thread
import win32api

def show(i):
    win32api.MessageBox(0,"你的电脑有点危险"+str(i),"360安全卫士",1)

for i in range(5):
    _thread.start_new_thread(show,(i,))#函数   元组
show(10)

结果:

 3.多进程实现加速:

import _thread
import time
def go():
    for i in range(5):
        print("------------"+str(i)+"----------------")
        time.sleep(1)
for i in range(10):
    _thread.start_new_thread(go,())#创建线程10个

go()
print("over")

结果:

 4.基于类实现多线程:
 

import threading
import _thread
import win32api
class Mythread(threading.Thread):#继承
    def run(self):#重写
        win32api.MessageBox(0,"你完了","正义的使者",1)

for i in range(5):
    t = Mythread()#创建
    t.start()#开启

while 1:#把进程卡住
    pass

结果:

 5.类线程的顺序执行:

import threading
import time
import win32api  #引用系统函数

class Mythread(threading.Thread): #继承threading.Thread
    def run(self): #run重写,
        win32api.MessageBox(0, "你的账户很危险", "来自支付宝的问候", 6)


for  i  in range(5):
    t=Mythread() #初始化
    t.start() #开启
    t.join() #主线程等待线程t执行完成,解决线程冲突的一种办法

结果:

 6.类线程的乱序执行:
 

import threading
import time
import win32api  #引用系统函数

class Mythread(threading.Thread): #继承threading.Thread
    def run(self): #run重写,
        win32api.MessageBox(0, "你的账户很危险", "来自支付宝的问候", 6)

mythread=[] #集合list
for  i  in range(5):
    t=Mythread() #初始化
    t.start()
    mythread.append(t)#加入线程集合

for mythd  in mythread:  #mythd是一个线程
    mythd.join() #主线程等待线程t执行完成,不需要阻塞,
print("game over")

结果:

 7.基于类解决撞车:

import threading

mutex=threading.Lock()#创建一个锁
num=0
class Mythread(threading.Thread):
    def run(self):
        global num
        if mutex.acquire(1):#锁住成功继续干活,没有成功就一直等待,1代表独占
            for i in range(1000000):#锁定期间其他人不能干活
                num+=1
            mutex.release()#释放锁
        print(num)

mythread=[]
for i in range(5):
    t=Mythread()
    t.start()
    mythread.append(t)
for i in mythread:
    t.join()
print("over")

结果:

 8.死锁:
 

import  threading
import time
boymutex=threading.Lock() #创建一个锁
girlmutex=threading.Lock() #创建一个锁

class  boy(threading.Thread):
    def run(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()

class  girl(threading.Thread):
    def run(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()
boy1.start()
girl1=girl()
girl1.start()

结果:

 如果把boy的锁提到if上面的话,就能避免死锁

9.Rlock:
 

import threading

num=0
mutex=threading.RLock()#Rlock避免单线程死锁
class Mythread(threading.Thread):
    def run(self):
        global num
        if mutex.acquire(1):
            num+=1
            print(self.name,num)
            if mutex.acquire(1):
                num+=1000
                mutex.release()
            mutex.release()

for i in range(8):
    t=Mythread()
    t.start()

结果:

 10.创建线程的三种风格:
 


# 函数实现
# import _thread
# import win32api
#
# def show():
#     win32api.MessageBox(0,"你的电脑有点危险","360安全卫士",1)
#
# for i in range(5):
#     _thread.start_new_thread(show,())
# show()

# 类实现
import threading
import win32api


class Mythread(threading.Thread):  # 继承threading
    def __init__(self,num):
        threading.Thread.__init__(self)#父类初始化
        self.num=num
    def run(self):  # 重写
        win32api.MessageBox(0, "你完了", "正义的使者", 1)

for i in range(5):
    t = Mythread(i)  # 创建
    t.start()  # 开启



# import threading
# import win32api
# def show(i):
#     win32api.MessageBox(0, "你完了"+str(i), "正义的使者", 1)
# #target =show线程函数,args=()参数
# #基于类的构造实现多线程
# threading.Thread(target=show,args=(1,)).start()
# threading.Thread(target=show,args=(2,)).start()
# threading.Thread(target=show,args=(3,)).start()
# threading.Thread(target=show,args=(4,)).start()
# threading.Thread(target=show,args=(5,)).start()

11.信号量限制线程数量:
 

import threading
import time

sem=threading.Semaphore(10)#限制线程的最大数量为2个

def gothread():
    with sem:#锁定数量
        for i in range(10):
            print(threading.current_thread().name,i)
            time.sleep(2)

for i in range(6):
    threading.Thread(target=gothread).start()

结果就是最多开展10个进程,不能超过那个数量

12.锁定匹配线程数量:
 

import time
import threading

#为了合理利用资源1000,必须n=10个线程一起执行
bar=threading.Barrier(3)#必须一起才能执行

def sever():
    print(threading.current_thread().name,"start")
    time.sleep(3)
    bar.wait()#凑现场
    print(threading.current_thread().name,"end")

for i in range(5):
    threading.Thread(target=sever).start()

结果是每次线程一定是规定的数量工作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值