也是锁,这个锁多加了wait(),notify()唤醒一个进程,notifyall()唤醒全部进程方法,创建的时候默认是Rlock类型的锁,可以设置为lock类型的,默认就ok

 from random import randint
import threading
import time class Producer(threading.Thread):
def run(self):
global L
while True:
val = randint(0,100)
print('生产者',self.name,':append',str(val),L)
if lock_con.acquire():
L.append(val)
lock_con.notify()
lock_con.release()
time.sleep(3) class Consumer(threading.Thread):
def run(self):
global L
while True:
lock_con.acquire()
if len(L) == 0:
lock_con.wait()
print('消费者',self.name,'delete',str(L[0]),L)
del L[0]
lock_con.release()
time.sleep(0.5) if __name__ == '__main__':
L = []
lock_con = threading.Condition()
threads = []
for i in range(5):
threads.append(Producer())
threads.append(Consumer())
for t in threads:
t.start()
for t in threads:
t.join()

使用例子

###########java改编:单生产单消费

 import time
import threading class Res:
def __init__(self):
self.flag = False
self.count = 0
self.product = '' def set(self,name):
lock_con.acquire()
if self.flag:
lock_con.wait()
time.sleep(0.00001)
self.count += 1
self.product = ''.join([name,'**',str(self.count)])
self.message = ''.join([self.product,'__生产者__',str(threading.current_thread())])
print(self.message)
self.flag = True
lock_con.notify()
lock_con.release() def get_product(self):
lock_con.acquire()
time.sleep(0.00001)
if not self.flag:
lock_con.wait()
self.message = ''.join([self.product,'__消费者__',str(threading.current_thread())])
print(self.message)
self.flag = False
lock_con.notify()
lock_con.release() class Producer(threading.Thread):
def __init__(self,r):
threading.Thread.__init__(self)
self.r = r def run(self):
for i in range(100):
self.r.set('大白兔奶糖') class Consumer(threading.Thread):
def __init__(self,r):
threading.Thread.__init__(self)
self.r = r def run(self):
for i in range(100):
self.r.get_product() if __name__ == '__main__':
lock_con = threading.Condition()
r = Res()
c = Consumer(r)
p = Producer(r)
c.start()
p.start()

单生产单消费

############多生产多消费

 import time
import threading class Res:
def __init__(self):
self.flag = False
self.count = 0
self.product = '' def set(self,name):
lock_con.acquire()
while self.flag:
lock_con.wait()
time.sleep(0.00001)
self.count += 1
self.product = ''.join([name,'**',str(self.count)])
self.message = ''.join([self.product,'__生产者__',str(threading.current_thread())])
print(self.message)
self.flag = True
lock_con.notifyAll()
lock_con.release() def get_product(self):
lock_con.acquire()
time.sleep(0.00001)
while not self.flag:
lock_con.wait()
self.message = ''.join([self.product,'__消费者__',str(threading.current_thread())])
print(self.message)
self.flag = False
lock_con.notifyAll()
lock_con.release() class Producer(threading.Thread):
def __init__(self,r):
threading.Thread.__init__(self)
self.r = r def run(self):
for i in range(100):
self.r.set('大白兔奶糖') class Consumer(threading.Thread):
def __init__(self,r):
threading.Thread.__init__(self)
self.r = r def run(self):
for i in range(100):
self.r.get_product() if __name__ == '__main__':
lock_con = threading.Condition()
r = Res()
l = []
for i in range(5):
l.append(Consumer(r))
for i in range(5):
l.append(Producer(r))
for a in l:
a.start()

多生产多消费

个人觉得例子理解是最好的,所以我学的东西一般使用例子

039条件变量同步(Condition)的更多相关文章

  1. 第8章 用户模式下的线程同步(4)_条件变量(Condition Variable)

    8.6 条件变量(Condition Variables)——可利用临界区或SRWLock锁来实现 8.6.1 条件变量的使用 (1)条件变量机制就是为了简化 “生产者-消费者”问题而设计的一种线程同 ...

  2. python多线程编程5: 条件变量同步-乾颐堂

    互斥锁是最简单的线程同步机制,Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还 ...

  3. Python学习---线程锁/信号量/条件变量同步/线程池1221

    线程锁 问题现象: 多线程情况下,CPU遇到阻塞会进行线程的切换,所以导致执行了tmp-=1的值还未赋值给num=tmp,另一个线程2又开始了tmp -=1,所以导致最后的值重复赋值给了num,所以出 ...

  4. c++并发编程之条件变量(Condition Variable)

    条件变量(Condition Variable)的一般用法是:线程 A 等待某个条件并挂起,直到线程 B 设置了这个条件,并通知条件变量,然后线程 A 被唤醒.经典的「生产者-消费者」问题就可以用条件 ...

  5. 练习生产者与消费者-PYTHON多线程中的条件变量同步-Queue

    以前练习过,但好久不用,手生,概念也生了, 重温一下.. URL: http://www.cnblogs.com/holbrook/tag/%E5%A4%9A%E7%BA%BF%E7%A8%8B/ ~ ...

  6. PYTHON线程知识再研习E---条件变量同步Condition

    Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的 acquire和release方法外,还提供了wait和notify ...

  7. Linux 多线程条件变量同步

    条件变量是线程同步的另一种方式,实际上,条件变量是信号量的底层实现,这也就意味着,使用条件变量可以拥有更大的自由度,同时也就需要更加小心的进行同步操作.条件变量使用的条件本身是需要使用互斥量进行保护的 ...

  8. c++11多线程记录6:条件变量(condition variables)

    https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...

  9. go条件变量同步机制

    sync.Cond代表条件变量,需要配置锁才能有用 package main import ( "fmt" "runtime" "sync" ...

随机推荐

  1. springmvc4 相关注解的详细讲解

    首先我是一个初学springmvc,抱着去加深印象的目的去整理相关springmvc4的相关注解,同时也希望给需要相关查阅的读者带来帮助. 1.@ControllerController控制器是通过服 ...

  2. dubbo示例

    Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 我也不明白这是什么意思,使用了之后大概就是提供一个将多个项目进行联合的一种分布式,使用的是一 ...

  3. Django多进程日志文件问题

    Django多进程日志文件问题 最近使用Django做一个项目.在部署的时候发现日志文件不能滚动(我使用的是RotatingFileHandler),只有一个日志文件. 查看Log发现一个错误消息:P ...

  4. 设置全局theme及读取theme方法

    在web.config中设置了默认的Theme,其部分如下的配置节点: <system.web> <pages theme="Default" controlRe ...

  5. poj 1284 Primitive Roots(未完)

    Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3155   Accepted: 1817 D ...

  6. React.js 小书 Lesson21 - ref 和 React.js 中的 DOM 操作

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson21 转载请注明出处,保留原文链接和作者信息. 在 React.js 当中你基本不需要和 DO ...

  7. 面向对象 OOP中的抽象类,接口以及多态

    [抽象类与抽象方法] 1.什么是抽象方法? 没有方法体{}的方法,必须使用abstract关键字修饰,这样的方法,我们称之为抽象方法. abstract function say() 2.什么是抽象类 ...

  8. Java基础(七)常用类

    一.Math类 1.Math类介绍 Math类属于java.lang包下面,里面包含用于执行基本数学运算的方法,如初等指数,对数,平方根和三角函数,该类被final修饰. 常用字段: 1.E 表示自然 ...

  9. es入门教程

    因为项目可能会用到es保存一些非结构化的数据,并从中检索数据.对es调研了一下 从官网:https://www.elastic.co/downloads下载,解压即安装. 进入解压目录,执行bin目录 ...

  10. json转换工具类

    using System;using System.Collections.Generic;using System.Text;using Newtonsoft.Json;using System.I ...