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. Django-website 程序案例系列-3 URL详解

    django参考资料:http://docs.30c.org/djangobook2/index.html urls.py是django中控制接收前端的参数指定函数去执行逻辑 第一种 函数的方式 ur ...

  2. BZOJ2135 刷题计划(贪心+二分)

    相邻数作差后容易转化成将这些数最多再切m刀能获得的最小偏差值.大胆猜想化一波式子可以发现将一个数平均分是最优的.并且划分次数越多能获得的偏差值增量越小.那么就可以贪心了:将所有差扔进堆里,每次取出增量 ...

  3. BZOJ5300 [Cqoi2018]九连环 【数学】【FFT】

    题目分析: 这道题是数学必修五的原题,做法如下图,书上讲得很详细了. 那么这道题目用快速幂就可以解决了,值得注意的是,分析时间复杂度会发现直接做乘法其实是O(n^2)的,但是有一个1/20左右的常数, ...

  4. codeforces 797B

    B. Odd sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  5. Rsync 服务器端配置

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

  6. Leetcode 29.两数相除 By Python

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 1: 输 ...

  7. Leetcode 28.实现strStr() By Python

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  8. BZOJ4332 JSOI2012 分零食 【倍增 + NTT】

    题目链接 权限题BZOJ4332 题解 容易想到\(dp\) 设\(g[i][j]\)表示前\(i\)人分到\(j\)颗糖的所有方案的乘积之和 设\(f(x) = Ox^2 + Sx + U\) \[ ...

  9. 【uoj291】 ZJOI2017—树状数组

    http://uoj.ac/problem/291 (题目链接) 题意 一个写错的树状数组有多大的概率与正常树状数组得出的答案一样. Solution 可以发现这个树状数组维护的是后缀和. 所以二维线 ...

  10. MATLAB:图像乘除法运算(immultiply、imdivide函数),同时比较双精度类型图像运算结果

    图像乘除法运算涉及到immultiply.imdivide函数,实现代码如下: 1.图像之间的乘法运算 close all; %关闭当前所有图形窗口,清空工作空间变量,清除工作空间所有变量 clear ...