python多线程--theading模块
来源:互联网 发布:华云数据许广彬 编辑:程序博客网 时间:2024/06/05 05:39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#coding:utf-8
import
threading
import
time
cond
=
threading.Condition()
class
kongbaige(threading.Thread):
def
__init__(
self
, cond, diaosiname):
threading.Thread.__init__(
self
, name
=
diaosiname)
self
.cond
=
cond
def
run(
self
):
self
.cond.acquire()
#获取锁
print
self
.getName()
+
':一支穿云箭'
#空白哥说的第一句话
self
.cond.notify()
#唤醒其他wait状态的线程(通知西米哥 让他说话)
#然后进入wait线程挂起状态等待notify通知(等西米哥的回复,接下来俩人就开始扯蛋)
self
.cond.wait()
print
self
.getName()
+
':山无棱,天地合,乃敢与君绝!'
self
.cond.notify()
self
.cond.wait()
print
self
.getName()
+
':紫薇!!!!(此处图片省略)'
self
.cond.notify()
self
.cond.wait()
print
self
.getName()
+
':是你'
self
.cond.notify()
self
.cond.wait()
#这里是空白哥说的最后一段话,接下来就没有对白了
print
self
.getName()
+
':有钱吗 借点'
self
.cond.notify()
#通知西米哥
self
.cond.release()
#释放锁
class
ximige(threading.Thread):
def
__init__(
self
, cond, diaosiname):
threading.Thread.__init__(
self
, name
=
diaosiname)
self
.cond
=
cond
def
run(
self
):
self
.cond.acquire()
self
.cond.wait()
#线程挂起(等西米哥的notify通知)
print
self
.getName()
+
':千军万马来相见'
self
.cond.notify()
#说完话了notify空白哥wait的线程
self
.cond.wait()
#线程挂起等待空白哥的notify通知
print
self
.getName()
+
':海可枯,石可烂,激情永不散!'
self
.cond.notify()
self
.cond.wait()
print
self
.getName()
+
':尔康!!!(此处图片省略)'
self
.cond.notify()
self
.cond.wait()
print
self
.getName()
+
':是我'
self
.cond.notify()
self
.cond.wait()
#这里是最后一段话,后面空白哥没接话了 所以说完就释放锁 结束线程
print
self
.getName()
+
':滚'
self
.cond.release()
kongbai
=
kongbaige(cond,
' '
)
ximi
=
ximige(cond,
'西米'
)
#尼玛下面这2个启动标志是关键,虽然是空白哥先开的口,但是不能让他先启动,
#因为他先启动的可能直到发完notify通知了,西米哥才开始启动,
#西米哥启动后会一直处于44行的wait状态,因为空白哥已经发完notify通知了进入wait状态了,
#而西米哥没收到
#造成的结果就是2根线程就一直在那挂起,什么都不干,也不扯蛋了
ximi.start()
kongbai.start()
######运行结果######
:一支穿云箭
西米:千军万马来相见
:山无棱,天地合,乃敢与君绝!
西米:海可枯,石可烂,激情永不散!
:紫薇!!!!(此处图片省略)
西米:尔康!!!(此处图片省略)
:是你
西米:是我
:有钱吗 借点
西米:滚
转自:http://zeping.blog.51cto.com/6140112/1258977
0 0
- python多线程--theading模块
- python 多线程和多进程的区别 mutiprocessing theading
- Python 多线程 Threading模块
- python threading多线程模块
- Python多线程模块
- python多线程-threading模块
- python 多线程模块
- python多线程模块thread
- python多线程模块threading
- python多线程模块threading简例
- [Python]多线程--threading模块实现
- python模块学习---多线程处理
- Python 多线程(threading模块)
- Python 多线程(threading模块)
- Python的多线程threading模块
- python多线程编程----threading模块
- python模块threading实现多线程
- Python 多线程编程threading模块
- LINQ to SQL
- COOKIE的作用域
- H.265学习之路(一):参考软件HM配置
- SAP ABAP编程 弹出按选择对话框函数-'POPUP_TO_CONFIRM'
- rabbitmq direct类型中,生产者端无法接收应答
- python多线程--theading模块
- JAVA操作properties文件
- windows下安装redis
- redis安装+redis集群配置+phpredis扩展安装
- 蓝屏dump分析教程,附分析工具WinDbg
- 我的RUBY之路--第三周(04) ubuntu 14.04 titanium 的安装
- asp.netWeb开发中ztree实现无极限增删改查连接数据同步数据库操作
- Linux下如何编译并运行C程序
- 11.07 深拷贝 浅拷贝