练习知识点:
1.RLock锁的使用及多线程的使用方法
代码如下:
import threading import time class thread_test(threading.Thread): def __init__(self, thread_name): threading.Thread.__init__(self, name=thread_name) def run(self): ''' override the run method''' global var_x lock.acquire() # lock for i in range(3): var_x = var_x + 1 time.sleep(2) print(var_x) lock.release() # unlock lock = threading.RLock() # create RLock object list_obj = [] for i in range(10): thread_obj = thread_test(str(i)) list_obj.append(thread_obj) var_x = 0 # global variable for i in list_obj: i.start() # thread start i.join() # wait utill all the threads terminate
执行结果:
Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 3 6 9 12 15 18 21 24 27 30 >>>
2. 第二种创建多线程的方法
代码如下:
import threading import time def run(): ''' override the run method''' global var_x lock.acquire() # lock for i in range(3): var_x = var_x + 1 time.sleep(2) print(var_x) lock.release() # unlock lock = threading.RLock() # create RLock object list_obj = [] for i in range(10): thread_obj = threading.Thread(name=str(i), target=run) # create thread object list_obj.append(thread_obj) var_x = 0 # global variable for i in list_obj: i.start() # thread start i.join() # wait utill all the threads terminate
3.下面这个简单的生产者消费者问题主要是用来看一下condition的用法。
import threading import time class producer(threading.Thread): def __init__(self,thread_name): threading.Thread.__init__(self,name=thread_name) def run(self): global var_x # global variable con.acquire() if var_x==100: con.wait() pass else: for i in range(100): var_x = var_x + 1 time.sleep(0.1) print('x=', var_x) con.notify() print(var_x) con.release() class consumer(threading.Thread): def __init__(self,thread_name): threading.Thread.__init__(self,name=thread_name) def run(self): global var_x # global variable con.acquire() if var_x==0: con.wait() pass else: for i in range(100): var_x=var_x - 1 print('m=',var_x) con.notify() print(var_x) con.release() con=threading.Condition() var_x=0 p=producer('Producer') c=consumer('Consumer') p.start() c.start() p.join() c.join() print(var_x)
4. Event对象的使用方法。
代码如下:
import threading import time class mythread1(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): print("thread1 run start!/n") s.clear() s.wait(10) print("thread1 on going...../n") print("thread1 end/n") class mythread2(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): print("thread2 run start!/n") time.sleep(3) s.set() print("thread2 on going...../n") print("thread2 end/n") s = threading.Event() m1=mythread1() m2=mythread2() m1.start() m2.start() m1.join() m2.join()
执行结果:
Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> thread1 run start! thread2 run start! thread2 on going..... thread2 end thread1 on going..... thread1 end >>>