zoukankan      html  css  js  c++  java
  • Python多线程(二):多线程的调度setDaemon() join()的用法

    在多线程调度中常常会遇到的三个场景:

    主线程 子线程 方法
    结束 继续执行 setDaemon(False)默认
    结束 跟着主线程立刻结束 setDaemon(True)
    结束->阻塞 执行结束后,一起终止 join()

    1.当一个进程启动后,会默认产生一个主线程,当设置多线程时,主线程会创建多个子线程。python在默认的情况下setDaemon(False),主线程执行完自己的任务后,就退出了,此时子线程会继续执行自己的任务,直到自己的任务结束

    import threading, time, datetime
    
    
    def eat():
        time.sleep(2)
        print(datetime.datetime.now().strftime('%X') + " 当前线程的名字是:" + threading.current_thread().name)
        time.sleep(2)
    
    
    if __name__ == '__main__':
        print(datetime.datetime.now().strftime('%X') + " 我是主线程: " + threading.current_thread().name)
    
        thread_list = []
        for i in range(5):
            t = threading.Thread(target=eat)
            thread_list.append(t)
    
        for t in thread_list:
            t.start()
        print(datetime.datetime.now().strftime('%X') + " 主线程结束了!!!!!!!!!!!!!!!")

    执行结果如下:

    13:31:42 我是主线程: MainThread
    13:31:42 主线程结束了!!!!!!!!!!!!!!!
    13:31:44 当前线程的名字是:Thread-3
    13:31:44 当前线程的名字是:Thread-2
    13:31:44 当前线程的名字是:Thread-1
    13:31:44 当前线程的名字是:Thread-4
    13:31:44 当前线程的名字是:Thread-5
    
    Process finished with exit code 0

    需要注意的地方是:主线程的任务结束后,主线程随之结束,子线程继续执行自己的任务,直到子线程任务全部结束,程序结束。

    2.第二种情况是当我们设置了setDaemon(True)方法,设置子线程为守护线程时,只要主线程一结束,则全部线程立即终止,不管子线程的任务有没有结束。

    import threading, time, datetime
    
    
    def eat():
        time.sleep(2)
        print(datetime.datetime.now().strftime('%X') + " 当前线程的名字是:" + threading.current_thread().name)
        time.sleep(2)
    
    
    if __name__ == '__main__':
        print(datetime.datetime.now().strftime('%X') + " 我是主线程: " + threading.current_thread().name)
    
        thread_list = []
        for i in range(5):
            t = threading.Thread(target=eat)
            thread_list.append(t)
    
        for t in thread_list:
            t.setDaemon(True) #设置守护进程,必须在start()之前
            t.start()
        print(datetime.datetime.now().strftime('%X') + " 主线程结束了!!!!!!!!!!!!!!!")

    执行结果为:

    13:37:20 我是主线程: MainThread
    13:37:20 主线程结束了!!!!!!!!!!!!!!!
    
    Process finished with exit code 0

    可以看到,子线程都还没来得及执行,程序就退出了

    3.第三种情况,join的作用就是实现线程同步,即主线程任务结束后,进入阻塞状态,等待子线程执行结束后,主线程再终止

    import threading, time, datetime
    
    
    def eat():
        time.sleep(2)
        print(datetime.datetime.now().strftime('%X') + " 当前线程的名字是:" + threading.current_thread().name)
        time.sleep(2)
    
    
    if __name__ == '__main__':
        print(datetime.datetime.now().strftime('%X') + " 我是主线程: " + threading.current_thread().name)
    
        thread_list = []
        for i in range(5):
            t = threading.Thread(target=eat)
            thread_list.append(t)
    
        for t in thread_list:
            t.setDaemon(True)
            t.start()
    
        for t in thread_list:
            t.join() #放在start之后
    
        print(datetime.datetime.now().strftime('%X') + " 主线程结束了!!!!!!!!!!!!!!!")

    执行结果为:

    13:42:06 我是主线程: MainThread
    13:42:08 当前线程的名字是:Thread-1
    13:42:08 当前线程的名字是:Thread-2
    13:42:08 当前线程的名字是:Thread-5
    13:42:08 当前线程的名字是:Thread-4
    13:42:08 当前线程的名字是:Thread-3
    13:42:10 主线程结束了!!!!!!!!!!!!!!!
    
    Process finished with exit code 0

    可以看到,主线程等待所有子线程结束后,自身才结束,程序退出

  • 相关阅读:
    HDU 5818 Joint Stacks
    HDU 5816 Hearthstone
    HDU 5812 Distance
    HDU 5807 Keep In Touch
    HDU 5798 Stabilization
    HDU 5543 Pick The Sticks
    Light OJ 1393 Crazy Calendar (尼姆博弈)
    NEFU 2016省赛演练一 I题 (模拟题)
    NEFU 2016省赛演练一 F题 (高精度加法)
    NEFU 2016省赛演练一 B题(递推)
  • 原文地址:https://www.cnblogs.com/ronyjay/p/12868618.html
Copyright © 2011-2022 走看看