以前练习过,但好久不用,手生,概念也生了,

重温一下。。

URL:

http://www.cnblogs.com/holbrook/tag/%E5%A4%9A%E7%BA%BF%E7%A8%8B/

~~~~~~~

互斥锁是最简单的线程同步机制,Python提供的Condition对象提供了对复杂线程同步问题的支持。Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还提供了wait和notify方法。线程首先acquire一个条件变量,然后判断一些条件。如果条件不满足则wait;如果条件满足,进行一些处理改变条件后,通过notify方法通知其他线程,其他处于wait状态的线程接到通知后会重新判断条件。不断的重复这一过程,从而解决复杂的同步问题。

可以认为Condition对象维护了一个锁(Lock/RLock)和一个waiting池。线程通过acquire获得Condition对象,当调用wait方法时,线程会释放Condition内部的锁并进入blocked状态,同时在waiting池中记录这个线程。当调用notify方法时,Condition对象会从waiting池中挑选一个线程,通知其调用acquire方法尝试取到锁。

Condition对象的构造函数可以接受一个Lock/RLock对象作为参数,如果没有指定,则Condition对象会在内部自行创建一个RLock。

除了notify方法外,Condition对象还提供了notifyAll方法,可以通知waiting池中的所有线程尝试acquire内部锁。由于上述机制,处于waiting状态的线程只能通过notify方法唤醒,所以notifyAll的作用在于防止有线程永远处于沉默状态。

~~~~~~~~~~~~~~

# coding=utf8
import threading
import time

class Producer(threading.Thread):
    def run(self):
        global count
        while True:
            if con.acquire():
                if count > 1000:
                    con.wait()
                else:
                    count += 100
                    msg = self.name + ' produce 100 ,count=' + str(count)
                    print msg
                    con.notify()
                con.release()
                time.sleep(1)

class Consumer(threading.Thread):
    def run(self):
        global count
        while True:
            if con.acquire():
                if count < 100:
                    con.wait()
                else:
                    count -= 3
                    msg = self.name + ' consume 3, count=' + str(count)
                    print msg
                    con.notify()
                con.release()
                time.sleep(1)

count = 500
con = threading.Condition()
def test():
    for i in range(2):
        p = Producer()
        p.start()
    for i in range(5):
        c = Consumer()
        c.start()

if __name__ == '__main__':
    test()
    print 'end'

# coding=utf8
import threading
import time
from Queue import Queue

class Producer(threading.Thread):
    def run(self):
        global queue
        count = 0
        while True:
            for i in range(1000):
                if queue.qsize() > 1000:
                    pass
                else:
                    count += 1
                    msg = u'生成产品' + str(count)
                    queue.put(msg)
                    print msg
            time.sleep(1)

class Consumer(threading.Thread):
    def run(self):
        global queue
        while True:
            for i in range(3):
                if queue.qsize() < 100:
                    pass
                else:
                    msg = self.name + u'消费了' + queue.get()
                    queue.put(msg)
                    print msg
            time.sleep(1)

queue = Queue()

def test():
    for i in range(500):
        queue.put(u'初始产品'+str(i))
    for i in range(2):
        p = Producer()
        p.start()
    for i in range(5):
        c = Consumer()
        c.start()

if __name__ == '__main__':
    test()
    print 'end'

  

最新文章

  1. DHtmlx组件获取选中行的某一列对应的值
  2. KPI:Key Performance Indicator
  3. 有哪些经常被误用的 HTML、JavaScript、CSS 的元素、方法和属性?
  4. WordPress基础:固定链接的设置
  5. jPList – 实现灵活排序和分页功能的 jQuery 插件
  6. 请求servlet操作成功后,在JSP页面弹出提示框
  7. 【python游戏编程之旅】第六篇---pygame中的Sprite(精灵)模块和加载动画
  8. 关于file的上传文件
  9. Day19_IO第一天
  10. java GZIP压缩和解压
  11. Android Studio 中配置强大的版本管理系统
  12. android 再按一次退出程序(实现代码)
  13. 【转载】 Searching过程粗略梳理
  14. js糟粕
  15. SQL之性能优化
  16. 在React中你真的用对了Ajax吗?
  17. 在实战中使用Sass和Compass
  18. python_9_集合
  19. Python爬虫入门教程 34-100 掘金网全站用户爬虫 scrapy
  20. MySQL Errno : 1062 错误修复

热门文章

  1. [置顶] c#对于文件的操作
  2. Bootstrap的竞争对手Zurb Foundation
  3. Unity3D 之武器系统冷却功能的实现方式
  4. Android开发MVP模式解析
  5. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW _TASK flag.
  6. 在office 2010中插入Mathtype出现ctrl+v不能复制的问题
  7. Visual Studio2013使用Microsoft Office Document Imaging(MODI)的方法
  8. redis基本数据类型【2】-Hash类型
  9. 04_XML_02_XML语法
  10. C#语言之“string格式的日期时间字符串转为DateTime类型”的方法(转)