• 多线程复习1


    1.进程在运行时会创建一个主线程,每一个进程只有一个主线程
    2.子进程 pid 唯一标识符
    3.任意时间里,只有一个线程在运行,且运行顺序不能确定(全局锁限制)
    4.threading.Thread(target = test,args = [i])
        target = 函数名 ,args = [ 参数 ]
    5.可以继承 threading.Thread 然后重写 run 方法
        class myThread(threading.Thread):
            def run():
                pass
    
    程序:
    # import threading 
    # # 导入线程库
    # def test():
    #     print(1)
    
    # a = threading.Thread(target = test)
    # # 创建 a 线程
    # b = threading.Thread(target = test)
    # a.start()
    # # 启动线程
    # b.start()
    
    # a.join()
    # b.join()
    # # 结束之前使用 join 等待其他线程
    
    
    import threading 
    # 导入线程库
    import time 
    def test(i):
        time.sleep(0.1)
        print(i)
    
    tests_thread = []
    for i in range(0,10):
        threads = threading.Thread(target = test,args = [i])
        # 创建 a 线程
        tests_thread.append(threads)
    
    
    for i in tests_thread:
        i.start()
        # 启动线程
    
    
    for i in tests_thread:
        i.join()
        # 结束之前使用 join 等待其他线程
    
    print("线程结束")

    2020-04-12

  • 相关阅读:
    本周学习小结(04/11
    学习笔记之知识图谱 (Knowledge Graph)
    本周学习小结(28/10
    本周学习小结(21/10
    条件分页 代替离线查询
    Apache POI 一键上传(导入excel文件到数据库)
    easyui 菜单按钮&提示框
    Jquery ztree树插件
    Jquery ztree树插件2
    ui
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12685980.html
Copyright © 2020-2023  润新知