python-多线程小演练2

标题:

1.Lock版本生产者与消费者模式:

# encoding=utf-8

import threading
import random
import time
# 走全局变量生产的钱
gMoney = 1000
gLock = threading.Lock()
gTotalTimes = 0
gTimes = 0
# 定义生产者:
class Producer (threading.Thread):
    # 定义 run 方法
    def run(self):
        global gMoney
        global gTimes
        while True:
            money = random.randint(100,1000)
            gLock.acquire()
            if gTimes >= gTotalTimes:
                gLock.release()
                break
            gMoney += money
            # threading.current_thread() :当前线程
            print('%s生产了%d元钱,剩余%d元钱' %(threading.current_thread(),
                                       money,gMoney))
            gTimes += 1
            gLock.release()
            time.sleep(0.5)

class Consumer (threading.Thread):
    # 定义 run 方法
    def run(self):
        global gMoney
        while True:
            money = random.randint(100,1000)
            gLock.acquire()
            if gMoney >= money:
                gMoney -= money
                print('%s消费者消费了%d元,剩余%d元钱' %(threading.current_thread(),
                                             money,gMoney))
            else:
                if gTimes >= gTotalTimes:
                    gLock.release()
                    break
                print('%s消费者准备消费%d元钱,剩余%d元钱,不足!' %(threading.current_thread(),
                                                   money,gMoney))
            gLock.release()
            time.sleep(0.5)


def main():
    # 定义消费者
    for x in range(3):
        t = Consumer(name="消费者线程%d"%x)
        t.start()

    # 定义生产者
    for x in range(5):
       t = Producer(name="生产者线程%d"%x)
       t.start()



if __name__ == '__main__':
    main()


2.Condition版的生产者与消费者模式:

# encoding=utf-8

import threading
import random
import time
# 走全局变量生产的钱
gMoney = 1000
gCondition = threading.Condition()
gTotalTimes = 0
gTimes = 0
# 定义生产者:
class Producer (threading.Thread):
    # 定义 run 方法
    def run(self):
        global gMoney
        global gTimes
        while True:
            money = random.randint(100,1000)
            gCondition.acquire()
            if gTimes >= gTotalTimes:
                gCondition.release()
                break
            gMoney += money
            # threading.current_thread() :当前线程
            print('%s生产了%d元钱,剩余%d元钱' %(threading.current_thread(),
                                       money,gMoney))
            gTimes += 1
            gCondition.notify_all()
            gCondition.release()
            time.sleep(0.5)

class Consumer (threading.Thread):
    # 定义 run 方法
    def run(self):
        global gMoney
        while True:
            money = random.randint(100,1000)
            gCondition.acquire()
            while gMoney < money:
                if gTimes >= gTotalTimes:
                    gCondition.release()
                    return
                print('%s准备消费%d元钱,剩余%d元钱,不足!' %(threading.current_thread(),
                                                money,gMoney))
                gCondition.wait()
            gMoney -= money
            print('%s消费了%d元钱,剩余%d元钱' %(threading.current_thread(),
                  money,gMoney))
            gCondition.release()
            time.sleep(0.5)


def main():
    # 定义消费者
    for x in range(3):
        t = Consumer(name="消费者线程%d"%x)
        t.start()

    # 定义生产者
    for x in range(5):
        t = Producer(name="生产者线程%d"%x)
        t.start()



if __name__ == '__main__':
    main()


你可能感兴趣的:(python-多线程小演练2)