#线程的两种创建方式 import time from threading import Thread def func(i): time.sleep(0.1) print(i) class myThread(Thread): def __init__(self,n): super().__init__() self.n=n def run(self): print('n>>>',self.n) if __name__ == '__main__': t=Thread(target=func,args=(1,)) t.start() t2=myThread('傻子') t2.start() t.join() print('主线程结束')
#同一进程下各个线程资源共享 import time from threading import Thread from multiprocessing import Process num = 100 def func(): global num num = 0 if __name__ == '__main__': t = Thread(target=func,) t.start() t.join() print(num)
#解决共享数据不安全问题 import time from threading import Thread,Lock haha=100 def func(lock): global haha with lock:#加锁 num=haha num-=1 haha=num time.sleep(0.00001) if __name__ == '__main__': lock=Lock()#同步锁,互斥锁 lst=[] for i in range(10): t=Thread(target=func,args=(lock,))#定义线程 t.start()#开启线程 lst.append(t) [tt.join() for tt in lst]#主线程等子线程完成 print('haha:',haha) print('主进程结束。。。')
#进程与线程运行效率对比 import time from threading import Thread from multiprocessing import Process def func(n): num = 0 for n1 in range(n): num += n1 print('num',num) if __name__ == '__main__': t_s_t = time.time() t_list = [] for i in range(10): t = Thread(target=func,args=(10,)) t.start() #速度非常快 t_list.append(t) [tt.join() for tt in t_list] t_e_t = time.time() t_dif_t = t_e_t - t_s_t #获取了线程的执行时间 p_s_t = time.time() #开始时间 p_list = [] for ii in range(10): p = Process(target=func,args=(10,)) p.start() p_list.append(p) [pp.join() for pp in p_list] p_e_t = time.time() #结束时间 p_dif_t = p_e_t - p_s_t #时间差 print('线程>>>>',t_dif_t) print('进程....',p_dif_t) print('主线程结束')
#死锁 import time from threading import Thread,Lock def func1(lockA,lockB): lockA.acquire() print('得到A锁') lockB.acquire() print('这是func1') time.sleep(0.01) lockB.release() lockA.release() def func2(lockA,lockB): lockB.acquire() print('得到B锁') lockA.acquire() print('这是func2') time.sleep(0.01) lockA.release() lockB.release() def haha(lockA,lockB): func1(lockA,lockB) func2(lockA,lockB) if __name__=="__main__": lockA=Lock() lockB=Lock() t1=Thread(target=haha,args=(lockA,lockB)) t1.start() t2=Thread(target=haha,args=(lockA,lockB)) t2.start() t1.join() t2.join() print('主线程结束')
#递归锁,解决线程死锁问题 import time from threading import Thread,RLock def func1(lockA,lockB): lockA.acquire() print('得到A锁') lockB.acquire() print('这是func1') time.sleep(0.01) lockB.release() lockA.release() def func2(lockA,lockB): lockB.acquire() print('得到B锁') lockA.acquire() print('这是func2') time.sleep(0.01) lockA.release() lockB.release() def haha(lockA,lockB): func1(lockA,lockB) func2(lockA,lockB) if __name__=="__main__": lockA=lockB=RLock() t1=Thread(target=haha,args=(lockA,lockB)) t1.start() t2=Thread(target=haha,args=(lockA,lockB)) t2.start() t1.join() t2.join() print('主线程结束')
#信号量 import time from threading import Thread,Semaphore def func(semaphore,i): semaphore.acquire() print(i) time.sleep(2) semaphore.release() if __name__=='__main__': semaphore=Semaphore(4) t_list=[] for i in range(10): t=Thread(target=func,args=(semaphore,i)) t.start() t_list.append(t) [tt.join() for tt in t_list] print('主线程结束')
#线程的守护 ##守护线程会跟随主线程代码的结束而结束,但是主线程不会结束,主线程会等非守护子线程结束后才结束 import time from threading import Thread def func1(n): time.sleep(2) print(n) def func2(n): time.sleep(1) print(n) if __name__ == '__main__': t1=Thread(target=func1,args=('我是子线程1',)) t1.daemon=True t1.start() t2=Thread(target=func2,args=('我是子线程2',)) t2.start() print('主线程结束')