cond = threading.Condition()

# 类似lock.acquire()

cond.acquire()

# 类似lock.release()

cond.release()

# 等待指定触发,同时会释放对锁的获取,直到被notify才重新占有琐。

cond.wait()

# 发送指定,触发执行

cond.notify()

以下分别为两个线程,三个线程,四个线程。可直接看四个线程的运行过程,更加直观,有时候用文字解释不如直接实验让人理解的快。

import threading,time

cond = threading.Condition()
#cond.acquire()
#cond.release()
#cond.wait()
#cond.notify()
def aa():
print ('thread_aa get')
cond.acquire()
time.sleep(5)
print ('thread_aa finished first job')
cond.wait()#释放对锁的控制,好让别的进程acquire到
#同时阻塞在此,等待得到锁的那个进程的先cond.notify后(cond.wait或cond.release)
print ('thread_aa get again')
time.sleep(5)
print ('thread_aa finished all work')
cond.notify()#告诉刚才释放锁的那个进程等通知吧,等我要么wait要么release
cond.release()#释放锁
def bb():
cond.acquire()
print ('thread_bb get')
cond.notify()#告诉刚才释放锁的那个进程等通知吧,等我要么wait要么release
print ('依然是我bb在运行')
time.sleep(5)
print ('thread_bb finished first job')
cond.wait()#释放对锁的控制,而后被阻塞的aa就可以执行了
#同时阻塞在此,等待得到锁的aa的先cond.notify后(cond.wait或cond.release)
print ('thread_aa get again')
print ('thread_bb finished all works')
cond.release() a=threading.Thread(target=aa)
a.start()
time.sleep(2)
b=threading.Thread(target=bb)
b.start()
import threading,time

cond = threading.Condition()
def aa():
cond.acquire()
time.sleep(1)
print ('aa1')
cond.wait()
time.sleep(1)
print ('aa2')
cond.notify()
cond.wait()
print ('aa3')
cond.notify()
cond.wait()
print ('aa4')
cond.release()
def bb():
cond.acquire()
print ('bb1')
cond.wait()#没notify而直接wait,就是将对a的控制权抛出,但是自己的控
# 制权还在a那里,任意一个 acquire到锁的进城将得到对a的控
# 制权(除非他自己又抛出),而这个acquire到锁的进程的控制
# 权在这个进程,如果这个进程在之后退出了,那么他控制的
# 进程的控制权将转交给控制他的进程 print ('bb2')
cond.notify()
cond.release()
def cc():
cond.acquire()
print ('cc1')
cond.notify()
cond.wait()
print ('cc2')
cond.notify()
cond.wait()
print ('cc3')
cond.notify()
cond.release()
a=threading.Thread(target=aa)
a.start()
time.sleep(2)
b=threading.Thread(target=bb)
b.start()
time.sleep(2)
c=threading.Thread(target=cc)
c.start() '''这是三个以上版本的,或许更加直观。运行结果:
aa1
bb1
cc1
aa2
bb2
cc2
aa3
cc3
aa4
'''
import threading,time

cond = threading.Condition()
def aa():
cond.acquire()
time.sleep(1)
print ('aa1')
cond.wait()
time.sleep(1)
print ('aa2')
cond.wait()
print ('aa3')
cond.notify()
cond.wait()
print ('aa4')
cond.release()
def bb():
cond.acquire()
print ('bb1')
cond.wait() print ('bb2')
cond.notify()
cond.release()
def cc():
cond.acquire()
print ('cc1')
cond.notify()
cond.wait()
print ('cc2')
cond.notify()
cond.wait()
print ('cc3')
cond.notify()
cond.release()
def dd():
cond.acquire()
print ('dd1')
cond.notify()
cond.release()
a=threading.Thread(target=aa)
a.start()
time.sleep(2)
b=threading.Thread(target=bb)
b.start()
time.sleep(2)
c=threading.Thread(target=cc)
c.start()
time.sleep(2)
d=threading.Thread(target=dd)
d.start() '''这是4个版本的,运行结果:
aa1
bb1
cc1
aa2
dd1
bb2
cc2
aa3
cc3
aa4
'''

python线程之condition的更多相关文章

  1. python 线程之 threading(四)

    python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...

  2. python 线程之 threading(三)

    python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...

  3. python 线程之_thread

    python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...

  4. python多线程之Condition(条件变量)

    #!/usr/bin/env python # -*- coding: utf-8 -*- from threading import Thread, Condition import time it ...

  5. python笔记11-多线程之Condition(条件变量)

    前言 当小伙伴a在往火锅里面添加鱼丸,这个就是生产者行为:另外一个小伙伴b在吃掉鱼丸就是消费者行为.当火锅里面鱼丸达到一定数量加满后b才能吃,这就是一种条件判断了. 这就是本篇要讲的Condition ...

  6. python 线程之threading(五)

    在学习了Event和Condition两个线程同步工具之后还有一个我认为比较鸡肋的工具 semaphores 1. 使用semaphores的使用效果和Condition的notify方法的效果基本相 ...

  7. python 线程之 threading(二)

    在http://www.cnblogs.com/someoneHan/p/6204640.html 线程一中对threading线程的开启调用做了简单的介绍 1 在线程开始之后,线程开始独立的运行直到 ...

  8. python 线程之 threading(一)

    threading:基于对象和类的较高层面上的接口,threading模块在内部使用_thread模块来实现线程的对象以及常用的同步化工具的功能. 使用定制类的方式继承 threading.Threa ...

  9. iOS多线程之8.NSOPeration的其他用法

      本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...

随机推荐

  1. ubuntu 安装 postgresql

    安装环境: Ubuntu 10.04-desktop-i386 PostgreSQL 8.4 1. 安装PostgreSQL 输入如下命令 sudo apt-get install postgresq ...

  2. Rsync 服务器端配置

    Centos 6.3 已经自带Rsync服务 安装xinetd # yum -y install xinetd 编辑/etc/xinetd.d/rsync文件,把disable = yes修改为dis ...

  3. 自学Linux Shell12.3-case命令

    点击返回 自学Linux命令行与Shell脚本之路 12.3-case命令 有了case命令,就不需要写出所有elif语句来不停的检查同一个变量的值了.case命令会采用列表格式来检查单个变量的多个值 ...

  4. 自学Zabbix14.1 二次开发API

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix14.1 二次开发API Zabbix API我们可以做很多,自己开发web界面. ...

  5. 洛谷 P1613 跑路 解题报告

    P1613 跑路 题目描述 小\(A\)的工作不仅繁琐,更有苛刻的规定,要求小\(A\)每天早上在\(6:00\)之前到达公司,否则这个月工资清零.可是小\(A\)偏偏又有赖床的坏毛病.于是为了保住自 ...

  6. Android 屏幕手势滑动中onFling()函数的技巧分析

    关于如何处理手势操作以及那四个基本固定的顺序我就不讲解了,这里直接跳到我们获得瞬间滑动后回调onFling()这个抽象函数时,应该如何根据参数比较准确的判断滑动方向.如果你没有前面的基础知识,你可以去 ...

  7. 51nod1238 最小公倍数之和 V3

    又被这神仙题给坑爆了. 神仙题解. 一开始我把lcm变成ij/gcd然后按照常规套路去推,推到最后发现不是miu * Id而是miu · Id......这还搞鬼啊. 正解居然跟这个差不多,先转成求其 ...

  8. getComponent()与getSource()

    Component[] items = 父控件.getComponents(); 获取父控件里的控件,返回Component类的数组.如panel中的许多buttone.getSource() 获取发 ...

  9. Linux command ------ vi / vim

    EDIT mode to GENERAL mode: press ESC General mode: operate file :q!    :force to close the file but ...

  10. 启动tomcat时报错:java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException:A child container failed during start

    解决方法:https://www.cnblogs.com/xiangxinhouse/p/6377842.html