zoukankan      html  css  js  c++  java
  • 多线程复习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

  • 相关阅读:
    php获取文件后缀名格式
    猴子分桃问题2
    猴子吃桃问题1
    判断字符串中字母出现的次数用分割法
    成绩表
    二维数组所有元素求和输出
    冒泡排序
    1.8作业
    1.8
    1.6
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12685980.html
Copyright © 2011-2022 走看看