2019 7.6学习笔记

多线程共享全局变量以及锁机制

import threading
VALUE=0
# gLock=threading.Lock()
def add_value():
    global VALUE
    # gLock.acquire()
    for i in range(1000000):
        VALUE+=1
    # gLock.release()
    print('value:%d'%VALUE)

def main():
    for x in range(1000000):
        t=threading.Thread(target=add_value)
        t.start()
if __name__ == '__main__':
    main()

 lock版本的生产者与消费者模型

import threading
import random
import time

gMoney=1000
gLock=threading.Lock()
gTotaltimes=10
gTime=0
class Producer(threading.Thread):
    def run(self):
        global gMoney,gTotaltimes,gTime
        while True:
            money=random.randint(100,200)
            gLock.acquire()
            if gTime>=gTotaltimes:
                gLock.release()
                break
            gMoney+=money
            print("%s生产了%d元钱,剩余%d的钱"%(threading.current_thread(),money,gMoney))
            gTime+=1
            gLock.release()
            time.sleep(1)
class Consumer(threading.Thread):
    def run(self):
        global gMoney,gTotaltimes,gTime
        while True:
            money = random.randint(150, 250)
            gLock.acquire()
            if gMoney>=money:
                gMoney -= money
                print("%s消费了%d 元钱,剩余%d的钱" % (threading.current_thread(), money, gMoney))
            else:
                if gTime>=gTotaltimes:
                    gLock.release()
                    break
                print("%s消费者准备消费%d元钱,剩余%d元钱,余额不足"%(threading.current_thread(),money,gMoney))

            gLock.release()
            time.sleep(1)

def main():
    for x in range(3):
        t=Producer(name="生产者线程%d"%x)
        t.start()
    for i in range(5):
        t=Consumer(name="消费者线程%d"%i)
        t.start()

if __name__ == '__main__':
    main()

  condition版的生产者与消费者模型:

import threading
import random
import time

gMoney=1000
gCondition=threading.Condition()
gTotaltimes=10
gTime=0
class Producer(threading.Thread):
    def run(self):
        global gMoney,gTotaltimes,gTime
        while True:
            money=random.randint(100,200)
            gCondition.acquire()
            if gTime>=gTotaltimes:
                gCondition.release()
                break
            gMoney+=money
            print("%s生产了%d元钱,剩余%d的钱"%(threading.current_thread(),money,gMoney))
            gTime+=1
            gCondition.notify_all()
            gCondition.release()
            time.sleep(1)
class Consumer(threading.Thread):
    def run(self):
        global gMoney,gTotaltimes,gTime
        while True:
            money = random.randint(150, 250)
            gCondition.acquire()
            while  gMoney<money:
                if gTime>=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(1)

def main():
    for x in range(3):
        t=Producer(name="生产者线程%d"%x)
        t.start()
    for i in range(5):
        t=Consumer(name="消费者线程%d"%i)
        t.start()

if __name__ == '__main__':
    main()

queue线程安全队列

from queue import Queue
import time
import threading
# q=Queue(4)
# q.put(2)
# q.put(3)
# q.put(1)           #添加数据
# print(q.get())     #取数据 先进先出     put跟get都有一个block=True默认参数
# print(q.qsize())    #打印队列的长度
# print(q.empty())  #判断是否为空
# print(q.full())  #判断是否为满
def set_value(q):
    index=0
    while True:
        q.put(index)
        index+=1
        time.sleep(3)
def get_value(q):
    while True:
        print(q.get())
def main():
    q = Queue(4)
    t1=threading.Thread(target=set_value,args=[q])
    t2=threading.Thread(target=get_value,args=[q])

    t1.start()
    t2.start()
if __name__ == '__main__':
    main()

 

posted on 2019-07-06 13:49  Joker乔柯  阅读(159)  评论(0编辑  收藏  举报

导航