线程,锁---生产者消费者

print('线程'.center(50,'-'))
import threading
import time
class Mythread(threading.Thread):
    def __init__(self,name):
        # super().__init__()#初始化父类--1
        # super(Mythread,self).__init__()#初始化父类--2
        threading.Thread.__init__(self)#初始化父类--3
        self.name = name
    def run(self):
        for i in range(1,5):
            time.sleep(1)
            print(self.name + 'run...',i)
mythread = Mythread('t1')
mythread2 = Mythread('t2')
mythread.start()
mythread2.start()

print('锁-生产者消费者'.center(50,'-'))
#多线程容易产生并发 ,修改数据时要加锁,当一个线程要访问共享数据时,必须要先获得锁定,如果已经有
#别的线程获得锁定,那么就进入暂停状态,等别的线程把锁释放后,再进行操作。
#Condition:更精确的控制锁,提供了四个方法,上锁acquire(),等待wait(),解锁release(),唤醒#notify(),notify_all()

# 线程同步-同步阻塞:生产者消费者
import threading
import time
import random
class Huofu(threading.Thread):
     def __init__(self,name=None):
         threading.Thread.__init__(self)
         self.name = name
     def run(self):
         while True:
            cond.acquire()#给吃货上锁
            for i in range(1,11):
               guo.append(i)
               print('做出第{0}个馒头'.format(i))
               time.sleep(1)
            cond.notify_all()#唤醒吃货锁
            cond.release()
            cond2.acquire()#给伙夫上锁
            cond2.wait()#等待,当被唤醒.notify()时才会执行下面的释放.release()
            cond2.release()#释放

class Chihuo(threading.Thread):
     def __init__(self,name=None):
         threading.Thread.__init__(self)
         self.name = name
     def run(self):
         while True:
             mantou=None
             cond.acquire()
             if len(guo)==0:
               cond2.acquire()
               cond2.notify()
               cond2.release()
               cond.wait()
             else:
               mantou=guo.pop()
             cond.release()
             if mantou is not None:#输出不在锁内就不会占用抢锁的时间
               print('{0}正在吃{1}'.format(self.name,mantou))
               time.sleep(random.randint(1,5))

guo = []
lock = threading.Lock()
cond = threading.Condition(lock=lock)#吃的锁
lock2 = threading.Lock()
cond2 = threading.Condition(lock=lock2)#蒸馒头的锁
Huofu(name='做饭和尚').start()
Chihuo(name='吃饭和尚1').start()
Chihuo(name='吃饭和尚2').start()
Chihuo(name='吃饭和尚3').start()

print('其他'.center(50,'-'))
import threading
import time
'''
event = threading.Event()
event.wait()#等待标志位被设定
event.set()#设置标志
event.clear()#清除标志
'''
# join 等待一个线程结束
# 线程内存共享   进程至少包含一个线程
# 线程同时修改同一份数据时必须加锁,mutex互斥锁

def run(n):
    time.sleep(1)
    print(',,,,{}'.format(n))

# 1相当于串行
for i in range(10):
    t = threading.Thread(target = run,args = ('tok',))
    t.start()
    t.join()

#2线程
list1 = []
for i in range(10):
    t = threading.Thread(target = run,args = ('Lucy',))
    t.start()
    list1.append(t)
for r in list1:
    r.join()

# 3守护线程(仆人):服务于非守护线程(主人master)
for i in range(10):
    t = threading.Thread(target = run,args = ('tom',))
    t.setDaemon(True)
    t.start()
time.sleep(0.5)#主线程时间(分别试试time.sleep(0.5)和time.sleep(3),有不一样的结果)
# time.sleep(3)
print('master is done')#主线程结束则守护线程结束


# queue 队列 有序的,只有一份数据取完就没了
# 解耦,是程序直接实现松耦合,提高效率
# FIFO = first in first out
# LIFO = last in first out

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值