zoukankan      html  css  js  c++  java
  • Thread对象的其他属性或方法

    Thread对象的其他属性或方法

    介绍

    Thread实例对象的方法
      # isAlive(): 返回线程是否活动的。
      # getName(): 返回线程名。
      # setName(): 设置线程名。
    
    threading模块提供的一些方法:
      # threading.currentThread(): 返回当前的线程变量。
      # threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
      # threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

    getName()和setName()

    from threading import Thread,currentThread
    import time
    # 当前线程的对象 currentThread
    
    def task():
        print("%s is running" % currentThread().getName())
        time.sleep(2)
        print("%s is done" % currentThread().getName())
    
    if __name__ == "__main__":
        t = Thread(target=task, name="线程1")  # t=currentThread()
        t.start()
        t.setName("儿子线程1")   # 改线程名字
    
        # print(t.getName())
        currentThread().setName("主线程名称")   # 该主线程名字
        print("<主线程>", currentThread().getName())
    

      执行结果

    线程1 is running
    主线程 主线程名称
    儿子线程1 is done

    isAlive()

    from threading import Thread, currentThread
    import time
    # 当前线程的对象 currentThread
    
    def task():
        print("%s is running" % currentThread().getName())
        time.sleep(2)
        print("%s is done" % currentThread().getName())
    
    if __name__ == "__main__":
        t = Thread(target=task, name="线程1")  # t=currentThread()
        t.start()
    
        print(t.isAlive())     # 查看是否存
    

      执行结果

    线程1 is running
    True
    线程1 is done

     

    主线程等待子线程结束

    from threading import Thread, currentThread
    import time
    # 当前线程的对象 currentThread
    
    def task():
        print("%s is running" % currentThread().getName())
        time.sleep(2)
        print("%s is done" % currentThread().getName())
    
    if __name__ == "__main__":
        t = Thread(target=task, name="线程1")  # t=currentThread()
        t.start()
        t.join()
    
        print(t.isAlive())     # 查看是否存活

    执行结果

    线程1 is running
    线程1 is done
    False

    active_count()

    from threading import Thread, currentThread, active_count
    import time
    # 当前线程的对象 currentThread
    
    def task():
        print("%s is running" % currentThread().getName())
        time.sleep(2)
        print("%s is done" % currentThread().getName())
    
    if __name__ == "__main__":
        t = Thread(target=task, name="线程1")  # t=currentThread()
        t.start()
    
        print(active_count())  # 活跃进程数
    

     执行结果:

    线程1 is running
    2
    线程1 is done

    只剩下主线程

    from threading import Thread, currentThread, active_count
    import time
    # 当前线程的对象 currentThread
    
    def task():
        print("%s is running" % currentThread().getName())
        time.sleep(2)
        print("%s is done" % currentThread().getName())
    
    if __name__ == "__main__":
        t = Thread(target=task, name="线程1")  # t=currentThread()
        t.start()
        t.join()
    
        print(active_count())  # 活跃进程数

    执行结果:

    线程1 is running
    线程1 is done
    1

    enumerate()

    from threading import Thread, currentThread, active_count, enumerate
    import time
    # 当前线程的对象 currentThread
    
    def task():
        print("%s is running" % currentThread().getName())
        time.sleep(2)
        print("%s is done" % currentThread().getName())
    
    if __name__ == "__main__":
        t = Thread(target=task, name="线程1")  # t=currentThread()
        t.start()
        # t.join()
    
        print(enumerate())  # 把当前活跃的线程对象取出
    

      执行结果:

    线程1 is running
    [<_MainThread(MainThread, started 10552)>, <Thread(线程1, started 3740)>]
    线程1 is done

  • 相关阅读:
    js获取上传图片真实的尺寸大小和存储大小
    java中Class.getResource用法(用于配置文件的读取)
    Spring Boot Application 事件和监听器
    docker打包centos增加中文支持
    安装polyglot出错
    docker内程序如何读取dockerfile和compose.yml中设置的环境变量
    Docker 构建网络服务后本机不能访问
    docker 错误:Error response from daemon: cannot stop container: connect: connection refused": unknown
    Mac上把python源文件编译成so文件
    Mac下更改Python pip的源
  • 原文地址:https://www.cnblogs.com/fantsaymwq/p/10129327.html
Copyright © 2011-2022 走看看