python-다선정 소연련2

17701 단어

제목:


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()


좋은 웹페이지 즐겨찾기