'''
A boolean value indicating whether this thread is a daemon thread (True) or not (False).
This must be set before start() is called, otherwise RuntimeError is raised.
Its initial value is inherited from the creating thread; the main thread is not
a daemon thread and therefore all threads created in the main thread default to
daemon = False.
The entire Python program exits when no alive non-daemon threads are left.
当daemon被设置为True时,如果主线程退出,那么子线程也将跟着退出,
反之,子线程将继续运行,直到正常退出。
'''
import threading
import time
class myThread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name=threadname)
self.sleeptime = 2
def run(self):
time.sleep(self.sleeptime)
print(self.getName())
def setSleeptime(self, t):
self.sleeptime = t
def fun1():
t1.start()
print("fun1 done")
def fun2():
t2.start()
print("fun2 done")
t1=myThread("t1")
t2=myThread("t2")
t2.setSleeptime(10);
t2.setDaemon(True)
fun1()
fun2()
print("now u will see me")
'''
D:\CODE\PYTHON\Python_Bao3Dian3>python tt.py
fun1 done
fun2 done
now u will see me
t1
'''
# -*- coding:utf-8 -*-
# file: syn.py
# 23.2 线程同步
'''
23.2.1 简单的线程同步
使用Thread对象的Lock和RLock可以实现简单的线程同步. 如果某个数据在某一时刻只允许一个线程进行操作,则可以
将操作过程放在acquire方法和release方法之间.
'''
import threading# 导入threading模块
import time
class mythread(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
global x
#lock.acquire()
for i in range(3):
x += 1
time.sleep(2)
print(x)
#lock.release()
#lock = threading.RLock()
t1 = []
for i in range(3):
t = mythread(str(i))
t1.append(t)
x = 0
for i in t1:
i.start()
'''
D:\CODE\PYTHON\Python_Bao3Dian3>python e23_2_1a.py
3
6
9
D:\CODE\PYTHON\Python_Bao3Dian3>python e23_2_1a.py
9
9
9
'''
# -*- coding:utf-8 -*-
# 23.2 线程同步
# P_C.py
'''
23.2.2 使用条件变量保持线程同步
Python的Condition对象提供了对复杂线程同步的支持. 使用Condition对象可以在某些事件
触发后在处理数据. Condition对象除了具有acquire方法和release方法外, 还有wait方法, notify方法,
notifyAll方法等用于条件处理的方法. 下面的P_C.py是使用Condition对象来实现著名的
生产者和消费者的关系.
'''
import threading# 导入threading模块
class Producer(threading.Thread):# 定义生产者类
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
global x
con.acquire()# 调用con的acquire方法
if x == 1000000:
con.wait()# 调用con的wait方法
#pass
else:
for i in range(1000000):
x = x + 1
con.notify()# 调用con的notify方法
print(x)
con.release()# 调用con的release方法
class Consumer(threading.Thread):# 定义生产者类
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
global x
con.acquire()
if x == 0:
con.wait()
#pass
else:
for i in range(1000000):
x = x -1
con.notify()
print(x)
con.release()
con = threading.Condition()# 生成Condition对象
x = 0
p = Producer('Producer')# 生成生产者对象
c = Consumer('Consumer')# 生成消费者对象
p.start()
c.start()
p.join()# 等待p线程结束
c.join()
print(x)
'''
D:\CODE\PYTHON\Python_Bao3Dian3>python e23_2_2a.py
1000000
0
0
# 把使用Condition对象的语句删除. 运行修改后的脚本, 输出如下(每次输出可能不同)
D:\Python27>python D:\CODE\PYTHON\Python_Bao3Dian3\ttb.py
-29904
895935
895935
'''
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 = 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 = 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()