这篇文章本来只想写python的线程同步,不过因为之前有JAVA线程同步的经验,所以也温故知新下。个人mark下,很基础的知识,大牛绕过。文中部分知识点非原创。
JAVA线程同步的实现:synchronized,wait(),notify
wait和notify是基于最底层的object基础类,每个对象都有notify和wait的功能。因为他们是用来操纵锁的,而每个对象都有锁,锁是每个对象的基础,既然锁是基础的,那么操纵锁的方法当然也是最基础了。
wait()允许我们将线程置入“睡眠”状态,同时又“积极”地等待条件发生改变.而且只有在一个notify()或notifyAll()发生变化的时候,线程才会被唤醒,并检查条件是否有变。wait可以设置时间,如wait(1000),以毫秒为单位。
以下为例:
synchronized(obj) { while(!condition) { obj.wait(); } obj.doSomething(); }
当线程A获得了obj锁后,发现条件condition不满足,无法继续下一处理,于是线程A就wait()。
在另一线程B中,如果B更改了某些条件,使得线程A的condition条件满足了,就可以唤醒线程A:
synchronized(obj) { condition = true; obj.notify(); }
补充说明:
# 调用obj的wait(), notify()方法前,必须获得obj锁,也就是必须写在synchronized(obj) {…} 代码段内。
# 调用obj.wait()后,线程A就释放了obj的锁,否则线程B无法获得obj锁,也就无法在synchronized(obj) {…} 代码段内唤醒A。
# 当obj.wait()方法返回后,线程A需要再次获得obj锁,才能继续执行。
# 如果A1,A2,A3都在obj.wait(),则B调用obj.notify()只能唤醒A1,A2,A3中的一个(具体哪一个由JVM决定)。
# obj.notifyAll()则能全部唤醒A1,A2,A3,但是要继续执行obj.wait()的下一条语句,必须获得obj锁,因此,A1,A2,A3只有一个有机会获得锁继续执行,例如A1,其余的需要等待A1释放obj锁之后才能继续执行。
# 当B调用obj.notify/notifyAll的时候,B正持有obj锁,因此,A1,A2,A3虽被唤醒,但是仍无法获得obj锁。直到B退出synchronized块,释放obj锁后,A1,A2,A3中的一个才有机会获得锁继续执行。
锁是和对象相关联的,每个对象有一把锁,为了执行synchronized语句,线程必须能够获得synchronized语句中表达式指定的对象的锁,一个对象只有一把锁,被一个线程获得之后它就不再拥有这把锁,线程在执行完synchronized语句后,将获得锁交还给对象。
在方法前面加上synchronized修饰符即可以将一个方法声明为同步化方法。同步化方法在执行之前获得一个锁。如果这是一个类方法,那么获得的锁是和声明方法的类相关的Class类对象的锁。如果这是一个实例方法,那么此锁是this对象的锁。
常用的方法:
wait()是使持有对象锁的线程释放锁;
wait(long)是使持有对象锁的线程释放锁时间为long(毫秒)后,再次获得锁,wait()和wait(0)等价;
notify()是唤醒一个正在等待该对象锁的线程,如果等待的线程不止一个,那么被唤醒的线程由jvm确定;
notifyAll是唤醒所有正在等待该对象锁的线程.
在这里我也重申一下,我们应该优先使用notifyAll()方法,因为唤醒所有线程比唤醒一个线程更容易让jvm找到最适合被唤醒的线程.
1.有synchronized的地方不一定有wait,notify
2.有wait,notify的地方必有synchronized.这是因为wait和notify不是属于线程类,而是每一个对象都具有的方法,而且,这两个方法都和对象锁有关,有锁的地方,必有synchronized。
PYTHON线程同步的实现:Thread对象的Lock和Rlock实现简单的线程同步
当然也可以自己来实现一个锁,还有一种利用了队列的特性,来实现同步,对我来说,以下这个方法已经足够满足我的需求了,而且是我熟悉的方式,很方便。
import 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=x+1 time.sleep(2) print x lock.release() lock=threading.RLock() t1=[] for i in range(10): t=mythread(str(i)) t1.append(t) x=0 for i in t1: i.start() 运行结果如下: 3 6 9 12 15 18 21 24 27 30
而如果我们把acquire()和release()去掉,结果就不同了:
30303030303030303030
这是因为每个线程执行后在打印出x之前都要休眠2秒钟,所以在这个过程中,每个线程都被执行了,所以等到休眠结束,打印出的X的值自然就是经过多次运算以后的X的值了。
而第一次,我们把全局变量X放到了acquire()和release()之间,python解释器每次回只允许一个线程对x进行操作,只有这个线程结束对其操作并且休眠结束打印出来以后,才允许下一个线程对x操作,所以输出的X是每次递增的,而且用时间也是比较长的。
我自己是用这个程序测试的:
#!/home/oracle/dbapython/bin/python # -*- coding: utf-8 -*- import threading import time import random class mythread(threading.Thread): def __init__(self,threadname): threading.Thread.__init__(self,name=threadname) def run(self): global x while 1: time.sleep(random.randint(1, 3)) lock.acquire() for i in range(10000): x=x+1 print self.getName,"result:",x lock.release() lock=threading.RLock() t1=[] for i in range(10): t=mythread('thread'+str(i)) t1.append(t) x=0 for i in t1: i.start()
输出结果如下,可以看到线程完全是随机执行的,但是实现了同步:
<bound method mythread.getName of <mythread(thread0, started 1084229984)>> result: 10000 <bound method mythread.getName of <mythread(thread6, started 1147169120)>> result: 20000 <bound method mythread.getName of <mythread(thread4, started 1126189408)>> result: 30000 <bound method mythread.getName of <mythread(thread3, started 1115699552)>> result: 40000 <bound method mythread.getName of <mythread(thread1, started 1094719840)>> result: 50000 <bound method mythread.getName of <mythread(thread2, started 1105209696)>> result: 60000 <bound method mythread.getName of <mythread(thread8, started 1168148832)>> result: 70000 <bound method mythread.getName of <mythread(thread5, started 1136679264)>> result: 80000 <bound method mythread.getName of <mythread(thread7, started 1157658976)>> result: 90000 <bound method mythread.getName of <mythread(thread9, started 1178638688)>> result: 100000 <bound method mythread.getName of <mythread(thread6, started 1147169120)>> result: 110000 <bound method mythread.getName of <mythread(thread3, started 1115699552)>> result: 120000 <bound method mythread.getName of <mythread(thread1, started 1094719840)>> result: 130000 <bound method mythread.getName of <mythread(thread2, started 1105209696)>> result: 140000 <bound method mythread.getName of <mythread(thread0, started 1084229984)>> result: 150000 <bound method mythread.getName of <mythread(thread5, started 1136679264)>> result: 160000 <bound method mythread.getName of <mythread(thread9, started 1178638688)>> result: 170000 <bound method mythread.getName of <mythread(thread4, started 1126189408)>> result: 180000 <bound method mythread.getName of <mythread(thread1, started 1094719840)>> result: 190000 <bound method mythread.getName of <mythread(thread8, started 1168148832)>> result: 200000 <bound method mythread.getName of <mythread(thread0, started 1084229984)>> result: 210000 <bound method mythread.getName of <mythread(thread7, started 1157658976)>> result: 220000 <bound method mythread.getName of <mythread(thread3, started 1115699552)>> result: 230000 <bound method mythread.getName of <mythread(thread4, started 1126189408)>> result: 240000 <bound method mythread.getName of <mythread(thread2, started 1105209696)>> result: 250000 <bound method mythread.getName of <mythread(thread1, started 1094719840)>> result: 260000 <bound method mythread.getName of <mythread(thread5, started 1136679264)>> result: 270000 <bound method mythread.getName of <mythread(thread6, started 1147169120)>> result: 280000
如果将LOCK去掉后,再看看情况,发现连print的结果都串到一起了:
<bound method mythread.getName of <mythread(thread3, started 1115699552)>> result: 10000 <bound method mythread.getName of <mythread(thread5, started 1136679264)>> result: 20000 <bound method mythread.getName of <mythread(thread0, started 1084229984)>> result: 30000 <bound method mythread.getName of <mythread(thread7, started 1157658976)>> result: 41012 <bound method mythread.getName of <mythread(thread5, started 1136679264)>> result: 46454 <bound method mythread.getName of <mythread(thread6, started 1147169120)>> <bound method mythread.getName of <mythread(thread3, started 1115699552)>> result: 49583result: 49583 <bound method mythread.getName of <mythread(thread1, started 1094719840)>> result: 59955 <bound method mythread.getName of <mythread(thread4, started 1126189408)>> result: 67112 <bound method mythread.getName of <mythread(thread2, started 1105209696)>> result: 75509 <bound method mythread.getName of <mythread(thread8, started 1168148832)>> result: 79509 <bound method mythread.getName of <mythread(thread9, started 1178638688)>> result: 84812 <bound method mythread.getName of <mythread(thread7, started 1157658976)>> result: 94812 <bound method mythread.getName of <mythread(thread6, started 1147169120)>> result: 104812 <bound method mythread.getName of <mythread(thread0, started 1084229984)>> result: 114812 <bound method mythread.getName of <mythread(thread5, started 1136679264)>> result: 127309 <bound method mythread.getName of <mythread(thread4, started 1126189408)>> result: 130577 <bound method mythread.getName of <mythread(thread7, started 1157658976)>> result: 134481 <bound method mythread.getName of <mythread(thread8, started 1168148832)>> result:<bound method mythread.getName of <mythread(thread6, started 1147169120)>> result:142885 142885 <bound method mythread.getName of <mythread(thread3, started 1115699552)>> result: 145033 <bound method mythread.getName of <mythread(thread1, started 1094719840)>> result: 155033 <bound method mythread.getName of <mythread(thread2, started 1105209696)>> result: 166833 <bound method mythread.getName of <mythread(thread9, started 1178638688)>> result: 173080
参考自: