zoukankan      html  css  js  c++  java
  • 线程 守护线程 递归锁

    ------------------------------方法不对,功力全废;做事讲方法,做人讲原则.
    # # ------------------------------------------------------------------------------------------------------------#

    ''''
    线程理论
    线程是cpu的最小执行单位,进程:资源单位

    线程的创建
    From threading import Thread
    T = Thread(target=func,args=())

    Class MyThread(Thread):
    Def __init__(self,n):
    Super().__init()
    Self.n = n

    Def run(self):
    Pass
    T = MyThread(12)

    线程和进程效率对比
    Join方法

    线程之间是数据共享的
    同一进程中线程之间是数据共享的,全局变量验证了一下

    锁(互斥锁同步锁) #Lock,解决数据共享时数据不安全的问题,但是同时了牺牲了效率

    死锁现象:双方互相等待对方释放对方手里拿到的那个锁

    递归锁: Rlock 同样是互斥的.里面存着个计数器

    守护线程:主线程等着进程中所有非守护线程的结束,才算结束
    守护进程:主进程代码结束,守护进程跟着结束

    信号量:大宝剑

    事件: Event

    '''
    # # # --------------[线程的创建]--------------
    #
    # import time
    # from threading import Thread
    # from multiprocessing import Process
    # #
    # # def func(n):
    # # print(n)
    # #
    # # if __name__ == '__main__':
    # # # t = Thread(target=func,args=('我是线程',))
    # # # t.start() #速度非常快
    # # p = Process(target=func,args=('我是进程',))
    # # p.start()
    # # print('主线程结束')
    #
    # # class MyThread(Thread):
    # #
    # # def run(self):
    # # print('sb明杰')
    # #
    # # if __name__ == '__main__':
    # # t = MyThread()
    # # t.start()
    # # 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
    # 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


    #演示共享资源的时候,数据不安全的问题
    # num = 100
    # def func():
    # global num
    # # num -= 1
    # mid = num
    # mid = mid - 1
    # time.sleep(0.0001)
    # num = mid
    # if __name__ == '__main__':
    #
    # t_list = []
    # for i in range(10):
    # t = Thread(target=func,)
    # t.start()
    # t_list.append(t)
    # [tt.join() for tt in t_list]
    # print('主线程>>>',num)


    ## # --------------[通过锁来解决数据不安全]--------------
    #的问题,线程模块里面引入的锁
    # num = 100
    # def func(t_lock):
    # global num
    # # num -= 1
    # t_lock.acquire()
    # mid = num
    # mid = mid - 1
    # time.sleep(0.0001)
    # num = mid
    # t_lock.release()
    # if __name__ == '__main__':
    # t_lock = Lock() #锁对象(同步锁,互斥锁)
    # t_list = []
    # for i in range(10):
    # t = Thread(target=func,args=(t_lock,))
    # t.start()
    # t_list.append(t)
    # [tt.join() for tt in t_list]
    #
    # print('主线程>>>',num)

    ## --------------[死锁现象]--------------

    import time
    from threading import Thread,Lock,RLock

    def fuc1(locka,lockb):
    locka.acquire()
    print("+++++++++++++++a")

    lockb.acquire()
    print("+++++++++++++++b")

    locka.release()
    lockb.release()


    def fuc2(locka, lockb):
    lockb.acquire()
    print("+++++++++++++++B")

    locka.acquire()
    print("+++++++++++++++A")

    lockb.release()
    locka.release()

    if __name__ == '__main__':
    # lock_A = Lock()
    # lock_B = Lock()
    lock_A = lock_B = RLock()
    t1 = Thread(target=fuc1, args=(lock_A, lock_B))
    t2 = Thread(target=fuc2, args=(lock_A, lock_B))
    t1.start()
    t2.start()



    # # --------------[守护线程]--------------
    #------主线程 会随着非守护线程的结束 而结束

    import time
    from threading import Thread
    from multiprocessing import Process
    def func1(n):
    time.sleep(5)
    print(n)

    def func2(n):
    time.sleep(3)
    print(n)

    if __name__ == '__main__':


    # # --------------[复习回顾---守护进程]--------------
    # ------主线程结束 守护进程也结束

    #进程+守护进程示例#
    p1 = Process(target=func1,args=('子进程1号',))
    p1.daemon = True #设置守护,在start之前加
    p1.start()
    p2 = Process(target=func2, args=('子进程2号',))
    # p2.daemon = True
    p2.start()

    # 线程+守护线程示例#
    # t1 = Thread(target=func1, args=('子线程一号',))
    # t1.daemon = True #设置守护,在start之前加
    # t1.start()

    # t2 = Thread(target=func2, args=('子线程二号',))
    # t2.setdaemon(Ture)
    # t2.start().


    print('主进程结束!!')
    # print('主线程结束')





  • 相关阅读:
    【转】VS2010中 C++创建DLL图解
    [转]error: 'retainCount' is unavailable: not available in automatic reference counting mode
    [转]关于NSAutoreleasePool' is unavailable: not available in automatic reference counting mode的解决方法
    【转】 Tomcat v7.0 Server at localhost was unable to start within 45
    【转】Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If
    【转】SVN管理多个项目版本库
    【转】eclipse安装SVN插件的两种方法
    【转】MYSQL启用日志,和查看日志
    【转】Repository has not been enabled to accept revision propchanges
    【转】SVN库的迁移
  • 原文地址:https://www.cnblogs.com/dealdwong2018/p/10044534.html
Copyright © 2011-2022 走看看