zoukankan      html  css  js  c++  java
  • python 多线程信号生产者消费者例子

    问题:
    生产者一次生产完6个后,消费者开始消费,消费完6个后,接着开始生产

    条件对象实现:

    import threading, time
    """
        生产5个开始消费
    
    """
    
    count = 0
    # 用 Sem
    semaphore = threading.Condition()
    
    class Product(threading.Thread):
        def run(self):
            global count
            semaphore.acquire()
            while True:
                if count <= 5:
                    count += 1
                    time.sleep(2)
                    print(f"开始生产:生产1个,现在{count}个")
                else:
                    print("生产够了,开始消费")
                    semaphore.notify()
                    semaphore.wait()
                
                
        
    
    class Consume(threading.Thread):
        def run(self):
            global count
            semaphore.acquire()
            while True:
                if count > 0:
                    count -= 1
                    time.sleep(2)
                    print(f"开始消费:消费1个,现在{count}个")
                    
                else:
                    print("消费够了,开始生产")
                    semaphore.notify()
                    semaphore.wait()
    

    信号量对象实现

    count = 0
    # 用 Sem
    semaphore1 = threading.Semaphore(1)
    semaphore2 = threading.Semaphore(0)
    
    class Product(threading.Thread):
        def run(self):
            global count
            semaphore1.acquire()
            while True:
                if count <= 5:
                    count += 1
                    time.sleep(2)
                    print(f"开始生产:消费1个,现在{count}个")
                else:
                    semaphore2.release()
                    print("生产够了,开始消费")
                    semaphore1.acquire()
                    
                    
                
                
        
    
    class Consume(threading.Thread):
        def run(self):
            global count
            semaphore2.acquire()
            while True:
                if count > 0:
                    count -= 1
                    time.sleep(2)
                    print(f"开始消费:消费1个,现在{count}个")
                    
                else:
                    
                    semaphore1.release()
                    print("消费够了,开始生产")
                    semaphore2.acquire()
    

    时间对象实现

    count = 0
    # 用 Sem
    event1 = threading.Event()
    event2 = threading.Event()
    
    class Product(threading.Thread):
        def run(self):
            global count
            while True:
                if count <= 5:
                    count += 1
                    time.sleep(2)
                    print(f"开始生产:生产1个,现在{count}个")
                else:
                    
                    print("生产够了,开始消费")
                    event2.set()
                    event1.clear()
                    event1.wait()
                    
                    
    
    class Consume(threading.Thread):
        def run(self):
            global count
            while True:
                event2.wait()
                if count > 0:
                    count -= 1
                    time.sleep(2)
                    print(f"开始消费:消费1个,现在{count}个") 
                else:
                    print("消费够了,开始生产")
                    event1.set()
                    event2.clear()
                    event2.wait()
    
    

    主程序:

    product = Product()
    product.start()
    
    consume = Consume()
    consume.start()
    
  • 相关阅读:
    search
    longestValidParentheses
    nextPermutation
    linux下通过安装xampp搭建环境卸载xampp、以及网站搬家-64位系统Apache启动不了的问题
    对拍器 ADC-ACMDataCreater
    Educational Codeforces Round 86 (Rated for Div. 2) A~D
    Codeforces Round #637 (Div. 2) A~D
    Codeforces Round #636 (Div. 3) A~D
    Codeforces Round #635 (Div. 2) A~D
    原型设计工具比较及实践 基于WolframAlphaAPI的科学计算器原型设计
  • 原文地址:https://www.cnblogs.com/ShanCe/p/14659582.html
Copyright © 2011-2022 走看看