• 线程之线程的创建,同一进程中线程是数据共享的,解决线程数据安全的办法,进程与线程运行效率的对比,死锁,递归锁,信号量,线程的守护进程


     

    #线程的两种创建方式
    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('主线程结束')
    

      

  • 相关阅读:
    leetcode每日一题(2020-06-24):16. 最接近的三数之和
    leetcode每日一题(2020-06-23):67. 二进制求和
    leetcode每日一题(2020-06-19):125. 验证回文串
    leetcode每日一题(2020-06-18):1028. 从先序遍历还原二叉树
    leetcode每日一题(2020-06-17):1014. 最佳观光组合
    leetcode每日一题(2020-06-16):297. 二叉树的序列化与反序列化
    leetcode每日一题(2020-06-14):1300. 转变数组后最接近目标值的数组和
    leetcode每日一题(2020-06-13):70.爬楼梯
    leetcode每日一题(2020-06-12):15. 三数之和
    springboot 项目中_ 利用Filter去解决跨域问题
  • 原文地址:https://www.cnblogs.com/PythonMrChu/p/9851345.html
Copyright © 2020-2023  润新知