''''' Created on 2012-9-7 @author: walfred @module: thread.ThreadTest3 @description: ''' import threading classMyThread(threading.Thread): def__init__(self): threading.Thread.__init__(self) defrun(self): print"I am %s" % (self.name) if __name__ == "__main__": for i in range(0, 5): my_thread = MyThread() my_thread.start()
''''' Created on 2012-9-8 @author: walfred @module: thread.ThreadTest3 ''' import threading import time counter = 0 classMyThread(threading.Thread): def__init__(self): threading.Thread.__init__(self) defrun(self): global counter time.sleep(1); counter += 1 print"I am %s, set counter:%s" % (self.name, counter) if __name__ == "__main__": for i in range(0, 200): my_thread = MyThread() my_thread.start()
I am Thread-69, set counter:64I am Thread-73, set counter:66I am Thread-74, set counter:67I am Thread-75, set counter:68 I am Thread-76, set counter:69I am Thread-78, set counter:70I am Thread-77, set counter:71I am Thread-58, set counter:72 I am Thread-60, set counter:73I am Thread-62, set counter:74I am Thread-66, set counter:75I am Thread-70, set counter:76 I am Thread-72, set counter:77I am Thread-79, set counter:78I am Thread-71, set counter:78
''' Created on 2012-9-8 @author: walfred @module: thread.ThreadTest4 ''' import threading import time counter = 0 mutex = threading.Lock() classMyThread(threading.Thread): def__init__(self): threading.Thread.__init__(self) defrun(self): global counter, mutex time.sleep(1); if mutex.acquire(): counter += 1 print"I am %s, set counter:%s" % (self.name, counter) mutex.release() if __name__ == "__main__": for i in range(0, 100): my_thread = MyThread() my_thread.start()
''''' Created on 2012-9-8 @author: walfred @module: thread.TreadTest5 ''' import threading counterA = 0 counterB = 0 mutexA = threading.Lock() mutexB = threading.Lock() classMyThread(threading.Thread): def__init__(self): threading.Thread.__init__(self) defrun(self): self.fun1() self.fun2() deffun1(self): global mutexA, mutexB if mutexA.acquire(): print"I am %s , get res: %s" %(self.name, "ResA") if mutexB.acquire(): print"I am %s , get res: %s" %(self.name, "ResB") mutexB.release() mutexA.release() deffun2(self): global mutexA, mutexB if mutexB.acquire(): print"I am %s , get res: %s" %(self.name, "ResB") if mutexA.acquire(): print"I am %s , get res: %s" %(self.name, "ResA") mutexA.release() mutexB.release() if __name__ == "__main__": for i in range(0, 100): my_thread = MyThread() my_thread.start()
''''' Created on 2012-9-8 @author: walfred @module: thread.ThreadTest6 ''' import threading import time counter = 0 mutex = threading.Lock() classMyThread(threading.Thread): def__init__(self): threading.Thread.__init__(self) defrun(self): global counter, mutex time.sleep(1); if mutex.acquire(): counter += 1 print"I am %s, set counter:%s" % (self.name, counter) if mutex.acquire(): counter += 1 print"I am %s, set counter:%s" % (self.name, counter) mutex.release() mutex.release() if __name__ == "__main__": for i in range(0, 200): my_thread = MyThread() my_thread.start()
''''' Created on 2012-9-9 @author: walfred @module: thread.TreadTest8 ''' import threading import time classMyThread(threading.Thread): def__init__(self, signal): threading.Thread.__init__(self) self.singal = signal defrun(self): print"I am %s,I will sleep ..."%self.name self.singal.wait() print"I am %s, I awake..." %self.name if __name__ == "__main__": singal = threading.Event() for t in range(0, 3): thread = MyThread(singal) thread.start() print"main thread sleep 3 seconds... " time.sleep(3) singal.set() ``` 运行效果如下:
I am Thread-1,I will sleep … I am Thread-2,I will sleep … I am Thread-3,I will sleep … main thread sleep 3 seconds… I am Thread-1, I awake…I am Thread-2, I awake… I am Thread-3, I awake… `